Flutter,如何使按钮在Flutter中打开抽屉

时间:2020-08-26 18:15:58

标签: flutter flutter-layout flutter-web flutter-animation flutter-test

我试图做一个打开抽屉的按钮,但我不能,这是我第一次使用颤动

我正在运行的用户界面

Flutter UI

  return Scaffold(

  drawer: Drawer(),
  body: Column(
    children: <Widget>[
      ClipPath(
        clipper: MyClipper(),
        child: Container(
          height: 350,
          width: double.infinity,
          decoration: BoxDecoration(
            gradient: LinearGradient(
                begin: Alignment.topRight,
                end: Alignment.bottomLeft,
                colors: [
                  Color(0xFF3383CD),
                  Color(0xFF11429F),
                ]),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              const SizedBox(height: 12),
              IconButton(
                icon: const Icon(
                  Icons.add, size: 18,
                  color: Colors.white,
                  ),
                onPressed: () {
                  Scaffold.of(context).openDrawer();
                },
              ),

1 个答案:

答案 0 :(得分:0)

做到这一点的最好方法是使用GlobalKey。

  1. 为窗口小部件定义ScaffoldState的GlobalKey。

    GlobalKey<ScaffoldState> scaffolKey = GlobalKey<ScaffoldState>();

  2. 将此密钥分配给脚手架。

Scaffold( key: scaffoldKey, ....)

  1. 使用按钮的onPressed呼叫中的此键呼叫Opendrawer。

FlatButton(onPressed: () { scaffoldKey.currentState.openDrawer(); })