17

I've successfully navigated to a class in a new file, but now I'm not getting a backbutton on my appBar. Here is my navigation from main.dart...

new RaisedButton(
                onPressed: () {
                  Navigator.pushNamed(context, '/searchpage');
                },

and here is my SearchPage appBar..

class SearchPageState extends State<SearchPage> {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        routes: <String, WidgetBuilder>{
          '/loginpage': (BuildContext context) => new Login.LoginPage(),
          '/mainpage': (BuildContext context) => new Main.MyApp(),
        },
        home: new Scaffold(
      appBar: new AppBar(
        title: new Text(
          "Search",
          style: new TextStyle(fontWeight: FontWeight.bold),
        ),
      ),
Charles Jr
  • 5,303
  • 12
  • 43
  • 68

2 Answers2

39

You should only have one MaterialApp at the root of your app. Each MaterialApp creates its own Navigator, and the existence of multiple routes on the navigation stack is what causes the implicit back button to appear in the leading slot of the AppBar.

Collin Jackson
  • 80,972
  • 26
  • 188
  • 132
  • 1
    Thanks, it really helped! It worked by returning a Scaffold in child page widget rather than having it in new MaterialApp. – DragonCherry Oct 13 '20 at 05:19
8

Remove NavigationDrawer from page, where you need to see back button. If your page specified NavigationDrawer, then hamburger menu will be showed instead of back button.

Yauhen Sampir
  • 1,489
  • 10
  • 13