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

Flutter 3.3.6升级后按钮参数未定义报错求助

Flutter 3.3.6 按钮参数错误修复方案

Flutter 3.x版本开始废弃了RaisedButton、FlatButton等旧按钮组件,同时调整了按钮样式参数的传递方式,以下是针对你代码的具体修改:

1. RaisedButton 替换为 ElevatedButton

旧的RaisedButton已被废弃,改用ElevatedButton,原elevation、color、shape参数需移至style配置中:

修改前代码:

return ButtonTheme(
  minWidth: 110,
  child: RaisedButton(
      elevation: 0,
      child: isLoading
          ? _buildLoadingIndicatorWithColor(textColor)
          : Text(
              text,
              style: TextStyle(
                  color: textColor,
                  fontWeight: FontWeight.bold,
                  fontSize: 16),
            ),
      color: communityColor,
      onPressed: onPressed,
      shape: new RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(borderRadius))),
);

修改后代码:

return ElevatedButton(
  style: ElevatedButton.styleFrom(
    minimumSize: const Size(110, kMinInteractiveDimension), // 替代ButtonTheme的minWidth
    elevation: 0,
    backgroundColor: communityColor, // 原color参数改为backgroundColor
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(borderRadius),
    ),
  ),
  child: isLoading
      ? _buildLoadingIndicatorWithColor(textColor)
      : Text(
          text,
          style: TextStyle(
            color: textColor,
            fontWeight: FontWeight.bold,
            fontSize: 16,
          ),
        ),
  onPressed: onPressed,
);

说明:

  • RaisedButton替换为ElevatedButton
  • 原ButtonTheme的minWidth通过styleFrom的minimumSize实现,kMinInteractiveDimension是Flutter内置的默认按钮高度
  • 原color参数更名为backgroundColor

2. FlatButton 替换为 TextButton

旧的FlatButton已被废弃,改用TextButton,原textColor参数需移至style配置:

修改前代码:

return ButtonTheme(
  minWidth: minWidth,
  height: height,
  child: FlatButton(
    textColor: Colors.black87,
    child: this.child,
    onPressed: this.onPressed,
  ),
);

修改后代码:

return TextButton(
  style: TextButton.styleFrom(
    minimumSize: Size(minWidth, height), // 替代ButtonTheme的minWidth和height
    foregroundColor: Colors.black87, // 原textColor参数改为foregroundColor
  ),
  child: child,
  onPressed: onPressed,
);

说明:

  • FlatButton替换为TextButton
  • 原ButtonTheme的尺寸参数合并到styleFrom的minimumSize
  • 原textColor参数更名为foregroundColor

3. TextButton 的 materialTapTargetSize 参数更名

Flutter 3.x中TextButton.styleFrom的materialTapTargetSize参数更名为tapTargetSize:

修改前代码:

hasText
                ? TextButton(
                    style: TextButton.styleFrom(
                        materialTapTargetSize:
                            MaterialTapTargetSize.shrinkWrap),
                    onPressed: _cancelSearch,
                    child: OBText(localizationService.user_search__cancel),
                  )
                : const SizedBox(
                    width: 15.0,
                  )

修改后代码:

hasText
    ? TextButton(
        style: TextButton.styleFrom(
          tapTargetSize: MaterialTapTargetSize.shrinkWrap, // 参数名修改
        ),
        onPressed: _cancelSearch,
        child: OBText(localizationService.user_search__cancel),
      )
    : const SizedBox(
        width: 15.0,
      )

说明:直接将参数名从materialTapTargetSize替换为tapTargetSize即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 01:20:37