flutter - 如何在保持对话框打开的同时关闭抽屉?
问题描述
最小代码:
void main() => runApp(MaterialApp(home: MainPage()));
class MainPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
drawer: MyDrawer(),
);
}
}
class MyDrawer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Drawer(
child: RaisedButton(
onPressed: () {
// Close the Drawer, not the Dialog.
Timer(Duration(seconds: 2), () => Navigator.of(context, rootNavigator: true).pop());
// Show the Dialog and keep it in opened state.
showDialog(
context: context,
builder: (_) => AlertDialog(title: Text('FooDialog')),
);
},
child: Text('Show Dialog'),
),
);
}
}
按下按钮时,我正在显示对话框,2 秒后我想关闭对话框,Drawer
同时保持Dialog
在屏幕上打开。Timer
为此,我正在使用rootNavigator
. Navigator
但是,我的对话被解雇了。
GlobalKey<DrawerControllerState>
除了使用东西之外,还有什么办法可以关闭抽屉吗?
解决方案
class MyDrawer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Drawer(
child: RaisedButton(
onPressed: () {
// Close the Drawer, not the Dialog.
Timer(Duration(seconds: 2), () => Scaffold.of(context).openEndDrawer());
// Show the Dialog and keep it in opened state.
showDialog(
context: context,
builder: (_) => AlertDialog(title: Text('FooDialog')),
);
},
child: Text('Show Dialog'),
),
);
}
}
推荐阅读
- discord - 不和谐自我机器人慢
- android-studio - “没有广告配置。” 尝试加载广告时的 onAdFailedToLoad() 方法
- java - 用于 Java 日志记录的净化信息模式
- css - 如何将此引导登录表单移动到视口中心
- python - DoesNotExist at /profiles/user-profile/ UserProfile 匹配查询不存在
- java - 如果同一个字符出现多次,如何获取一个字符数组并返回true
- javascript - 为什么我看不到调试输出
- javascript - 如何在不覆盖身体的情况下将 div 放在动画身体上方
- tensorflow - Keras - 在 fit() 中更改数据集中拍摄图像的方式
- python - 匹配python中正则表达式的子字符串