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
相关产品推荐
相关产品推荐

