Flutter踩坑记录(二)-- GestureDetector+Expanded点击无效果

Expanded是自动扩展布局,它可以用于Row, ColumnFlex布局中。Expanded部件会尝试占用尽可能多的空间。

但是在GestureDetector中,点击事件会无效:

解决方法

给GestureDetector包裹的最外层Widget加一个背景色:

复制代码
GestureDetector(
      onTap: onTap,
      child: Container(
        color: Colors.transparent,
        height: 50,
        margin: const EdgeInsets.only(left: 10, right: 10),
        child: Stack(
          alignment: Alignment.center,
          children: [
            Row(
              children: <Widget>[
                Image.asset(image),
                Container(
                  margin: const EdgeInsets.only(left: 10),
                  child: Text(
                    title,
                    style: const TextStyle(fontSize: 16),
                  ),
                ),
                Expanded(child: Container()), //自动扩展挤压
                Image.asset(
                  'assets/images/community_icon_right.png',
                  width: 20,
                )
              ],
            ),
          ],
        ),
      ),
    );
相关推荐
恋猫de小郭33 分钟前
Flutter Zero 是什么?它的出现有什么意义?为什么你需要了解下?
android·前端·flutter
一只大侠的侠5 小时前
Flutter开源鸿蒙跨平台训练营 Day 10特惠推荐数据的获取与渲染
flutter·开源·harmonyos
崔庆才丨静觅7 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60618 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了8 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅8 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅8 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
renke33649 小时前
Flutter for OpenHarmony:色彩捕手——基于HSL色轮与感知色差的交互式色觉训练系统
flutter
崔庆才丨静觅9 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment9 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端