【问题标题】:How to shift tabs with a CupertinoTabBar ? Unlike the material tab bar we do not have tab controller to switch tabs如何使用 CupertinoTabBar 移动标签?与材质标签栏不同,我们没有标签控制器来切换标签
【发布时间】:2019-06-14 03:09:39
【问题描述】:

我在主页选项卡上,我需要使用 CupertinoTabBar 切换到我的第二个选项卡。我没有 tabcontroller 可以使用材质选项卡栏之类的全局键进行切换。你能建议选择吗?

我尝试使用全局键并更改选项卡的 currentIndex。然而,没有运气。已经没有选项了。

HomeScreen.dart - 包含标签

```Widget build(BuildContext context) {
    return new CupertinoTabScaffold(
      tabBar: CupertinoTabBar(
        key: HomeScreen._myTabbedPageKey,
        backgroundColor: Colors.black,
        currentIndex: _currentIndex,
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
              icon: new Icon(Icons.home, color: Colors.white),
              activeIcon: new Icon(Icons.home,
                  color: Color.fromARGB(198, 39, 169, 227)),
              title: new Text(
                'Home',
                style: TextStyle(color: Color.fromARGB(198, 39, 169, 227)),
              )),
          BottomNavigationBarItem(
              icon: new Icon(Icons.track_changes, color: Colors.white),
              activeIcon: new Icon(Icons.track_changes,
                  color: Color.fromARGB(198, 39, 169, 227)),
              title: new Text(
                'Trips',
                style: TextStyle(color: Color.fromARGB(198, 39, 169, 227)),
              )),
          BottomNavigationBarItem(
              icon: new Icon(Icons.traffic, color: Colors.white),
              activeIcon: new Icon(Icons.traffic,
                  color: Color.fromARGB(198, 39, 169, 227)),
              title: new Text('Track',
                  style: TextStyle(color: Color.fromARGB(198, 39, 169, 227)))),
          BottomNavigationBarItem(
              icon: new Icon(Icons.settings, color: Colors.white),
              activeIcon: new Icon(Icons.settings,
                  color: Color.fromARGB(198, 39, 169, 227)),
              title: new Text('Settings',
                  style: TextStyle(color: Color.fromARGB(198, 39, 169, 227))))
        ],
      ),
      tabBuilder: (BuildContext context, int index) {
        if (_currentIndex != -1) {
          _currentIndex = index;
          return _children[_currentIndex];
        } else {
          return _children[index];
        }
      },
    );
  }```

HomeTab.dart - 包含“查看更多行程”按钮。点击按钮应该带我到第二个标签

 Widget build(BuildContext context) {
     return new CupertinoPageScaffold(
         backgroundColor: Colors.white,
         navigationBar: new CupertinoNavigationBar(
           middle: title,
           backgroundColor: Colors.black,
           automaticallyImplyLeading: false,
         ),
         child: new RefreshIndicator(
             key: _homeRefreshIndicatorKey,
             onRefresh: _refresh,
             child: new SingleChildScrollView(
               child: new Container(
                 child: new Center(
                   child: new Column(
                     // center the children
                     mainAxisAlignment: MainAxisAlignment.start,
                     children: <Widget>[
                       CupertinoButton(
                           color: Color.fromARGB(198, 39, 169, 227),
                           disabledColor: Colors.grey,
                           child: Text('SEE MORE TRIPS',
                               style: TextStyle(
                                   fontSize: 12,
                                   color: Colors.white,
                                   fontFamily: 'Lato')),
                           onPressed: () {
                             //to call second tab
                           }),
                       new SizedBox(
                         height: 16,
                       )
                     ],
                   ),
                 ),
               ),
             )));
 }```

To be navigated to second tab




【问题讨论】:

  • 也许你应该在更新 selectedIndex 时使用setState 调用?因为根据文档,您所要做的就是更改 currentIndex 变量以使其正确更新。你试过了吗?
  • @George - 我无法引用全局键来更改我的主页选项卡中当前索引的值。 HomeScreen 包含 CupertinoTabBar。而且,我无法在 HomeTab 中获取全局键的引用来使用 setState() 方法更改索引。
  • @George - HomeScreenState.of(context).state(); - 我已经包含了这一行,我可以将标签更改为第二个位置。但是,这只适用于我的第一次点击。我的第二次点击被调用,但是,它不起作用。名称为 state() 的函数的 PFB 详细信息。 void state(){ setState((){ _currentIndex = 1; print("Current index : " + _currentIndex.toString()); }); }
  • 遇到同样的问题。有解决办法吗?
  • @JoelHernandez - 仍然没有找到解决方案。使用上述 cmets,它会间歇性地工作。我们应该提出反对颤振回购的案例吗?

标签: dart flutter cupertinotabbar


【解决方案1】:

您应该使用CupertinoTabController 按预期更改标签栏索引,将CupertinoTabController 的实例传递给controllerCupertinoTabScaffold 属性。

最后,以编程方式更改索引。

setState(() {
     _tabController.index = 0;
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2020-07-24
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    相关资源
    最近更新 更多