[Flutter] Hide back button in AppBar

2023-03-18 hit count image

Let's see how to hide the auto generated back button in the AppBar on the Flutter app.

Outline

When the screen is switched in Flutter, the back button is automatically shown in AppBar on the second screen even if you didn’t configure it.

Flutter automaticallyImplyLeading - back button

In this blog post, I will introduce how to hide the back button generated automatically in AppBar.

You can see the full source code of this blog post in GitHub.

Hide back button

In the AppBar widget, the back button is shown even if there is nothing in the leading option.

class SecondScreen extends StatelessWidget {
  const SecondScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Second Screen'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () => Navigator.pop(context),
          child: const Text('Go Back'),
        ),
      ),
    );
  }
}
Flutter automaticallyImplyLeading - back button

To hide the back button here, you need to use the automaticallyImplyLeading option in the AppBar widget.

You can hide the back button to set the automaticallyImplyLeading option in the AppBar widget like the following.

class SecondScreen extends StatelessWidget {
  const SecondScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: const Text('Second Screen'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () => Navigator.pop(context),
          child: const Text('Go Back'),
        ),
      ),
    );
  }
}

And then, you can see the back button disappeared like the following.

Flutter automaticallyImplyLeading - hide back button

Completed

Done! we’ve seen how to hide the back button generated automatically in the AppBar widget. It’s very convenient that the back button is created automatically, but we don’t need it sometimes, so remember the automaticallyImplyLeading option to remove the back button.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts