使用静态主题使用themexension vs类的差异
Flutter 3已经熄灭了,我一直在尝试一下。
我已经使用了theeextension
yt Ref: https://www.youtube.com/watch?v=8-szcyzfvao
api参考: https://api.flutter.flutter.dev/flutter.dev/flutter.dev/flutter.材料/主题/扩展。html
及其伟大。但是,我开始意识到并问自己,如果我创建了一个带有static const
的类作为颜色,文本风格或任何相关主题的属性,我可以做相同的结果。
如果我应该使用theeexentensions
,有人可以启发我吗?
静态类方法:
// Setup
class AppColors {
static const primaryColor = Color(0xFFFFFF);
static const secondaryColor = Color(0xFFFFFF);
}
// Use case inside build
return Container(
child: Text('Hello world'),
color: AppColors.primaryColor,
)
themeextension方法
// Setup
class AppColors extends ThemeExtension<AppColors>{
final Color primaryColor;
final Color secondaryColor;
AppColors(this.primaryColor, this.secondaryColor);
// .
// ..
// ... some @overrides such as copyWith() and lerp()
}
// Use case inside build
final colors = Theme.of(context).extensions<AppColors>()!;
return Container(
child: Text('Hello world'),
color: colors.primaryColor,
)
,您可以在此处看到themeexension
在仅使用静态类以实现相同结果的设置。
Flutter 3 is out and I've been experimenting a little.
I have used the ThemeExtension
yt ref: https://www.youtube.com/watch?v=8-szcYzFVao
api ref: https://api.flutter.dev/flutter/material/ThemeData/extensions.html
and its great. However I'm starting to realize and ask myself I could have done the same result if I created a class with static const
as properties such as colors, textstyles or any related theming.
Can someone enlighten me if why I should use ThemeExtensions
instead?
Static class way:
// Setup
class AppColors {
static const primaryColor = Color(0xFFFFFF);
static const secondaryColor = Color(0xFFFFFF);
}
// Use case inside build
return Container(
child: Text('Hello world'),
color: AppColors.primaryColor,
)
ThemeExtension Way
// Setup
class AppColors extends ThemeExtension<AppColors>{
final Color primaryColor;
final Color secondaryColor;
AppColors(this.primaryColor, this.secondaryColor);
// .
// ..
// ... some @overrides such as copyWith() and lerp()
}
// Use case inside build
final colors = Theme.of(context).extensions<AppColors>()!;
return Container(
child: Text('Hello world'),
color: colors.primaryColor,
)
As you can see here setting up for ThemeExtension
is quite huge compared on using just the static classes for theme to achieve the same result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您应该使用
主题
和/或theeexension
因为:您可以使用主题(和themeextension)来使小部件具有反应性。如果更改了某些值(颜色,文本样式等...),它将通知所有侦听器,
构建
依赖小部件的方法将被派发。如果将
thememode
更改为Dark/Light,则所有应用程序都将对使用主题的窗口小部件进行反应。您可以获得范围的主题,因此,如果您需要一些小部件或屏幕来使用整个应用程序的不同主题。
您可以在多个应用程序之间共享
themedata
在所有应用程序中看起来都非常相似主题是保持一致的视觉模式
的好选择。
孩子/儿童小部件只需要知道主题,这是一个真理的来源。如果您需要更改整个应用程序的所有屏幕按钮的颜色,那么您将在一个地方安全且乐于这样做:)
您无需使用
themeexension
获得所有这些提到的好处,但是如果您这样做,则将非常好的文档设计系统作为代码变得更加简单。I think you should use
Theme
and/orThemeExtension
because:You can use Theme(and ThemeExtension) in order to get widgets reactive. If some value is changed(color, text style, etc...), it will notify all listeners and the
build
method of dependent widgets will be dispatched.If you change the
themeMode
to dark/light, all application will react to update widgets that are using the theme.You can get scoped theme, so if you need some widgets or screen to use a different theme from the entire application, it will be possible too.
You can share you
ThemeData
between multiple apps and his use will looks like very similar in all appsTheme is a good choice to keep a consistent visual pattern
The child/children widgets just need to know the Theme, as a Single source of truth. If you need to change all screens button's color of the entire app, you will be safe and happy to do that in a single place :)
You don't need to use
ThemeExtension
to get all these mentioned benefits, but if you do, it will make simpler to keep a very good documented Design System as code.