- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 ThemeData 中使用自定义颜色和主题。我知道我可以在单独的文件中定义它们并导入该文件并使用它们。但我希望这些颜色在更改深色和浅色主题时起作用。
目前,我有单独的深色和浅色主题可以切换。无论如何要分别在深色和浅色 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
);
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Dashboard(),
theme: AppTheme.lightTheme,
darkTheme: AppTheme.darkTheme,
debugShowCheckedModeBanner: false,
);
}
}
最佳答案
我认为您在自己的问题中已经有了答案,您在代码示例中所做的事情是正确的,并且是合法的。当然,“合法”一词是指代码将按预期工作并产生结果,当然,actionButtonColor
部分除外。属性,因为该属性不属于 ThemeData
api。
或者,如果您询问如何使用该自定义 actionButtonColor
你的主题数据中的属性,你不能。因为它是与 Material 主题一起使用的预定义小部件。那actionButton
应该只使用主题中的现有属性。但是如果你真的需要这样做,你可以使用 dart 2.7 中的实验性扩展方法,比如,
extension MyColorScheme on ColorScheme {
Color get actionButtonColor => const Colors.red;
}
ActionButtonComponent(
color: Theme.of(context).colorScheme.actionButtonColor,
);
ThemeData
设置不同的配色方案。与扩展方法一起使用时,例如深色和浅色主题。您可能想查看
this issue .
关于android - Flutter - 在 ThemeData 中使用自定义颜色和主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60693179/
如何在 ThemeData 中全局设置行高? theme: ThemeData( brightness: Brightness.light, accentColor: my
我试图找到以下问题的答案:“如何正确编码ThemeData的已贬值的'title'参数? (我正在学习一个教程)。但是,在其他主题的一些评论中,我找到了正确的答案。我将答案留在下面,以供将来引用和轻松
我刚刚在 flutter 中开始一个新项目,对它完全陌生。 在 Flutter 中定义主题的约定是什么? 我想要一个带有主题的单独文件,以保持 main.dart 简单。有好的/正确的/经典的方法吗?
我在 LinkedIn Learning 上关注来自 London App Brewery 的 BMI Calculator 应用程序。 尝试将primaryColor设置为红色时,即使我覆盖了Pri
代码: @override Widget build(BuildContext context) { return Theme( data: ThemeData(textTheme: Te
我很可能会错过一些东西,因为我对 Flutter 很陌生,但我发现 ThemeData 的选项非常有限(至少在我对如何实现它的理解的情况下)。 如果你从 MaterialUp 看下面这个随机设计,我想
我想在 ThemeData 中使用自定义颜色和主题。我知道我可以在单独的文件中定义它们并导入该文件并使用它们。但我希望这些颜色在更改深色和浅色主题时起作用。 目前,我有单独的深色和浅色主题可以切换。无
我知道您可以像这样用自己的字体定义一个新的 ThemeData 对象: ThemeData(fontFamily: 'Roboto') 但是,我想使用 copyWith 方法复制现有的 ThemeDa
我想为浅色主题定义一个 themeData,为深色主题定义另一个。 但我的问题是我不知道如何为每个 themeData 定义我自己的自定义颜色,例如 badgeColor、CarColor... 我搜
我要换 color基于主题的小部件。我有单独的 ThemeData用于明暗主题。现在可以将自定义属性添加到 ThemeData以便我可以根据主题并使用该自定义属性更改小部件的颜色? 最佳答案 可悲的是
我的应用程序是使用 MaterialApp 作为根小部件构建的。对于这个 Material 应用程序,我添加了一个主题如下: MaterialApp( theme: myTheme, home
我一直关注 Material Design 组件,在底部,每个组件都有一个主题部分。 这是ThemeData代码, final ThemeData base = ThemeData.light();
即使我设置了也会 flutter primaryColor: Colors.orange, 然后在 UI 的某些部分,默认的 Colors.blue 仍然存在。有没有办法为 ThemeData 全局覆
是否可以在 flutter ThemeData 的 backgroundColor 属性中设置渐变颜色而不是单一颜色? final ThemeData base = ThemeData.light()
我试图改变 accentColor复制 ThemeData.light() 后,然后我有一个带有 FloatingActionButton 的示例屏幕 class Sample extends Sta
我正在研究来自 flutter.dev 的 startup_namer 教程示例。以下是 MyApp 的定义: class MyApp extends StatelessWidget { @ove
我正在开发一个在所有屏幕上都使用大写文本的应用程序,我认为如果我可以添加这样的东西,它是生产有效的: ... return MaterialApp( title: '***',
我是一名优秀的程序员,十分优秀!