问题描述
我正在尝试使用可以在应用页面上打开抽屉的IconButton,因此,当我点击图标按钮时,我希望看到抽屉.我一直在寻找一种在线执行此操作的方法,但似乎只有两种解决方案:我可以使用应用程序栏将IconButton放入其中,也可以尝试浮动操作按钮.但是它们不是我想要的,我只希望使用IconButton打开抽屉.有可能吗?
I'm trying to use the IconButton that can open the drawer on my app page, so when I tap the icon button, I expect to see the drawer. I've been looking up a way to do so online, but seemed like there're only two solutions: I can either use the appbar to put the IconButton in, or I can try the floating action button. But they're not what I'm looking for, I want just the IconButton to open the drawer. Is it possible do it?
推荐答案
是的,您可以通过 IconButton 轻松打开抽屉,而无需使用 appBar .您需要像使用_scaffoldKey一样使用Key并使用 _scaffoldKey.currentState.openDrawer()方法在 IconButton 小部件中打开抽屉.
Yes, you can easily open the drawer through IconButton without using appBar.You need to use Key like I had used _scaffoldKey and use _scaffoldKey.currentState.openDrawer() method to open the drawer in IconButton widget.
class HomeState extends StatelessWidget {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
drawer: Drawer(
child: ListView(
children: <Widget>[
ListTile(
title: Text("Ttem 1"),
trailing: Icon(Icons.arrow_forward),
),
ListTile(
title: Text("Item 2"),
trailing: Icon(Icons.arrow_forward),
),
],
),
),
body: ListView(
children:[
Container(
margin: EdgeInsets.only(left: 15.0,top:100.0),
child: IconButton(
icon: Icon(Icons.menu),
onPressed: () {
_scaffoldKey.currentState.openDrawer();
},
),
),
]
),
);}
}
这篇关于Flutter-有没有办法仅使用IconButton(不创建应用栏)来打开抽屉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!