Flutter: How to open Drawer programmatically

前端 未结 6 1001
暖寄归人
暖寄归人 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条回答
  •  Happy的楠姐
    2020-12-29 21:47

    Here is another example of opening the drawer programmatically from a hamburger icon and without the Appbar:-

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatefulWidget {
      @override
      State createState() => MyAppState();
    }
    
    class MyAppState extends State {
      var scaffoldKey = GlobalKey();
    
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            key: scaffoldKey,
            drawer: new Drawer(
              child: new ListView(
                padding: EdgeInsets.zero,
                children: [
                  DrawerHeader(
                    child: Text('Drawer Header'),
                    decoration: BoxDecoration(
                      color: Colors.blue,
                    ),
                  ),
                  ListTile(
                    title: Text('Item 1'),
                    onTap: () {
                      //Do some stuff here
                      //Closing programmatically - very less practical use
                      scaffoldKey.currentState.openEndDrawer();
                    },
                  )
                ],
              ),
            ),
            body: Stack(
              children: [
                new Center(
                    child: new Column(
                  children: [],
                )),
                Positioned(
                  left: 10,
                  top: 20,
                  child: IconButton(
                    icon: Icon(Icons.menu),
                    onPressed: () => scaffoldKey.currentState.openDrawer(),
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
    

提交回复
热议问题