我希望能够在ThemeData中使用自定义颜色和主题。我知道我可以在一个单独的文件中定义它们,然后导入并使用它们。但我希望这些颜色在切换深色和浅色主题时也能起作用。
目前,我有独立的深色和浅色主题,我要切换它们。有没有办法在深色和浅色主题中分别定义自定义颜色?
AppTheme.dart:
class AppTheme {
AppTheme._();
// Light Theme
static final ThemeData lightTheme = ThemeData.light().copyWith(
appBarTheme: AppBarTheme(
color: Colors.grey[100],
brightness: Brightness.light,
),
scaffoldBackgroundColor: Colors.white,
actionButtonColor: Colors.black, // Like this
);
// Dark Theme
static final ThemeData darkTheme = ThemeData.dark().copyWith(
appBarTheme: AppBarTheme(
color: Colors.grey[850],
brightness: Brightness.dark,
),
scaffoldBackgroundColor: Colors.grey[900],
actionButtonColor: Colors.red, // Like this
);
}
App.dart
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Dashboard(),
theme: AppTheme.lightTheme,
darkTheme: AppTheme.darkTheme,
debugShowCheckedModeBanner: false,
);
}
}