Flutter: Changing the current tab in tab bar view using a button

后端 未结 5 1851
北荒
北荒 2020-12-12 15:24

I am creating an app that contains a tab bar on its homepage. I want to be able to navigate to one of the tabs using my FloatingActio

5条回答
  •  清歌不尽
    2020-12-12 15:54

    chemamolin's answer above is correct, but for additional clarification/tip, if you want to call your tabcontroller "from anywhere", also make sure the tabcontroller is not a private property of the class by removing the underscore, otherwise the distant class will not be able to see the tabcontroller with the example provided even when using the GlobalKey.

    In other words, change

    TabController _tabController;
    

    to:

    TabController tabController;
    

    and change

    MyApp._myTabbedPageKey.currentState._tabController.animateTo(...);
    

    to:

    MyApp._myTabbedPageKey.currentState.tabController.animateTo(...);
    

    and everywhere else you reference tabcontroller.

提交回复
热议问题