How to maintain state of pages in TabBarView in Flutter? - flutter

I have used TabBarView as my page holder for BottomNavigationBar instead of Indexed stack. But this does refresh the page every time tab is changed. The state is not maintained. What to do?

Using AutomaticKeepAliveClientMixin flutter.dev
class _SampleScaffoldSate extends State<SampleScaffold> with AutomaticKeepAliveClientMixin {
// ... other code
#override
bool get wantKeepAlive => true;
}

Related

Does BottomNavigationBar require IndexedStack widgets? If so, they load all at once, which is annoying

I want a BottomNavigationBar
I currently do this, and to prevent the BottomNavigationBar from reloading each time when going to a different screen, I use IndexedStack widgets
Because I used IndexedStacked widgets, whenever the app starts, it runs all the widgets in the entire app which is annoying. I only want a widget to run if I am on that screen.
How can I modify/fix this where I can use the BottomNavigationBar but not have this IndexedStack widgets running all at once
you can try another approach to keep state in a page:
// 1. AutomaticKeepAliveClientMixin
class _PageState extends State<_HomePage> with AutomaticKeepAliveClientMixin<_HomePage> {
// 2.
#override
bool get wantKeepAlive => true;
#override
Widget build(BuildContext context) {
//3.
super.build(context);
return Scaffold(...
);
}
}

How to pass a number (through clicking Button "OnPressed property) from a stful widget (First screen on the app) to ListView.builder at Second Page

I got an issue. I am at the opening page of my app, which is stful widget. In the middle of the screen is a button, and a container below. In the container appears random numbers every some time. How can I solve the problem, where I want to create ListView.builder at the Second Page, which is also stful widget. I need him to be stful. I want to create ListView according to the numbers at the first page, by clicking the button and sending data to the Listview.builder.
I need a simple solution how to manage this case. All my trails failed.
It would be fantastic for a help!
as I understand you should keep one int array and set random numbers into the array, then on the second page, you need a constructor to take data, then in init state set them. The second page should be as below example.
class SecondPage extends StatefulWidget {
final List<int> data;
SecondPage(this.data);
#override
_SecondPageState createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {
List<int> _list = List<int>();
#override
void initState() {
_list = widget.data;
super.initState();
}

how to keep the nested navigator stack history when swiping to another stack in Flutter

I have made a TabBarView with a TabBar and each tab has its own navigator stack
when navigating through the navigator stack of the first tab and at some point swiping to another tab
and swiping back to the previous tab, the previous tab navigator stack has reset like it has build again
how to handle this situation
I want each tab navigator stack to hold its history when swiping to another tabs
I've figured out the solution and it is simply just wrap the nested Navigator with a stateful widget and use the state of the stateful widget with the mixin AutomaticKeepAliveClientMixin and implement the (must be implemented method)
#override
bool get wantKeepAlive => true;
as the following example
class NotificationsNavigator extends StatefulWidget {
#override
_NotificationsNavigatorState createState() => _NotificationsNavigatorState();
}
class _NotificationsNavigatorState extends State<NotificationsNavigator> with
AutomaticKeepAliveClientMixin {
#override
bool get wantKeepAlive => true;
#override
Widget build(BuildContext context) {
return new Navigator(
key: someKey,
onGenerateRoute: notificationsOnGenerateRoutes,
initialRoute: '/',
);
}
}

How to keep checkbox state while browsing between navigation bar items

In Flutter, I have a bottom navigator bar and there are 4 different bottom navigator bar items in it. In the 4th menu item, I have a checkbox widget. While browsing in the navigation bar items I would like the checked or unchecked items situation remains. For example if I uncheck this widget I would like it to stay still "unchecked" while browsing in other navigation bar items. How can I do this? I tried to use key parameter of checkbox but I failed.
You should use AutomaticKeepAliveClientMixin
to keep state Alive while you browse between your items.Extend your state class with it and create an override method wannaKeepAlive and set it to true.Here is an example of a widget using AutomaticKeepAliveClientMixin.
class Example extends StatefulWidget {
#override
_ExampleState createState() => _ExampleState();
}
class _ExampleState extends State<Example> with AutomaticKeepAliveClientMixin {
#override
Widget build(BuildContext context) {
return Container(
);
}
#override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
}
Welcome to State management!
Official guideline for state management from Flutter team.
I would recommend to read everything starting from introduction to state management.

How do I preserve the data in my textfield when navigating to another tabview

How do I preserve my data when I navigate between two different tabs in Flutter... Basically what I want to achieve is, on one side of the tab, I see users data, and on the other side of the tab, I want to be able to copy some of those user data and paste into some textfields on the other tab without losing the data in the text fields when I navigate back and forth and still also preserving the level of scrolling I might have done on the tab where the users data show.
You need to use AutomaticKeepAliveClientMixin This will ensure the State instance is not destroyed when leaving the screen.Extend your every tabview's state class with it and create an override method wannaKeepAlive and set it to true.Here is an example of a widget using AutomaticKeepAliveClientMixin.
class Example extends StatefulWidget {
#override
_ExampleState createState() => _ExampleState();
}
class _ExampleState extends State<Example> with AutomaticKeepAliveClientMixin {
#override
Widget build(BuildContext context) {
return Container(
);
}
#override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
}