【问题标题】:How to get openEndDrawer() to respond to my custom AppBar Button instead of imaginary AppBar button?如何让 openEndDrawer() 响应我的自定义 AppBar 按钮而不是想象的 AppBar 按钮?
【发布时间】:2021-07-08 14:32:13
【问题描述】:

我对颤振/飞镖相对较新,我想在用户点击我的自定义应用栏上的汉堡菜单时以编程方式打开一个抽屉。但是,在执行以下代码后,

profile_page.dart

Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
            child: CustomScrollView(
          slivers: [
            SliverAppBar(
              floating: false,
              pinned: true,
              primary: true,
              flexibleSpace: TopBar(
                title: "Profile",
                hasBackButton: false,
                hasHamburgerMenu: true,
                openDrawer: () {
                  Scaffold.of(context).openEndDrawer();
                },
              ),
              collapsedHeight: 180,
              expandedHeight: 180,
              backgroundColor: Color.fromRGBO(250, 250, 250, 1),
            )
          ],
        )),
        endDrawer: Drawer(
            child: ListView(
          padding: EdgeInsets.zero,
          children: [
            DrawerHeader(
                decoration: BoxDecoration(color: Theme.of(context).accentColor),
                child: Text("Drawer Header")),
            ListTile(
              title: Text("Test 1"),
              onTap: () {
                Navigator.pop(context);
              },
            )
          ],
        )));
  }

top_bar.dart

Widget build(BuildContext context) {
    return Container(
      child: Padding(
        padding: const EdgeInsets.only(top: 25.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            if (hasBackButton ?? true)
              Container(
                child: GestureDetector(
                  child: Image(
                      image: AssetImage("assets/images/previous.png"),
                      color: Theme.of(context).accentColor,
                      width: 25,
                      height: 25),
                  onTap: onPressed,
                ),
              )
            else
              Text(""),
            Text(""),
            Text(title ?? "Top Bar",
                style: TextStyle(
                    fontSize: 24,
                    fontWeight: FontWeight.w800,
                    color: Theme.of(context).accentColor)),
            Text(""),
            if (hasHamburgerMenu ?? false)
              InkWell(
                  onTap: openDrawer, child: Icon(Icons.menu))
            else
              Text("")
          ],
        ),
      ),
    );
  }

我的界面显示openEndDrawer() 被标记为“虚构的”AppBar 汉堡菜单按钮,而不是我的汉堡菜单图标,如下面的屏幕截图所示。

响应onTap() 的白色汉堡菜单根本没有在我的应用程序中编码。 很高兴我能在这方面得到任何形式的帮助。谢谢!

【问题讨论】:

    标签: flutter dart navigation-drawer flutter-appbar


    【解决方案1】:

    End Drawer 是 Scaffold 的一部分,因此您可以像这样打开 Drawer:

    Scaffold.of(context).openEndDrawer();
    

    【讨论】:

    • 我确实在 profile_page.dart 中这样做了 .. 但这不是我所要求的。我可以打开抽屉,但 onTap() 函数被标记为“不可见”的汉堡菜单按钮,而不是我的 appbar 汉堡按钮。考虑到我什至没有使用默认的 AppBar,不知道为什么会发生这种情况。
    猜你喜欢
    • 2013-02-17
    • 1970-01-01
    • 2021-06-12
    • 2022-11-14
    • 2018-02-08
    • 2020-10-05
    • 1970-01-01
    • 2021-06-18
    • 2013-06-05
    相关资源
    最近更新 更多