gpt4 book ai didi

Flutter 在一个页面上自定义 AppBar

转载 作者:IT王子 更新时间:2023-10-29 06:56:16 25 4
gpt4 key购买 nike

我正在使用 AppBarBottomNavigationBar 开发 Flutter 应用程序。这是代码:

class MainPage extends StatefulWidget {
final MainModel model;

MainPage(this.model);

@override
State<StatefulWidget> createState() {
return _MainPageState();
}
}

class _MainPageState extends State<MainPage> {
int _selectedIndex = 0;

Widget _openSelectedPage() {
switch(_selectedIndex) {
case 0:
return HomePage(widget.model);
case 1:
return CapturePage(widget.model);
case 2:
return MealsPage(widget.model);
default:
return HomePage(widget.model);
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My App'),
),
body: _openSelectedPage(),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.camera_alt),
title: Text('Capture'),
),
BottomNavigationBarItem(
icon: Icon(Icons.format_list_bulleted),
title: Text('Meals'),
),
],
currentIndex: _selectedIndex,
onTap: (int index) {
setState(() {
_selectedIndex = index;
});
}
),
);
}
}

如何在 MealsPage 上自定义 AppBar?如果我在 MealsPagebuild 方法中添加另一个 Scaffold,那么它会创建两个 AppBar,这不是什么我想。我只想拥有一个 AppBar,即我在 MealsPage 中定义的那个。

最佳答案

class MainPage extends StatefulWidget {
final MainModel model;

MainPage(this.model);

@override
State<StatefulWidget> createState() {
return _MainPageState();
}
}

class _MainPageState extends State<MainPage> {
int _selectedIndex = 0;

Widget _openSelectedPage() {
switch (_selectedIndex) {
case 0:
return HomePage(widget.model);
case 1:
return CapturePage(widget.model);
case 2:
return MealsPage(widget.model);
default:
return HomePage(widget.model);
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: _selectedIndex == 2 ? null : AppBar(title: Text('My App')),
body: _openSelectedPage(),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.camera_alt),
title: Text('Capture'),
),
BottomNavigationBarItem(
icon: Icon(Icons.format_list_bulleted),
title: Text('Meals'),
),
],
currentIndex: _selectedIndex,
onTap: (int index) {
setState(() {
_selectedIndex = index;
});
},
),
);
}
}

关于Flutter 在一个页面上自定义 AppBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56892535/

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