我有 3 个屏幕-home和. 我想要一个and总是出现在我所有的屏幕上。现在,我在文件中定义了and 。每当点击一个选项时,正文就会替换为所选屏幕的内容。friendsprofileAppBarBottomNavigationBarAppBarBottomNavigationBarScaffoldmain.dartBottomNavigationBarScaffold
主要飞镖:
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: SafeArea(
child: Container(
child: _pageOptions[_selectedPage],
),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _selectedPage,
onTap: (int index) {
setState(() {
_selectedPage = index;
});
},
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home), title: Text('Home')),
BottomNavigationBarItem(
icon: Icon(Icons.people), title: Text('Friends')),
BottomNavigationBarItem(
icon: Icon(Icons.person), title: Text('Profile'))
],
),
),
routes: <String, WidgetBuilder>{
'/home': (BuildContext context) => Home(),
'/friends': (BuildContext context) => Friends(),
},
);
}
现在,我有一个在屏幕avatar中重复使用的自定义小部件。home单击 时avatar,我想显示friends屏幕。onTap我在以下方法中尝试了此代码avatar:
{Navigator.pushNamed(context, '/friends')},
朋友飞镖:
Widget build(BuildContext context) {
return Container(
child: Text('Friends'),
);
}
在点击avatar时,我正在获取friends屏幕,但其他所有内容都丢失了(AppBar等BottomNavigationBar)。
我如何实现这一目标?我应该从我的所有 3 个屏幕返回一个带有 等AppBar的脚手架吗?BottomNavigationBar这会对性能产生负面影响吗?Scaffold或者有没有办法通过替换in的主体来做到这一点main.dart?