I want to add action icons to my AppBar as shown in this Flutter example
However I’m using go_flutter and I can’t see any property in the MaterialApp.router constructor that would allow to insert icon widgets.
Here’s my code so far. How can I add action icons from here ?
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
title: 'My App',
routerConfig: _router,
debugShowCheckedModeBanner: false,
);
}
}
final GoRouter _router = GoRouter(routes: [
GoRoute(path: '/', builder: ((context, state) => Loading())),
GoRoute(name: 'error', path: '/error/:message/:statusCode', builder: (context, state) => CheckError(
message: state.params['message']!,
statusCode: state.params['statusCode']!
)),
GoRoute(name: 'home', path: '/home', builder: (context, state) {
Map data = state.extra as Map;
return Home(data: data);
},
),
]);
2
Answers
You need to use
push
instead ofgoNamed
.@Duddy67 answering your comment. And extending @Yeasin Sheikh’s answer.
I assume you want to have
<-
button in the appBar of the new screen.You need to use
context.push()
to add page to the navigation stack.If you use
context.go()
you are just replacing the page, so there is no screen to go back to .Note: You need to add
appBar: AppBar(title:Text("Home Page"))
inside the scaffold for the<-
to appear. (Emphasis on invokingAppBar
instance not on setting title.)