Flutter 通过在Flatter应用程序中应用主题数据,Appbar颜色、工具栏颜色、文本、字体和按钮不会改变
例如,若要在按钮上应用主题,我正在执行此操作,但主题在按钮上不起作用Flutter 通过在Flatter应用程序中应用主题数据,Appbar颜色、工具栏颜色、文本、字体和按钮不会改变,flutter,flutter-theme,Flutter,Flutter Theme,例如,若要在按钮上应用主题,我正在执行此操作,但主题在按钮上不起作用 @override Widget build(BuildContext context) { return MaterialApp( title: 'Personal Expenses', theme: ThemeData( brightness: Brightness.light, primaryColor: Colors.lightGreen
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Personal Expenses',
theme: ThemeData(
brightness: Brightness.light,
primaryColor: Colors.lightGreen[800],
primarySwatch: Colors.amber,
errorColor: Colors.red,
accentColor: Colors.cyan[600],
fontFamily: 'Quicksand',
textTheme: ThemeData.light().textTheme.copyWith(
title: TextStyle(
fontFamily: 'Quicksand',
backgroundColor: Colors.grey,
fontSize: 18,
),
),
appBarTheme: AppBarTheme(
textTheme: ThemeData.light().textTheme.copyWith(
title: TextStyle(
fontFamily: 'OpenSans',
fontSize: 20,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic),
button: TextStyle(color: Colors.white),
),
)
),
home: MyHomePage(),
);
}
您可以在
AppBarTheme
下设置按钮文本颜色,而不是在textTheme
中设置按钮文本颜色,您可以在FlatButton
中引用该主题
这可能会解决你的问题
FlatButton(
color: Colors.white,
textColor: Theme.of(context).textTheme.button.color,
onPressed: _presentDatePicker,
child: Text(
'Choose Date',
style: TextStyle(fontWeight: FontWeight.bold),
),
)
或者你可以像这样设置按钮颜色
FlatButton(
color: Colors.white,
textColor: Theme.of(context).AppBarTheme.textTheme.button.color,
onPressed: _presentDatePicker,
child: Text(
'Choose Date',
style: TextStyle(fontWeight: FontWeight.bold),
),
)
theme: ThemeData(
primarySwatch: Colors.purple,
accentColor: Colors.amber,
buttonColor: Colors.white,
),
在平面按钮中使用该按钮颜色,如下所示
FlatButton(
color: Colors.white,
textColor: Theme.of(context).AppBarTheme.textTheme.button.color,
onPressed: _presentDatePicker,
child: Text(
'Choose Date',
style: TextStyle(fontWeight: FontWeight.bold),
),
)
theme: ThemeData(
primarySwatch: Colors.purple,
accentColor: Colors.amber,
buttonColor: Colors.white,
),