【问题标题】:How to listen to previous screen Listener when navigate to a new Page?导航到新页面时如何收听上一个屏幕监听器?
【发布时间】:2021-11-18 08:08:43
【问题描述】:

我有一个关于手势的问题。

我有一个按钮,在长按时触发显示对话框,在 onMovePointer 时选择选项,并在用户释放指针时关闭对话框。想象一下它就像偷看和拖动选择一样。

这是我的代码:

ElevatedButton(
  onPressed: () {
    Navigator.of(context).push(PageRouteBuilder(
        pageBuilder: (context, animation,
            secondaryAnimation) {
          return IgnorePointer(
              child: OnMoveDialog());
        },
        opaque: false));
  },
  child: Text('Show dialog'),

【问题讨论】:

    标签: flutter dart flutter-layout gesture-recognition


    【解决方案1】:

    尝试使用 GetX 状态管理,而不是使用监听器使用 observation 变量 有关更多信息,请查看此链接 https://pub.dev/packages/get

    如果你想坚持纯飞镖,你必须使用全局监听器和通知器。

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • 我想用纯flutter,所以不依赖包
    • @KhoaLe 我无法理解您的具体需求,但请查看此链接,希望对您有所帮助medium.com/flutter-community/…
    【解决方案2】:

    您可以使用 Navigator API 返回模态结果。

    文档显示了页面示例,但同样适用于模式/对话框页面

    页面小部件

    在您的情况下,在您的 ElevatedButton 上,等待 Navigator 响应:

    class _MyWidgetState extends State<MyWidget> {
      /// ...
    
      @override
      Widget build(BuildContext context) {
        return ElevatedButton(
            onPressed: () {
            /// Wait for the result with `await Navigator...` and store it on a variable
            final result = await Navigator.of(context).push<String>(
              PageRouteBuilder(
                pageBuilder: (context, animation, secondaryAnimation) {
                  return IgnorePointer(
                    child: OnMoveDialog(),
                  );
                },
                opaque: false,
              ),
            );
    
            if (result == 'option-1') { /* Handle option 1 */ }
            if (result == 'option-2') { /* Handle option 2 */ }
          },
          child: Text('Show dialog'),
        );
      }
    
      /// ...
    }
    

    对话框小部件

    在您的模态小部件中,弹出时返回值

    
    class _MyModalState extends State<MyModalWidget> {
      /// ...
    
      @override
      Widget build(BuildContext context) {
        /// Pseudo Widget, is just to examplify your modal behavior
        return MyModal(
          children: [
            Button(
              'Option 1', 
              /// Here the magic happens, you need to pop the result
              onTap: () => Navigator.pop<String>('option-1'),
            ),
            Button(
              'Option 2', 
              /// Same here, remeber: you can receive this callback as arguments
              onTap: () => Navigator.pop<String>('option-2'),
            ),
          ],
        );
      }
    
      /// ...
    }
    
    

    【讨论】:

      猜你喜欢
      • 2018-06-07
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 2013-11-10
      • 1970-01-01
      • 2016-06-12
      • 2022-01-14
      相关资源
      最近更新 更多