Flutter,如何让按钮在 Flutter 中打开抽屉
Flutter, How To make Button to open drawer in Flutter
我想制作一个打开抽屉的按钮,但我做不到,这是我第一次使用 flutter
我的运行ui
return Scaffold(
drawer: Drawer(),
body: Column(
children: <Widget>[
ClipPath(
clipper: MyClipper(),
child: Container(
height: 350,
width: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [
Color(0xFF3383CD),
Color(0xFF11429F),
]),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const SizedBox(height: 12),
IconButton(
icon: const Icon(
Icons.add, size: 18,
color: Colors.white,
),
onPressed: () {
Scaffold.of(context).openDrawer();
},
),
最好的方法是使用 GlobalKey。
为您的小部件定义 ScaffoldState 的 GlobalKey。
GlobalKey<ScaffoldState> scaffolKey = GlobalKey<ScaffoldState>();
将此密钥分配给脚手架。
Scaffold( key: scaffoldKey, ....)
- 使用按钮的 onPressed 调用中的此键调用 Opendrawer。
FlatButton(onPressed: () { scaffoldKey.currentState.openDrawer(); })
我想制作一个打开抽屉的按钮,但我做不到,这是我第一次使用 flutter
我的运行ui
return Scaffold(
drawer: Drawer(),
body: Column(
children: <Widget>[
ClipPath(
clipper: MyClipper(),
child: Container(
height: 350,
width: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [
Color(0xFF3383CD),
Color(0xFF11429F),
]),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const SizedBox(height: 12),
IconButton(
icon: const Icon(
Icons.add, size: 18,
color: Colors.white,
),
onPressed: () {
Scaffold.of(context).openDrawer();
},
),
最好的方法是使用 GlobalKey。
为您的小部件定义 ScaffoldState 的 GlobalKey。
GlobalKey<ScaffoldState> scaffolKey = GlobalKey<ScaffoldState>();
将此密钥分配给脚手架。
Scaffold( key: scaffoldKey, ....)
- 使用按钮的 onPressed 调用中的此键调用 Opendrawer。
FlatButton(onPressed: () { scaffoldKey.currentState.openDrawer(); })