Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to persist state between tabs (TabBar) in Flutter?

I have a TabBarView with 3 TabBar. When I'm in tab 1 I do something, then I navigate to tab 2 when I come back to tab 1 I want to the previous state of tab 1 will not change.

How can I achieve this in Flutter?

Below is my code screenshot

class _LandingPageState extends State<LandingPage> with SingleTickerProviderStateMixin {
  int _selectedIndex = 0;
  PageController pageController;
  TabController tabController;

  @override
  void initState() {
    tabController = TabController(length: 3, vsync: this, initialIndex: 0);
    pageController = PageController(initialPage: 0)
      ..addListener(() {
        setState(() {
          _selectedIndex = pageController.page.floor();
        });
      });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
              bottomNavigationBar: BottomNavigationBar(
              currentIndex: _selectedIndex,
              onTap: (index) {
                setState(() {
                  _selectedIndex = index;
                  tabController.animateTo(index,
                      duration: Duration(microseconds: 300),
                      curve: Curves.bounceIn);
                });
              },
              items: [
                BottomNavigationBarItem(
                    icon: Icon(Icons.assignment), title: Text("Các yêu cầu")),
                BottomNavigationBarItem(
                    icon: Icon(Icons.history), title: Text("Lịch sử")),
                BottomNavigationBarItem(
                    icon: Icon(Icons.person), title: Text("Hồ sơ")),
              ]),
          body: TabBarView(
              controller: tabController,
              children: [
                RequestPage(key: PageStorageKey<String>("request_page"),),
                HistoryPage(key: PageStorageKey<String>("history_page")),
                ProfilePage(key: PageStorageKey<String>("profile_page"))])
          ),
    );
  }

enter image description here

like image 772
Hoang Trung Nguyen Avatar asked Apr 23 '20 09:04

Hoang Trung Nguyen


People also ask

How do you save the state of tabs in Flutter?

In case you want to keep the state of your screen in your TabBarView , you can use the mixin class called AutomaticKeepAliveClientMixin in your State class. After that you have to override the wantKeepAlive method and return true . this should be the suggested answer!

Can we use TabBar without AppBar in Flutter?

TabBar with labels without AppBar Instead, we can use PreferredSize widget. Since TabBar has preferredSize , we should use it to decide the height. Yes!

How do you use TabController in Flutter?

To create a tab in it, create a tab list and create an object of its TabController. TabController _tabController; Initalize the TabController inside the initState() method and also override it in the dispose() method.

How to create a Tabbar in flutter?

Here’s the minimal code to get TabBar up and running: To implement TabBar in your Flutter app, complete the following steps: Wrap the Scaffold widget inside the DefaultTabController. This should be used for most simple use cases. If you want to control the tabs programmatically, you should use TabController and avoid this step

How to create a tabcontroller in WordPress?

Either create a TabController manually, or automatically by using a DefaultTabController widget. Using DefaultTabController is the simplest option, since it creates a TabController and makes it available to all descendant widgets. 2. Create the tabs When a tab is selected, it needs to display content. You can create tabs using the TabBar widget.

How to create tabs in an appbar?

Using DefaultTabController is the simplest option, since it creates a TabController and makes it available to all descendant widgets. 2. Create the tabs When a tab is selected, it needs to display content. You can create tabs using the TabBar widget. In this example, create a TabBar with three Tab widgets and place it within an AppBar.

How to move to the next page using tabcontroller?

Use the TabController to move to the next page with the click of a button: You may want to perform some operations when a specific tab is open. This callback comes in handy when you want to perform initialize something again when a particular tab is open or destroy something when tab is not open.


2 Answers

Make sure that all of your TabBarView children are StatefulWidgets and then add a AutomaticKeepAliveClientMixin like so in all of them, for example, for your RequestPage, it should look like this:

class RequestPage extends StatefulWidget {
  RequestPage({Key key}) : super(key: key);

  @override
  _RequestPageState createState() => _RequestPageState();
}

class _RequestPageState extends State<RequestPage> with AutomaticKeepAliveClientMixin{
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return // Your widget tree
  }

  @override
  bool get wantKeepAlive => true;
}
like image 54
Miguel Ruivo Avatar answered Oct 07 '22 09:10

Miguel Ruivo


Try AutomaticKeepAliveClientMixin and override wantKeepAlive to always return true

like image 1
Kamil Poniewierski Avatar answered Oct 07 '22 10:10

Kamil Poniewierski