Flutter配置ThemeData时提示foregroundColor参数未定义如何解决?
报错原因
ThemeData 类本身没有顶层的foregroundColor参数,你将AppBar组件专属的样式属性直接配置到了ThemeData的全局层级下,IDE无法识别到对应参数,因此抛出参数未定义的报错,最终导致项目构建失败。
修复方案
你需要将AppBar相关的样式配置嵌套到appBarTheme参数对应的AppBarTheme对象中,调整后的完整代码如下:
Widget build(BuildContext context) { return MaterialApp( title: 'Startup Name Generator', theme: ThemeData( backgroundColor: Colors.white, // 将foregroundColor移到AppBar专属的主题配置内 appBarTheme: const AppBarTheme( foregroundColor: Colors.black, ), ), home: RandomWords(), ); }
修改完成后重新执行热重载或者全量构建项目,报错就会消失,配置的前景色也会正常生效。
内容的提问来源于stack exchange,提问作者Ned-Arthur
相关产品推荐
相关产品推荐

