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_12 分钟前
Flutter for OpenHarmony 心情日记功能实战指南
flutter·华为
jiejiejiejie_1 小时前
Flutter for OpenHarmony 倒计时功能实战开发
flutter
Math_teacher_fan1 小时前
Flutter 跨平台开发实战:鸿蒙与音乐律动艺术(六)、Lissajous 利萨茹曲线:频率耦合的轨迹艺术
flutter·ui·数学建模·华为·harmonyos·鸿蒙系统
里欧跑得慢1 小时前
17. Flutter Hero动画实现:让界面过渡更加优雅
前端·css·flutter·web
liulian09162 小时前
Flutter for OpenHarmony 跨平台开发:秒表功能实战指南
flutter
xmdy58663 小时前
Flutter+开源鸿蒙实战|智安盾电商溯源平台Day3 溯源查询逻辑+鸿蒙网络请求适配
flutter·开源·harmonyos
maaath4 小时前
【maaath】Flutter 跨平台日历日程应用开发实战
flutter·华为·harmonyos
xmdy58666 小时前
Flutter+开源鸿蒙实战|智安盾电商溯源平台Day2 首页+核心入口UI开发(鸿蒙多端适配)
flutter·开源·harmonyos
jiejiejiejie_6 小时前
Flutter for OpenHarmony 萌系 UI 实战合集:骨架屏 + 引导页一站式指南
flutter·ui·华为
liulian09167 小时前
Flutter for OpenHarmony 跨平台开发:倒计时功能实战指南
flutter