flutter - Flutter/Dart - Navigator.pop(context) 在添加 BottomNavigationBar 后停止工作
问题描述
我的堆栈中的第一页有一个BottomNavigationBar
. 第一个选项卡调用 a FutureBuilder
,它返回 a PageView.builder
。滑动它导航到第二页。
以前,点击Navigator.pop(context)
第二页让我回到第一页。但是自从向第二页添加了一个 BottomNavigationBar 后,该按钮不再弹回。相反,我得到一个黑屏。
为什么将 a 添加BottomNavigationBar
到第二页会阻止它弹回第一页?
奇怪的是,使用我手机上的常规 Android 后退按钮确实有效。使用不应该Navigator.pop(context)
与使用 Android 手机上的后退按钮相同吗?如果不是,我怎样才能让我的按钮的行为与 Android 的后退按钮相同?
这是第二页的代码;
class HomeReply extends StatefulWidget {
@override
_HomeReplyState createState() => new _HomeReplyState();
}
class _HomeReplyState extends State<HomeReply> {
@override
void initState() {
super.initState();
}
static List<Widget> _myPages = <Widget>[
ReplyStage(),
Account(),
];
int _selectedIndex = 0;
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
var socialProvider = Provider.of<SocialProvider>(context);
return MaterialApp(
home: FutureBuilder(
future: Future.wait([socialProvider.loadCurrentName(), socialProvider.loadCurrentAvatar()]),
builder: (context, snapshot) {
if (snapshot.hasData) {
return
Scaffold(
appBar: AppBar(
title: Text(
"Back to "Original Post",
),
leading: new IconButton(
icon: new Icon(
Icons.arrow_back,
),
onPressed: () {
Navigator.pop(context);
},
),
),
body: _myPages[_selectedIndex],
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(
Icons.home,
),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Consumer<SocialProvider>(
builder: (context, socialProvider, child) {
return Image.network(snapshot.data[1],
);
}),
title: Consumer<SocialProvider>(
builder: (context, socialProvider, child) {
return Text(snapshot.data[0]);
}
),
),
],
currentIndex: _selectedIndex,
onTap: _onItemTapped,
),
);
} else if(snapshot.hasError) {
return Center(child: CircularProgressIndicator());
} else{
return Center(child: CircularProgressIndicator());
}
}
),
);
}
}
解决方案
看来问题BottomNavigationBar
毕竟不是问题。问题在这里重复MaterialApp
:
return MaterialApp(
home: FutureBuilder
一旦我将其更改return FutureBuilder
为Navigator.pop(context)
恢复正常工作。
推荐阅读
- flutter - Reading asset image dimensions without loading into memory
- python - 在 Python 中循环导入字典
- c# - ASP.NET Core 让用户在部署到 IIS 并在浏览器中启动时无法工作
- javascript - Koajs,简单的应用程序不在本地主机上运行
- r - 重新编码数据框 R 中的多列
- java - How do you save data after onPause(), once the app is closed by user or device?
- javascript - 为什么这个 RegEx 匹配这个数字?
- python-3.x - How to use markdown in discord.py?
- canvas - 帆布进度轮
- r - Error when using for loop in R: 'plot.new has not been called yet'