Flutter DefaultTabController unexpectedly initializing widget during animation when navigating from tab 0 to tab 2

北战南征 提交于 2019-12-24 07:13:43

问题


I am using a DefaultTabController to manage 3 tabs. The default tab controller is set up like this:

DefaultTabController(
        length: 3,
        initialIndex: 1,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Material(
              type: MaterialType.canvas,
              elevation: 1.0,
              child: Container(
                color: Color(0xFFe3f2fd),
                child: TabBar(tabs: [Tab(text: 'ACTIVITY'), Tab(text: 'LEADERBOARD',) ,Tab(text: 'SETTINGS',)
                  ],
                  labelColor: Theme.of(context).primaryColor,
                  indicatorColor: Theme.of(context).primaryColor,
                  labelPadding: EdgeInsets.all(0.0),
                  labelStyle: TextStyle(fontWeight: FontWeight.bold, fontFamily: 'Montserrat'),
                  indicatorPadding: EdgeInsets.symmetric(horizontal: 20.0),
                ),
              ),
            ),
            Expanded(
              child: Container(
                child: TabBarView(
                    children: [
                      ActivityTab(),
                      LeaderBoardTab(),
                      SettingsTab(rebuildLeaderBoard: refreshLeaderBoard,),
                    ]),
              ),
            )
          ],
        ),
      );

I am experiencing setState errors when navigating from tab 0 to 2, or 2 to 0. Through testing, I've identified two unexpected behaviors:

1) When navigating from ActivityTab() to SettingsTab() (so from 0 to 2) LeaderboardTab() is being initialized and then disposed. This is causing a setState error.

2) During the same navigation, I'm finding that when leaving a tab, the first thing to occur is that the tab I'm leaving is initialized, causing another setState error.

This odd functionality doesn't occur when only moving 1 tab left or right. It only occurs when moving over two tabs. I found some clues here: https://github.com/flutter/flutter/issues/13848 but this discussion seems to address a swiping vs. tapping behavior issue that I am not experiencing.

I rebuilt just this segment of the project in a new project and put print statements in the initState and dispose. Here is what gets spit out when moving from tab 0 to tab 2.

flutter: INIT TAB ZERO
flutter: DISPOSE TAB ZERO
flutter: INIT TAB TWO
flutter: INIT TAB ONE
flutter: DISPOSE TAB ZERO
flutter: DISPOSE TAB ONE

来源:https://stackoverflow.com/questions/55576685/flutter-defaulttabcontroller-unexpectedly-initializing-widget-during-animation-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!