Flutter问题:独立Widget中InkWell点击无响应
你的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
相关产品推荐
相关产品推荐

