Flutter通过showDialog实现下拉筛选菜单效果

一、效果图

二、 实现方式

  1. 获取固定在顶部筛选头部Widget在屏幕上的位置和它的高度
  2. 在弹窗中通过获取到的高度进行内容显示区域定位
  3. 巧用AnimatedContainer组件实现下拉动画效果
  4. 最后在底部加上黑色蒙层
dart 复制代码
unawaited(
      showDialog(
        context: context,
        useSafeArea: false,
        barrierColor: Colors.transparent,
        builder: (_) {
          return Padding(
            padding: EdgeInsets.only(top: startY),
            child: Column(
              children: [
                ColoredBox(
                  color: Colors.white,
                  child: Column(
                    children: [
                      Container(
                        height: 1,
                        margin: const EdgeInsets.symmetric(horizontal: 16),
                        color: const Color(0xfff6f7f9).withOpacity(0.8),
                      ),
                      ///下拉效果
                      ValueListenableBuilder<double>(
                        valueListenable: _notifier,
                        builder: (context, value, child) {
                          return AnimatedContainer(
                            height: value,
                            curve: Curves.fastEaseInToSlowEaseOut,
                            duration: const Duration(milliseconds: 300),
                            child: child,
                          );
                        },
                        child: widget.content,
                      ),
                    ],
                  ),
                ),
                ///底部黑色蒙层
                Expanded(
                  child: GestureDetector(
                    onTap: _close,
                    behavior: HitTestBehavior.opaque,
                    child: Container(
                      color: Colors.black.withOpacity(0.7),
                    ),
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );

三、具体代码可查看此处的完整Demo

相关推荐
jiejiejiejie_4 小时前
Flutter 三方库 pull_to_refresh 的鸿蒙化适配指南
flutter·华为·harmonyos
SoaringHeart5 小时前
Flutter进阶:用OverlayEntry 实现所有弹窗效果
前端·flutter
MonkeyKing12 小时前
Flutter约束模型(BoxConstraints)与布局体系完全解析
flutter
IntMainJhy13 小时前
Flutter 三方库 ImageCropper 图片裁剪鸿蒙化适配与实战指南(正方形+自定义比例全覆盖)
flutter·华为·harmonyos
IntMainJhy13 小时前
Flutter for OpenHarmony 第三方库六大核心模块整合实战全解|从图片处理、消息通知到加密存储、设备推送 一站式鸿蒙适配开发总结
flutter·华为·harmonyos
张风捷特烈13 小时前
状态管理大乱斗#02 | Bloc 源码全面评析
android·前端·flutter
IntMainJhy14 小时前
【fluttter for open harmony】Flutter 三方库适配实战:在 OpenHarmony 上实现图片压缩功能(附超详细踩坑记录)
flutter·华为·harmonyos
jiejiejiejie_14 小时前
Flutter for OpenHarmony 多语言国际化超简单实现指南
flutter·华为·harmonyos
里欧跑得慢15 小时前
12. CSS滤镜效果详解:为页面注入艺术灵魂
前端·css·flutter·web
里欧跑得慢15 小时前
CSS 级联层:控制样式优先级的新方式
前端·css·flutter·web