You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter问题:独立Widget中InkWell点击无响应

Flutter ContainerButton 点击无响应的修复方案

你的ContainerButton点击没反应有两个核心问题:

  • 没有调用传入的回调函数:InkWell的onTap里只写了widget.onTap,这只是引用函数对象,没有执行它,必须加括号调用:widget.onTap()
  • 添加了触摸屏蔽组件:你给Container套了IgnorePointer,这个组件会直接忽略所有触摸事件,完全没必要保留,直接删掉它就行

修复后的ContainerButton代码:

class ContainerButton extends StatefulWidget {
  final Function onTap;
  final Color containerColor;
  final String containerText;

  const ContainerButton({Key? key, required this.onTap, required this.containerColor, required this.containerText}) : super(key: key);

  @override
  State<ContainerButton> createState() => _ContainerButtonState();
}

class _ContainerButtonState extends State<ContainerButton> {
  @override
  Widget build(BuildContext context) {
    return Material(
      child: InkWell(
        onTap: () {
          widget.onTap(); // 修正:调用传入的回调函数
        },
        child: Container( // 移除IgnorePointer组件
          width: 350,
          height: 100,
          decoration: BoxDecoration(
            color: widget.containerColor,
            borderRadius: BorderRadius.circular(20),
            border: Border.all(color: Colors.black38, width: 5),
            boxShadow: [
              BoxShadow(
                color: Colors.black54,
                blurRadius: 2.0,
                spreadRadius: 0.0,
                offset: Offset(2.0, 2.0),
              )
            ],
          ),
          child: Center(child: Text(widget.containerText, style: TextStyle(fontSize: 25))),
        ),
      ),
    );
  }
}

内容的提问来源于stack exchange,提问作者HolyPoly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 03:45:31