Flutter 手動控制drawer(抽屜)打開關閉


  • GlobalKey 方式打開
    import "package:flutter/material.dart";
     
    class MyTest extends StatefulWidget {
      @override
      _MyTestState createState() => new _MyTestState ();
    }
     
    class _MyTestState extends State<MyTest> {
      final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          key: _scaffoldKey,
          drawer: new Drawer(),
          appBar: new AppBar(
            leading: new IconButton(
                icon: new Icon(Icons.settings),
                onPressed: () => _scaffoldKey.currentState.openDrawer()),
          ),
        );
      }
    }
  • builder方式
  • class _MyTestState extends State<MyTest> {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          drawer: new Drawer(),
          appBar: new AppBar(
            leading: Builder(
                builder: (context) => IconButton(
                      icon: new Icon(Icons.settings),
                      onPressed: () => Scaffold.of(context).openDrawer(),
                    ),
            ),
          ),
        );
      }
    }

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM