Keeping nested pages persistent inside a flutter app - flutter

I am currently working on an app using the flutter framework I have the main.dart file loading the homefeed page with bottom navigation bar with two tabs one for home and one for settings... The home.dart file is loading the body.dart whereas the body.dart file is loading list.dart and the problem occurs when I go to the settings tab and go back to the home tab the home page gets rebuilt. how can I keep all pages persistent?
To give you a basic idea:
home.dart:
Scaffold(
....
body: Body();
...
);
body.dart:
Scaffold(
...
body: List();
...
);

You can store necessary tabs state in the Home widget. When building tabs, pass appropriate state through constructor. When user switch tab, the state will persist because it's stored inside root widget.
If you'll need to pass the state to number of nested levels, consider using some packages like scoped_model, flutter_bloc or flutter_redux

Related

Flutter beamer routing package causes rebuilding the previous screen when using beamBack method

I am using beamer routing package for my flutter project. When calling Beamer.of(context).beamBack();, it causes rebuilding the previous page in the navigation stack.
For example, there are two screens, ScreenA and ScreenB. Initially navigated from ScreenA to ScreenB, then using beamBack method from ScreenB causes ScreenA rebuilds. How can i avoid rebuilding?

Using bottom navigation with individual Navigators but single AppBar

I am trying to create an app that has the following basic setup for navigation:
tab1 -> subpage
tab2
tab3
with a bottom navigation bar for switching between the tabs and each tab having its own navigation stack. Furthermore I would like to add an AppBar with a drawer (as endDrawer) and the back button as leading widget in the AppBar.
The AppBar should be centrally managed so I put it in the main page of the app for maintenance purposes instead of having it on every page that is pushed. The drawer should also be updated later on when the user is logging in or out so it encouraged me more to place the AppBar only in the main page.
Now the issue is that I would like to update the AppBar to contain the back button when the app navigated to the subpage of tab1. Since the AppBar is in the parent I thought of RouteObservers as a possible solution by registering a RouteObserver on each NavigationState.
Yet I cannot get it to work because when I would like to subscribe on the RouteObserver in the HomePage in the didChangeDependencies() the currentState of the NavigatorState does not exist. Also I tried subscribing in the build method after the Scaffold with a flag for subscribing only once, but this didn't work either.
I created a github project with the basics of my current implementation:
https://github.com/S-Braeutigam/bottom-navbar-single-appbar
To get to the current state of code I used the following links:
https://codewithandrea.com/articles/multiple-navigators-bottom-navigation-bar/
https://api.flutter.dev/flutter/widgets/RouteObserver-class.html
Flutter 2.0 appbar back button disappeared if contains endDrawer (The solution gets close to what I would like to implement, but the AppBar should always be visible, not only on the sub page)
My main questions are:
Are RouteObservers the correct approach on how to track the navigation to subpages of tabs and display the back button based on it?
At what lifecycle hook in the main page should or could the subscription on the currentState of each NavigatorState be done? Wasn't able to find any where they are not null.

Flutter subclass Scaffold so I can ensure a Drawer and BottomNavigationBar are always present

I have a Flutter app with sections that I can access from both a BottomNavigationBar and a Drawer. I have this working in a Scaffold at the root level of my app, and I then use Navigator widgets to create separate routes and stacks within each section. The BottomNavigationBar persists as I push new screens onto the stack, and these new screens will have their own AppBar and actions specific to those user journeys.
The problem is that I need the Drawer to be present on all of my screens, and setting a new AppBar in a Scaffold on individual screens removes the Drawer that I had set in my root level Scaffold.
How can I pass that Drawer through to all my screen AppBar instances? I don't want to create new Drawer instances over multiple widgets, so I should create a Singleton of the Drawer or even the entire Scaffold? What's the best practise here?
You can use a state management package. I suggest use provider packages or flutter_bloc so that you can get more out of flutter UI but be careful about their name and here is a doc link that is made by flutter team and will help you with all the state management package:
https://flutter.dev/docs/development/data-and-backend/state-mgmt/options
This one is for bloc_flutter doc I couldn't find it on the previous link document: https://bloclibrary.dev

How to route to page, without removing BottomNavigationBar in flutter?

I'm creating my first app in flutter, and i'm having issues with state management. I have a MainPage.dart with BottomNavigationBar, and body: with tabPages[MainTab, ...]. In the MainTab i have a ListView, and when i click ListView item, it should open details, however BottomNavigationBar shouldn't be removed.
Below i have a design.(I'm using Scoped Model).
I tried these solutions
https://gist.github.com/HansMuller/0e76c54b1f2d4423efbdc2c185e761ef and How to route to page, without removing BottomNavigationBar in flutter?
But in those cases, i can't route to page without bottom navigation. When i click on FLoatingActionButton: it should open new page without bottom navigation (BottomNavigationBar stay always at the bottom, even if i don't want it)
https://medium.com/#daniyargilimov/lutter-bottomnavigationbar-with-multiple-navigators-725ff013489c
here is the working example...

designing an app with multiple pages with different app bar and a single drawer

I am trying to build an app in Flutter in which I have multiple pages, each with their specific actions in the app bar. I would like to add a Drawer to the app which contains a list of page names that take them to the respective pages. I learnt that both AppBar and Drawer widgets have to be part of a Scaffold widget. Currently, all my pages are basically StatefulWidgets with the build method returning a complete Scaffold widget. If I want to include a Drawer, I think I will have to add the drawer object in each of my page. Is there a better way/pattern to do this?
Write the code for the drawer once and enclose it in a function. Then just call the function each time for that drawer.
eg)
drawer: myDrawer()
In one of my Apps, I have something similar. My Drawer is in the app class which is called in the main. This app class calls the home classes. My app class is also Stateful.
Each home class has his own Appbar and Search function.
The Drawer is called only in the app class.
Tell me if this explanation is okay for you or if you need a sample code.