纸月
发布于 2021-04-22 / 266 阅读
0
0

Flutter drawer 自定义图标 icon 手动打开 drawer

使用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')
          );
        })
      ],
    );
  }
}

评论