如何在 Flutter 的 appbar 之外添加 endDrawer?

How can I add endDrawer outside of appbar in Flutter?

我想给图标按钮添加一个抽屉,但它在应用栏之外 在这段代码中,我试图通过观看一些教程来实现它,但它对我不起作用,也许是因为我使用了 endDraw 任何人都知道如何去做?

这是我的代码

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

    @override
     
    
         Widget build(BuildContext context) {
            return Scaffold(
              key: _scaffoldKey,

          endDrawer: Drawer2() ,
     

     appBar: AppBar(
        backgroundColor: MyColor.backgroud_primary_color,
        leading: Icon(
          Icons.chevron_left_rounded,
          color: MyColor.icon_color,
        ),
        centerTitle: true,
        title: Container(
          width: 100,
          height: 100,
          child: Image(
            image: AssetImage("assets/logo.png"),
            fit: BoxFit.contain,
          ),
        ),
        actions: [
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 5),
            child: IconButton(
              onPressed: () => _scaffoldKey.currentState!.openDrawer(),
              icon: Icon(Icons.sort),
              iconSize: 30,
              color: MyColor.icon_color,
            ),
          )
        ],
      ),

Drawer2() 是我制作的自定义抽屉我想在单击图标按钮时打开最后一个抽屉有什么办法吗?

使用:

 Scaffold.of(context).openEndDrawer()

如果您想禁用拖动行为,请在 Scaffold 中设置:

drawerEnableOpenDragGesture: false,

要打开你需要使用的抽屉_scaffoldKey.currentState!.openEndDrawer(), 基于 endDrawer 文档,here

所以,您的代码应该是:

actions: [
  Padding(
    padding: EdgeInsets.symmetric(horizontal: 5),
    child: IconButton(
      onPressed: () => _scaffoldKey.currentState!.openEndDrawer(),
      icon: Icon(Icons.sort),
      iconSize: 30,
      color: MyColor.icon_color,
    ),
  )
],
import 'package:flutter/material.dart';

class DemoScreen extends StatelessWidget {
  const DemoScreen({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Demo Screen"),),
      endDrawer: Drawer2(),
      body: Center(
        child: Text("data")
      ),
    );
  }
}

class Drawer2 extends StatelessWidget {
  const Drawer2({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Drawer( // Custom widget must be return Drawer 
      child: ListTile( //Implement any design on child property
        title: Text("Demo tile"),
      ),
    );
  }
}