Flutter: How to open Drawer programmatically

前端 未结 6 997
暖寄归人
暖寄归人 2020-12-29 21:11

I want to open Drawer programmatically not by sliding it, how to disable that sliding functionality (touch functionality of Drawer)

6条回答
  •  感情败类
    2020-12-29 22:07

    You'll need to create a GlobalKey and use openDrawer() method on it.

    GlobalKey _drawerKey = GlobalKey();
    
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        key: _drawerKey, // assign key to Scaffold
        drawer: Drawer(),
        floatingActionButton: FloatingActionButton(
          onPressed: () => _drawerKey.currentState.openDrawer(), // open drawer
        ),
      );
    }
    

提交回复
热议问题