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

相关推荐
Eric_HYD2 小时前
Flutter 字体字生效原理解析
flutter
maaath2 小时前
【无标题】Flutter for OpenHarmony 的文具手账应用开发实践
flutter·华为·harmonyos
里欧跑得慢2 小时前
Flutter 主题管理:构建一致的用户界面
前端·css·flutter·web
liulian091617 小时前
Flutter for OpenHarmony 跨平台开发:单位转换功能实战指南
flutter
千码君201618 小时前
Trae:一些关于flutter和 go前后端开发构建的分享
android·flutter·gradle·android-studio·trae·vibe code
maaath19 小时前
【maaath】Flutter for OpenHarmony 手表配饰应用实战开发
flutter·华为·harmonyos
maaath20 小时前
【maaath】Flutter for OpenHarmony 跨平台计算器应用开发实践
flutter·华为·harmonyos
maaath1 天前
【maaath】Flutter for OpenHarmony 闹钟时钟应用开发实战
flutter·华为·harmonyos
maaath1 天前
【maaath】Flutter for OpenHarmony 短信管理应用实战
flutter·华为·harmonyos
maaath1 天前
【maaath】Flutter for OpenHarmony打造跨平台便签备忘录应用
flutter·华为·harmonyos