I am new to flutter and I am trying to create a simple app with a "static" that doesn’t change between pages. I want also that the app bar will have a back button the page is changed, how can i keep truck of that.
I have a created a widget for the app bar and tried making it statefull widget and save the Navigator state so that when it updates i will update my appBar widget.
I will add the code for how i change pages as well:
onTap: () {
Navigator.push(
context,
PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) =>
UserInfoPage(data: 'Hello from Home Page!'),
transitionsBuilder:
(context, animation, secondaryAnimation, child) {
return FadeTransition(opacity: animation, child: child);
},
),
);
},
Thank you for your help guys, I am here to learn 🙂
2
Answers
To help you with navigation and control of the back button in the AppBar, a good practice is to use the Scaffold widget on each page and manage navigation through the Navigator. Then, you can configure the AppBar of each page to display the back button automatically when needed, using the automaticallyImplyLeading property of the AppBar. This will make Flutter display the back button when there is a previous page in the navigation stack.
Here is a simplified approach:
Create an app with a static app bar that includes a back button, You can use a WillPopScope to handle back navigation and customize your app bar to reflect the current page.
Now, you can create a main widget that uses the CustomAppBar. here we set up the navigation logic.
Create another class UserInfoPage
In the UserInfoPage, you can again use the CustomAppBar, but this time, you will pass the onBackPressed callback to handle navigation back to the HomePage.
Now run your app. You’ll see a static app bar on both pages. Clicking the button will take you to the UserInfoPage, with a back button to return to the HomePage.