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

Flutter项目AppBarTheme用textTheme报错:无对应命名参数

问题解决方法

报错原因

Flutter 2.0及以上版本中,AppBarTheme类已移除textTheme命名参数,原功能被拆分为titleTextStyle(控制AppBar标题样式)和toolbarTextStyle(控制工具栏内其他文本样式)两个独立参数,直接使用textTheme会触发参数不存在的报错。

修改后的代码

将原代码中的textTheme替换为titleTextStyle即可适配当前版本:

AppBarTheme _lightAndroidBar = AppBarTheme(
    centerTitle: true,
    color: Color(COLOR_PRIMARY),
    iconTheme: IconThemeData(color: Colors.white),
    // 替换原textTheme为titleTextStyle
    titleTextStyle: TextStyle(
        color: Colors.white,
        fontSize: 20.0,
        letterSpacing: 0,
        fontWeight: FontWeight.w500),
    brightness: Brightness.light);

补充说明

如果需要统一配置应用全局主题,应在ThemeData中设置appBarTheme参数,而非尝试添加不存在的theme参数,示例如下:

ThemeData lightTheme = ThemeData(
  appBarTheme: AppBarTheme(
    centerTitle: true,
    color: Color(COLOR_PRIMARY),
    iconTheme: IconThemeData(color: Colors.white),
    titleTextStyle: TextStyle(
        color: Colors.white,
        fontSize: 20.0,
        letterSpacing: 0,
        fontWeight: FontWeight.w500),
    brightness: Brightness.light,
  ),
  // 其他主题配置项...
);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 16:42:01