flutter - 从特定屏幕自动返回时如何关闭导航抽屉?
问题描述
我正在开发一个颤振项目,该项目有一个带有三个路线的导航抽屉。每当我前往特定路线并返回时,导航抽屉就会自动打开。我希望导航抽屉保持关闭状态,除非用户特别点击它。有什么办法可以做到这一点?
这是我的代码:
class NavList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
UserAccountsDrawerHeader(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/orange.png'),
fit: BoxFit.cover,
)),
arrowColor: Colors.deepOrangeAccent[700],
accountName: Text(''),
accountEmail: Text(
'username@gmail.com',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
),
),
currentAccountPicture: CircleAvatar(
radius: 22.0,
backgroundColor: Colors.deepOrangeAccent[700],
backgroundImage: AssetImage('images/profile.png'),
),
),
ListItems(
listTitle: 'Shops',
listIcon: Icon(Icons.shop),
listFunction: () {
Navigator.pushNamed(context, ShopScreen.id);
},
),
ListItems(
listTitle: 'Orders',
listIcon: Icon(Icons.monetization_on),
listFunction: () {
Navigator.pushNamed(context, OrderScreen.id);
},
),
ListItems(
listTitle: 'Logout',
listIcon: Icon(Icons.logout),
listFunction: () {
Navigator.pushNamed(context, LoginScreen.id);
},
),
],
),
);
}
}
I have refactored ListItems.
class ListItems extends StatelessWidget {
ListItems({this.listTitle, this.listIcon, this.listFunction});
final String listTitle;
final Icon listIcon;
final Function listFunction;
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(listTitle),
leading: IconButton(
icon: listIcon,
onPressed: () {
},
),
onTap: listFunction,
);
}
}
解决方案
有一些方法可以做到这一点:
onPressed:(){
Navigator.pop(context);
Navigator.pushNamed(context, ShopScreen.id);
}
或使用导航器的 .then:
onPressed:(){
Navigator.pushNamed(context, ShopScreen.id).then((val){
Navigator.pop(context);
});
}
此外,您可以检查抽屉当前是否打开:
onPressed:(){
Navigator.pushNamed(context, ShopScreen.id).then((val){
if(Scaffold.of(context).isDrawerOpen){
Navigator.pop(context);
}
});
}
推荐阅读
- opengl-es-2.0 - 在 GLSurfaceView Android 上更改 Bitmp
- python - 如何使用非跟踪 CECMod 计算能量输出
- java - 为什么我的后端应用程序没有在 Springboot 中运行?
- design-patterns - 何时使用工厂方法
- laravel - Laravel 用反引号包装 int 参数
- java - NoClassDefFoundError - 通过 SpigotMC 插件中的 URLClassLoader 加载子类
- html - 当我在图像上设置宽度时,它会消失(fllexbox)
- r - 使用 geom_xspline() 部分平滑折线图
- r - window.default(x, ...) 中的错误:在 R 中对 VAR 模型进行交叉验证期间,“开始”不能在“结束”之后
- php - Ubuntu 20.04 上的 Maxima 分段错误 PHP exec()