gpt4 book ai didi

dart - Flutter 更新 BottomNavigationBar

转载 作者:IT王子 更新时间:2023-10-29 06:48:28 24 4
gpt4 key购买 nike

我将 BottomNavigationBar 与 TabController 一起使用。通过单击 BottomNavigationBar 的不同选项卡,TabView 正在更改内容。但是,如果我在 TabView 上滑动以切换到另一个 View /选项卡,则 BottomNavigationBar 不会更新到我滑动到的选项卡。我已经向 TabController 添加了一个监听器来检测更改。但是我如何以编程方式更新 BottomNavigationBar 以反射(reflect)更改?

最佳答案

我认为使用 PageView 更优雅而不是 TabBarView 专门针对您的情况。

enter image description here

class BottomBarExample extends StatefulWidget {
@override
_BottomBarExampleState createState() => new _BottomBarExampleState();
}

class _BottomBarExampleState extends State<BottomBarExample> {
int _page = 0;
PageController _c;
@override
void initState(){
_c = new PageController(
initialPage: _page,
);
super.initState();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
bottomNavigationBar: new BottomNavigationBar(
currentIndex: _page,
onTap: (index){
this._c.animateToPage(index,duration: const Duration(milliseconds: 500),curve: Curves.easeInOut);
},
items: <BottomNavigationBarItem>[
new BottomNavigationBarItem(icon: new Icon(Icons.supervised_user_circle), title: new Text("Users")),
new BottomNavigationBarItem(icon: new Icon(Icons.notifications), title: new Text("Alerts")),
new BottomNavigationBarItem(icon: new Icon(Icons.email), title: new Text("Inbox")),

],

),
body: new PageView(
controller: _c,
onPageChanged: (newPage){
setState((){
this._page=newPage;
});
},
children: <Widget>[
new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Icon(Icons.supervised_user_circle),
new Text("Users")
],
),
),
new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Icon(Icons.notifications),
new Text("Alerts")
],
),
),
new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Icon(Icons.mail),
new Text("Inbox")
],
),
),
],
),
);
}
}

关于dart - Flutter 更新 BottomNavigationBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49781227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com