Flutter ScaffoldKeyのopenDrawerメソッドを使ってDrawerを開く方法

環境
Windows11 pro 64bit
Flutter 3.3.7

構文
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

_scaffoldKey.currentState!.openDrawer();
ScaffoldKeyのopenDrawerメソッドを使うことによって、Drawerを開くことが出来ます。

使用例

final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

 @override
 Widget build(BuildContext context) {
   return Scaffold(
       key: _scaffoldKey, //Keyの設定
       drawer: Drawer(),
       appBar: AppBar(
         leading: IconButton(
             onPressed: () {
               _scaffoldKey.currentState!.openDrawer(); 
             },
             icon: Icon(Icons.settings)),
         title: Text('sample'),
       ),
       body: Center());
 }

 

Flutter

Posted by arkgame