[英]How to Navigate to parent of widget in flutter
我想从其他页面导航到颤动页面上的第二个底部项目。
这里的父级是我想要配置以更改页面的 BottomNavigationBar(我在 Provider 中为它定义了一个键以从其他页面访问它)。
List<Widget> pages = new List<Widget>();
BottomNavigationBar bottomNavigationBar;
updateList(){
pages.addAll({
Dashboard(),
AllServices(),
Account(),
Charge(),
});
}
int _selectedIndex = 0;
Widget _bottomNavigationBar(int selectedIndex,key) {
return BottomNavigationBar(
key: key,
onTap: (int index) => setState(() => _selectedIndex = index),
currentIndex: selectedIndex,
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: new Icon(Icons.home),
title: new Text("A page"),
),
BottomNavigationBarItem(
icon: new Icon(Icons.location_on),
title: new Text("B page")
),
BottomNavigationBarItem(
icon: new Icon(Icons.store),
title: new Text("C page")
),
BottomNavigationBarItem(
icon: new Icon(Icons.person),
title:new Text("D page")
),
],
);
}
@override
void initState() {
// TODO: implement initState
super.initState();
updateList();
}
@override
Widget build(BuildContext context) {
final globalKeyForBottomNavigate = Provider.of<Controller>(context, listen: false).bottomNavigate;
return Scaffold(
bottomNavigationBar: _bottomNavigationBar(_selectedIndex,globalKeyForBottomNavigate),
body: pages[_selectedIndex],
);
}
和孩子是页面A,B,C,D。现在从页面AI导航到页面F。然后如果触发了一个动作,我想导航到页面B,但我的解决方案是更改页面F中的BottomNavigationBar的索引和然后导航到页面A。它可以工作,但不会直接导航到页面B和BottomNavigationBar的内容。 在我的解决方案中,我说好的,首先更改BottomNavigationBar的索引,然后转到最后一页(这里是A页),但是如您所见,我不知道如何直接导航到B页。这是F页的代码.
onTap: (){
var bottomKey=Provider.of<Controller>(context, listen: false).bottomNavigate;
final BottomNavigationBar navigationBar = bottomKey.currentWidget;
navigationBar.onTap(1);
Navigator.pop(context);
},
您可以尝试在应用程序中使用环境变量。 并根据操作更改环境变量的值。 其次,将 selectedIndex 的值与该环境变量绑定,因此每当该变量发生变化时,它都会更改 selectedIndex 的值并使用新的索引值再次调用仪表板页面,它将从底部栏导航到所需的页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.