Flutter:如何在不单击图标的情况下更改导航栏的视图?
我使用经典的 navigationRail ,在 row()两分裂的内部来更改视图。但是我也想通过单击视图中显示的项目来更改视图。 例如,在开始时 showtables()是可见的,当我单击 showtables()构建的按钮时,我想替换 showtables()由其他小部件。这个新的小部件可能有可能返回 showtables(),保持我的地图表的价值(或者至少我的应用程序状态 data 如果不可能)。我该怎么办?
class NavBarTest extends StatefulWidget {
final Metadata? data;
const NavBarTest({Key? key, required this.data}) : super(key: key);
@override
State<NavBarTest> createState() => _NavBarTestState();
}
class _NavBarTestState extends State<NavBarTest> {
Map tables = {};
int _selectedIndex = 0;
@override
void initState() {
/*
here I init tables...
*/
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child:
Row(
children: <Widget>[
NavigationRail(
groupAlignment: -1.0,
selectedIndex: _selectedIndex,
leading:
Column(children: [
SizedBox(
height: MediaQuery.of(context).size.height/3,
),
SizedBox(
width: 50,
height: 50,
child: Image.asset("assets/logo.png"),
),
],),
onDestinationSelected: (int index) {
setState(() {
_selectedIndex = index;
});
},
labelType: NavigationRailLabelType.none,
destinations: const <NavigationRailDestination>[
NavigationRailDestination(
icon: Icon(Icons.spoke_rounded,color: icon),
selectedIcon: Icon(Icons.spoke_rounded,color: Colors.yellow),
label: Text('First'),
),
NavigationRailDestination(
icon: Icon(Icons.show_chart_sharp,color: icon),
selectedIcon: Icon(Icons.show_chart_sharp,color: Colors.yellow),
label: Text('Second'),
),
NavigationRailDestination(
icon: Icon(Icons.info_sharp,color: icon),
selectedIcon: Icon(Icons.info_outline,color: Colors.yellow),
label: Text('Third'),
),
],
),
// This is the main content.
Expanded(
child: buildPage(_selectedIndex)
)
],
)
)
);
}
Widget buildPage(index){
switch (index) {
case 0:
return ShowTables(tables: tables);
case 1:
return const Details();
case 2:
return Links(tables: tables);
default:
return ShowTables(tables: tables);
}
}
}
I use a classic NavigationRail, inside a Row() divided in two, to change the view. But I would also like to change the view by clicking on items displayed in the view.
For example, at the beginning ShowTables() is visible, and when I click on buttons built by ShowTables(), I want to replace ShowTables() by an other widget. This new widget could have the possibility to return to ShowTables(), keeping the value of my Map Tables (or at least my app state data if not possible). How can I do ?
class NavBarTest extends StatefulWidget {
final Metadata? data;
const NavBarTest({Key? key, required this.data}) : super(key: key);
@override
State<NavBarTest> createState() => _NavBarTestState();
}
class _NavBarTestState extends State<NavBarTest> {
Map tables = {};
int _selectedIndex = 0;
@override
void initState() {
/*
here I init tables...
*/
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child:
Row(
children: <Widget>[
NavigationRail(
groupAlignment: -1.0,
selectedIndex: _selectedIndex,
leading:
Column(children: [
SizedBox(
height: MediaQuery.of(context).size.height/3,
),
SizedBox(
width: 50,
height: 50,
child: Image.asset("assets/logo.png"),
),
],),
onDestinationSelected: (int index) {
setState(() {
_selectedIndex = index;
});
},
labelType: NavigationRailLabelType.none,
destinations: const <NavigationRailDestination>[
NavigationRailDestination(
icon: Icon(Icons.spoke_rounded,color: icon),
selectedIcon: Icon(Icons.spoke_rounded,color: Colors.yellow),
label: Text('First'),
),
NavigationRailDestination(
icon: Icon(Icons.show_chart_sharp,color: icon),
selectedIcon: Icon(Icons.show_chart_sharp,color: Colors.yellow),
label: Text('Second'),
),
NavigationRailDestination(
icon: Icon(Icons.info_sharp,color: icon),
selectedIcon: Icon(Icons.info_outline,color: Colors.yellow),
label: Text('Third'),
),
],
),
// This is the main content.
Expanded(
child: buildPage(_selectedIndex)
)
],
)
)
);
}
Widget buildPage(index){
switch (index) {
case 0:
return ShowTables(tables: tables);
case 1:
return const Details();
case 2:
return Links(tables: tables);
default:
return ShowTables(tables: tables);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论