Flutter:通过滑动关闭对话框

标签 flutter dart

我已经使用对话框创建了此图像查看器,我需要实现滑动以存在(向上或向下拖动)以执行 Navigator.of(context).pop()

这是我需要在其中实现滑动的代码。

showGeneralDialog(
  context: context,
  barrierColor: Colors.grey.shade900
      .withOpacity(0.95), // Background color
  barrierDismissible: false,
  barrierLabel: 'Dialog',
  transitionDuration: Duration(
      milliseconds:
          400), 
  pageBuilder: (_, __, ___) {
    // Makes widget fullscreen
    return SafeArea(
      child: SizedBox.expand(
        child: Column(
          children: <Widget>[
          
          ],
        ),
      ),
    );
  },
);

enter image description here

最佳答案

您可以使用 GestureDetectorSafeArea 包装在对话框内,并使用其 onVerticalDragUpdate

我还用透明的Container包裹了Column以使GestureDetector工作。

showGeneralDialog(
  context: context,
  barrierColor: Colors.grey.shade900
  .withOpacity(0.95), // Background color
  barrierDismissible: false,
  barrierLabel: 'Dialog',
  transitionDuration: Duration(milliseconds: 400),
  pageBuilder: (context, __, ___) {
    // Makes widget fullscreen
    return GestureDetector(
      onVerticalDragUpdate: (details) {
        int sensitivity = 10;
        if (details.delta.dy > sensitivity ||
            details.delta.dy < -sensitivity) {
          Navigator.of(context).pop();
        }
      },
      child: SafeArea(
        child: SizedBox.expand(
          child: Container(
            color: Colors.transparent,
            child: Column(
              children: [],
            ),
          ),
        ),
      ),
    );
  },
);

要实现滑动关闭行为,您可以使用 Dismissible 代替 GestureDetector:

showGeneralDialog(
  context: context,
  barrierColor: Colors.grey.shade900
  .withOpacity(0.95), // Background color
  barrierDismissible: false,
  barrierLabel: 'Dialog',
  transitionDuration: Duration(milliseconds: 400),
  pageBuilder: (context, __, ___) {
    // Makes widget fullscreen
    return Dismissible(
      direction: DismissDirection.vertical,
      onDismissed: (_) {
        Navigator.of(context).pop();
      },
      key: Key("key"),
      child: SafeArea(
        child: SizedBox.expand(
          child: Container(
            color: Colors.transparent,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  color: Colors.white,
                  height: 300,
                )
              ],
            ),
          ),
        ),
      ),
    );
  },
);

关于Flutter:通过滑动关闭对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68056304/

相关文章:

flutter - 如何在Flutter中使用提供程序包创建全局变量?

dart - 如何让 pub build 在 dart 聚合物项目上使用源映射

firebase - 从 flutter 火的火力中获取时间时出错?

flutter - VISUAL STUDIO CODE 工作时 scrcpy 不工作

dart - 导航到另一个屏幕时总是空白

flutter - 从子组件访问值和函数

animation - Flutter - 从父类或另一个子类调用子类的动画函数

dart - 为什么我的 Flutter 包突然警告我它不依赖于它的示例应用程序?

firebase - 未定义类 'FirebaseUser'

android - 无法使用 Flutter 中的项目创建 ExpansionPanelList