使用GlobalKey 方式
import "package:flutter/material.dart";
class Test extends StatefulWidget {
@override
_TestState createState() => new _TestState();
}
class _TestState extends State<Test> {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return new Scaffold(
key: _scaffoldKey,
drawer: Drawer(),
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.change_history),
onPressed: () => _scaffoldKey.currentState!.openDrawer()),
),
);
}
}
使用Builder方式
import "package:flutter/material.dart";
class Test extends StatefulWidget {
@override
_TestState createState() => new _TestState();
}
class _TestState extends State<Test> {
@override
Widget build(BuildContext context) {
return Column(
children: [
Builder(builder: (context) {
return InkWell(
onTap: () {
Scaffold.of(context).openEndDrawer();
},
child: Text('data')
);
})
],
);
}
}