【发布时间】: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