在Flutter中导航时如何更改默认选项卡? [英] How to change the default tab while navigating in Flutter?

查看:940
本文介绍了在Flutter中导航时如何更改默认选项卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在TabBar页面上有以下代码:

I have the following code for the TabBar page:

class HomePage extends StatefulWidget {


  static String tag = 'home-page';

  @override
  _homepage createState() => new _homepage();
}

class _homepage extends State<HomePage> with TickerProviderStateMixin{


  AnimationController percentageAnimationController;
  TabController _tabController;


  @override
  void initState() {
    _tabController = new TabController(length: 3, vsync: this);


    super.initState();
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {

    return new Scaffold(

        bottomNavigationBar: new Material(
          color: Colors.white,
          child: new TabBar(
              controller: _tabController,
              indicatorColor: Theme.Colors.loginGradientStart,
              labelColor: Theme.Colors.loginGradientStart,
              tabs: <Widget>[
                new Tab(
                  icon: new Icon(wind_icon),
                ),
                new Tab(
                  icon: new Icon(chart_icon),
                ),
                new Tab(
                  icon: new Icon(settings_icon),
                ),
              ]
          ),
        ),
        body:
        new TabBarView(
          children: <Widget>[

            TabOne(),

            TabTwo(),

            TabThree(),

            ],
          controller: _tabController,
        ),
    );
  }
}

现在,如果我导航至此页面,它将自动打开第一个标签,但我想打开第二个标签,而不是第一个标签(即标签索引1).

Now if I navigate to this page it automatically opens the first tab but I want to open the second tab instead of the first i.e. tab index 1.

我能够发现我们可以通过使用 _tabController.animateTo(1); 但是我想知道如何通过其他页面的按钮操作来做到这一点.

I am able to find out that we can achieve this by using _tabController.animateTo(1); but I want to know how can I do this from button press of other pages.

推荐答案

您可以使用初始索引:

_tabController = new TabController(length: 3, vsync: this, initialIndex: 1);

这篇关于在Flutter中导航时如何更改默认选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆