Flutter修改AppBar背景色报错:无backgroundColor命名参数
解决Flutter AppBar设置背景色报错的问题
为啥会报错?
Flutter 3.13及以上版本已经移除了AppBar的backgroundColor参数(之前是废弃状态),你直接写这个参数自然会提示找不到。
怎么改?
给单个AppBar改颜色
直接用theme参数来设置:
AppBar( title: const Text('你的页面标题'), theme: Theme.of(context).appBarTheme.copyWith( backgroundColor: MaterialStateProperty.all(Colors.blueGrey[900]), ), )
或者也可以这么写(部分新版本支持):
AppBar( title: const Text('你的页面标题'), backgroundColor: MaterialStateProperty.all(Colors.blueGrey[900]), surfaceTintColor: Colors.transparent, // 可选:去掉默认的浅色遮罩,让背景色完全显示出来 )
给所有AppBar统一改颜色
如果想让整个应用的AppBar都用这个颜色,就在全局主题里设置:
MaterialApp( theme: ThemeData( appBarTheme: const AppBarTheme( backgroundColor: MaterialStatePropertyAll(Colors.blueGrey[900]), surfaceTintColor: Colors.transparent, ), ), home: const YourHomePage(), // 替换成你的首页组件 )
内容的提问来源于stack exchange,提问作者Neetadev
相关产品推荐
相关产品推荐

