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

Flutter类内定义Function调用失效:CustomDrawer组件点击回调无响应

问题原因

  • InkWell组件的onTap回调写法错误,当前onTap: () => GoTap仅返回了GoTap函数对象,没有触发执行逻辑,因此点击无响应。
  • 可选优化:StatelessWidget的成员变量需要用final修饰保证不可变性,Function类型建议明确声明为VoidCallback提升类型安全性,同时变量命名建议遵循Dart小驼峰规范。

修正后完整代码

class CustomDrawer extends StatelessWidget {
  final IconData icon;
  final String text;
  final VoidCallback goTap;

  CustomDrawer(this.icon, this.text, this.goTap);

  @override
  Widget build(BuildContext context) {
    return Padding(
        padding: const EdgeInsets.fromLTRB(8.0, 0, 8.0, 0),
        child: Container(
          decoration: BoxDecoration(
              border: Border(bottom: BorderSide(color: Colors.grey))
          ),
          child: InkWell(
              splashColor: Colors.yellow,
              // 直接传入回调函数,也可以写作 onTap: () => goTap()
              onTap: goTap,
              child: Container(
                height: 40,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Icon(icon),
                        Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Text(
                              text,
                              style: TextStyle(fontSize: 16.0),
                            ))
                      ],
                    ),
                    Icon(Icons.arrow_right)
                  ],
                ),
              ))));
  }
}

修改后原有调用逻辑CustomDrawer(Icons.arrow_left, 'Back', () => Get.back())无需调整,点击即可正常触发返回逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 00:15:04