flutter - 如何在 Flutter 中的任何图标上显示弹出菜单?
问题描述
当我单击应用程序栏中的图标时,我想要一个弹出菜单或某种带有选项的幻灯片屏幕,但是我不想使用 PopMenuButton,因为我不想使用该图标。我怎样才能做到这一点?
我的代码
return new Scaffold(
appBar: new AppBar(
title: new Text("Home"),
leading: IconButton(
icon: Icon(
Icons.dehaze,
color: Colors.black,
),
onPressed: () {
// do something
},
),
),
body: new Center(...),
);
解决方案
@Denise,您无需手动创建按钮并为抽屉菜单分配操作。你可以像这样简单drawer
地Scaffold
使用小部件,Drawer
class MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Test'),
),
drawer: Drawer(
// Add a ListView to the drawer. This ensures the user can scroll
// through the options in the drawer if there isn't enough vertical
// space to fit everything.
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(
color: Colors.blue,
),
),
ListTile(
title: Text('Item 1'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
title: Text('Item 2'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
],
),
),
body: Padding(
padding: EdgeInsets.all(20.0),
child: Center(
child: Column(
children: <Widget>[
Text('')
],
)
)
),
)
);
}
}
如果你想使用不同的图标,
class MyAppState extends State<MyApp> {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text('Test'),
leading: new IconButton(
icon: new Icon(Icons.dehaze),
onPressed: () => _scaffoldKey.currentState.openDrawer()),
),
drawer: Drawer(......
希望这可以帮助。
推荐阅读
- javascript - Jquery不显示循环数据的结果
- scala - 具有相同签名和参数类型的 Scala 模糊引用
- linux - 如何使用 bash 脚本删除范围行和最后一行?
- shell - 如何显示在 shell 脚本中作为参数传递的文件的 n 行?
- shell - 我想删除文件中与确切模式而不是字符串匹配的行
- solr - 移除请求 URL 中的 L 参数
- android - 更改sdk版本时无法访问互联网
- javascript - 如何定义服务工作者缓存的范围?
- c# - C# - comboBox1 的数据源依赖于 comboBox2 的值
- docusignapi - DocuSign 仍然支持 grant_type=password 吗?