flutter remove back button on appbar

0 votes

I am wondering, if anyone knows of a way to remove the back button that shows up on the appBar in a flutter app when you use Navigator.pushNamed to go to another page. The reason I do not want it on this resulting page is that it is coming from the navigation and I want users to use the logout button instead, so that the session starts over.

Mar 9, 2023 in Android by sarit
• 1,830 points
15,489 views

1 answer to this question.

0 votes

You can remove the back button from the AppBar in Flutter by setting the AppBar's leading property to null. Here's an example:

AppBar(
  leading: null,
  // other AppBar properties
);

In your case, you can conditionally set the leading property to null based on whether the current page is the resulting page or not. Here's an example:

AppBar(
  leading: Navigator.of(context).canPop() ? null : IconButton(
    icon: Icon(Icons.logout),
    onPressed: () {
      // handle logout
    },
  ),
  // other AppBar properties
);

In this example, the leading property is set to null if the current page can be popped (i.e., there is a previous page on the navigation stack), and it's set to an IconButton with an "logout" icon and a callback that handles the logout if the current page cannot be popped (i.e., it's the resulting page).

To know more about Flutter, join our Flutter Certification Course today.

answered Mar 10, 2023 by vinayak