在离开页面之前,使用导航抽屉菜单图标执行某些操作。

huangapple go评论67阅读模式
英文:

Do something before leaving page using navigation drawer menu icon

问题

在Flutter中,我有一些页面带有AppBar,它显示导航抽屉的菜单图标,用于在不同页面之间导航。其中一些页面允许用户编辑一些数据。因此,当用户点击菜单图标而没有保存所做的编辑时,我想要请求确认,但我还没有找到一种方法来覆盖(相当于)导航菜单图标的onTap

在使用底部导航栏时有解决方案,还有一些可以检测页面已被弹出的解决方案,但我需要在关闭页面之前显示确认对话框。

英文:

In Fluter, I have some pages with an AppBar that shows the menu icon of the navigation drawer that it is used to navigate among distinct pages. Some of this pages allow the user to edit some data. So, I would like to ask for confirmation when the user clicks the menu icon without saving the editions she has made, but I have not found a way to override the (equivalent to the) onTap for the navigation menu icon.

There are solutions when a bottom navigation bar is used, and solutions that detect when the page has been popped, but I need the confirmation dialogue to be shown on the page being closed before the closing takes place.

答案1

得分: 0

这可以通过控制抽屉使用传递给Scaffold的全局键(GlobalKey)以及明确指定AppBarleading参数来简单完成:

class MyPage extends StatelessWidget {
  MyPage({Key? key}) : super(key: key);
  final _key = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _key,
      drawer: const Drawer(),
      appBar: AppBar(
        leading: DrawerButton(
          onPressed: () async {
            final state = _key.currentState;
            final isOpen = state?.isDrawerOpen ?? false;

            if (!isOpen) {
              final close = await showDialog<bool>(
                context: context,
                builder: (context) => AlertDialog(
                  title: const Text('确定要关闭此页面吗?'),
                  actions: [
                    ElevatedButton(
                      onPressed: () => Navigator.of(context).pop(true),
                      child: const Text('关闭'),
                    )
                  ],
                ),
              );
              if (close ?? false) state?.openDrawer();
              return;
            }
            state?.closeDrawer();
          },
        ),
      ),
    );
  }
}

如果还有其他需要翻译的内容,请提供并告诉我。

英文:

This can be done simply by controlling the drawer through the global key, passed to the Scaffold, and specifying explicitly the leading parameter of AppBar:

class MyPage extends StatelessWidget {
  MyPage({Key? key}) : super(key: key);
  final _key = GlobalKey&lt;ScaffoldState&gt;();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _key,
      drawer: const Drawer(),
      appBar: AppBar(
        leading: DrawerButton(
          onPressed: () async {
            final state = _key.currentState;
            final isOpen = state?.isDrawerOpen ?? false;

            if (!isOpen) {
              final close = await showDialog&lt;bool&gt;(
                context: context,
                builder: (context) =&gt; AlertDialog(
                  title:
                      const Text(&#39;Are you sure you want to close this page?&#39;),
                  actions: [
                    ElevatedButton(
                      onPressed: () =&gt; Navigator.of(context).pop(true),
                      child: const Text(&#39;Close&#39;),
                    )
                  ],
                ),
              );
              if (close ?? false) state?.openDrawer();
              return;
            }
            state?.closeDrawer();
          },
        ),
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年6月12日 15:06:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定