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

后端 未结 5 1843
北荒
北荒 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条回答
  •  -上瘾入骨i
    2020-12-12 15:57

    You can use TabController:

    TabController _controller = TabController(
      vsync: this,
      length: 3,
      initialIndex: 0,
    );
    
    _controller.animateTo(_currentTabIndex);
    
    return Scaffold(
      appBar: AppBar(
        bottom: TabBar(
          controller: _controller,
          tabs: [
            ...
          ],
        ),
      ),
      body: TabBarView(
        controller: _controller,
        children: [
          ...
        ],
      ),
    );
    

    And than, setState to update screen:

    int _currentTabIndex = 0;
    
    setState(() {
      _currentTabIndex = 1;
    });
    

提交回复
热议问题