在复制accentColor
之后,我试图更改ThemeData.light()
,然后在此示例屏幕中显示FloatingActionButton
class Sample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
),
)}}
然后在main.dart
的主窗口小部件中调用runApp
,如果我将ThemeData
小部件设置为MaterialApp
,则FloatingActionButton
将具有橙色。
theme: ThemeData(
accentColor: Colors.orange
)
但是,如果我尝试从Themedata.light().copyWith
继承颜色,则FloatingActionButton
仍然会以浅色主题为蓝色。
theme: ThemeData.light().copyWith(
accentColor: Colors.orange
)
我期望FloatingActionButton
应该是橙色,因为它继承了light
主题并覆盖了accentColor
。
答案 0 :(得分:0)
看, 我认为您不能使用ThemeData.copyWith()继承强调色,但是如果您确定要使用ThemeData.copyWith()来更改您的floatActionButton颜色,那么您可以按以下方式进行操作,
theme:ThemeData.dark().copyWith(
textTheme:ThemeData.dark().textTheme.copyWith(
title :TextStyle( --your color and text configuratons here like color,font etc)
button: TextStyle(--do--),
...and so on....
)
)
您想要在默认文本标题中进行的配置将位于上述title属性的TextStyle内,与按钮相同
现在,您可以使用此功能在您的FAB内实现这一目标
color: Theme.of(context).textTheme.button.color,
通过执行此操作,您可以获取为ThemeData中的按钮设置的颜色。
如果您强制使用默认的配色,则必须使用
theme:ThemeData(
primaryColor: -----
accentColor : -----
)
这将允许您将默认强调色用于FAB
答案 1 :(得分:0)
这是Flutter中的常见问题,但是您可以通过执行以下操作暂时解决此问题:
theme: ThemeData.light().copyWith(
floatingActionButtonTheme:
ThemeData.light().floatingActionButtonTheme.copyWith(
backgroundColor: Colors.orange,
),
),
如果您使用了其他任何按钮,则应执行相同操作并覆盖其主题,
您可以在Updating the Material Buttons and their Themes
上了解有关此问题的更多信息和buttonColor not honored when using ThemeData.light().copyWith()