Pri*_*rma 1 onbackpressed flutter
在我创建的侧抽屉中,当用户单击任何链接时,用户会转到该屏幕,抽屉也会关闭,没有问题,当用户打开侧抽屉并按后退键时,抽屉也会关闭,这也有效。
但是,假设用户单击“我的个人资料”链接(或任何链接),然后如果用户从个人资料屏幕按后退键返回主屏幕(或在应用栏中使用后退键),用户会发现侧抽屉在主屏幕上打开屏幕。然后,用户必须按返回键或点击屏幕上的任意位置才能关闭抽屉。
最初,我的抽屉甚至在后压时也没有关闭,我找到了这篇文章 [/sf/ask/4221990951/] 并实现了WillPopScope() [实际上,这帮助我在后压时关闭抽屉],但是问题我已经解释过,WillPopScope 仍未解决这个问题。
请让我知道我必须做哪些改变才能实现此行为。
这就是我创建文件的方式:HomePage,它有 BottomNavigationDrawer,用户通过它进入购物页面(实际上标记为“主页”),这是用户看到侧抽屉的页面。因此,在上面解释时,当我说“主页”时,我指的是用户导航到个人资料页面的购物页面。
class ShoppingPage extends StatefulWidget {
@override
_ShoppingPageState createState() => _ShoppingPageState();
}
class _ShoppingPageState extends State<ShoppingPage> {
GlobalKey<ScaffoldState> _key = new GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
ThemeData getTheme = Theme.of(context);
return WillPopScope(
onWillPop: () async {
if (_key.currentState.isDrawerOpen) {
Navigator.of(context).pop();
return false;
}
return true;
},
child: Scaffold(
key: _key,
appBar: AppBar(
title: ,
elevation: 0,
),
drawer: CustomDrawer(),
body: SafeArea(
child: Container(
Run Code Online (Sandbox Code Playgroud)
您只需致电:-
Navigator.pop(context);
Run Code Online (Sandbox Code Playgroud)
从抽屉导航到另一个屏幕之前。就像当用户单击您的个人资料时,然后在 onTap 方法中首先弹出它然后导航。
onTap:(){
Navigator.pop(context);
//then write navigation statement over here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4316 次 |
| 最近记录: |