问题描述
我正在使用这个问题中的方法来改变脚手架的主体:
I am using the method in this question to change the body of a Scaffold in flutter:
Flutter Drawer小部件-更改Scaffold.body内容
所描述的方法非常有效.现在,我只希望抽屉在用户点击其中一项后自动关闭.
The method described works perfectly. Now I would like just the drawer to automatically close after the users taps on one of the items.
我尝试使用Navigator.pop()方法,但它会弹出整个屏幕,而不仅仅是弹出窗口.它让我的屏幕全黑了.
I tried using the Navigator.pop() method, but it pops the entire screen, not just the drawer. It leaves me with a totally black screen.
有什么建议吗?
推荐答案
您是否完全使用Navigator.of(context).pop()
?我无法重现您的问题,您能发表一个简单的例子来重现它吗?
Are you using exactly Navigator.of(context).pop()
? I cannot reproduce your problem, can you post a minimal example to reproduce it?
以下代码按预期工作:设置按钮弹出抽屉,而其他按钮则不弹出.
The following code works as expected: the settings button pops away the drawer, while the other don't.
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String text = "Initial Text";
@override
Widget build(BuildContext context) {
return new Scaffold(
drawer: new Drawer(
child: new ListView(
children: <Widget>[
new Container(child: new DrawerHeader(child: new Container())),
new Container (
child: new Column(
children: <Widget>[
new ListTile(leading: new Icon(Icons.info),
onTap:(){
setState((){
text = "info pressed";
});
}
),
new ListTile(leading: new Icon(Icons.save),
onTap:(){
setState((){
text = "save pressed";
});
}
),
new ListTile(leading: new Icon(Icons.settings),
onTap:(){
setState((){
text = "settings pressed";
});
Navigator.of(context).pop();
}
),
]
),
)
],
),
),
appBar: new AppBar(title: new Text("Test Page"),),
body: new Center(child: new Text((text)),
));
}
}
这篇关于更改Scaffold.body值后如何在抽屉中隐藏抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!