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

Flutter BMI计算器点击体重年龄加减按钮数值不更新问题求助

问题原因排查

你遇到的数值不更新问题是两处代码BUG导致的,按下方修正即可:

  • 第一处:自定义RoundIconButton组件的点击回调未被执行
    你在RoundIconButton中给RawMaterialButton配置的onPressed属性仅引用了传入的回调函数,没有实际触发执行,修正方式如下:
class RoundIconButton extends StatelessWidget {
  RoundIconButton({@required this.icon,@required this.onPressed});
  final IconData icon;
  final Function onPressed;
  Widget build(BuildContext context) {
    return RawMaterialButton(
      elevation: 6,
      child: Icon(icon,color: Colors.white,),
      // 错误写法:只引用函数没有执行
      // onPressed: (){
      //   onPressed;
      // },
      // 修正写法:直接透传回调函数即可
      onPressed: onPressed,
      constraints: BoxConstraints.tightFor(
        width:56.0,
        height:56.0,
      ),
      shape:CircleBorder(),
      fillColor:  Color(0xFF4C4F5E),
    );
  }
}
  • 第二处:年龄模块的加号按钮逻辑写错
    你在年龄的加按钮回调中误写了修改体重的逻辑,导致点击年龄加号只会修改体重数值,年龄不会变化,修正方式如下:
// 年龄模块的加号按钮修正后代码
RoundIconButton(
  icon:FontAwesomeIcons.plus,
  onPressed: (){
    setState(() {
      age++;
    });
  },
),

完成以上两处修改后,体重和年龄的加减按钮就可以正常更新对应数值了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 11:24:05