在带有接口的 Dart 中的 Flutter class 上设置强制变量
Set mandatory variable on Flutter class in Dart with interface
正在为新的 Flutter 项目设置多个主题。我正在寻找通过实现接口或想法中的东西来更一致地做到这一点。
我目前有这个class
/// Theme constant class
class AppTheme{
AppTheme._();
/// The default blue app theme
static final ThemeData blueTheme = ThemeData(
primaryColor: Colors.blue,
backgroundColor: Colors.black,
);
/// Brown theme
static final ThemeData brownTheme = ThemeData(
primaryColor: Colors.green,
);
/// Elevation
static final List<BoxShadow> elevation = [
const BoxShadow(
color: Colors.black12,
offset: Offset(0, 9),
blurRadius: 28,
spreadRadius: 8,
),
const BoxShadow(
color: Colors.black12,
offset: Offset(0, 6),
blurRadius: 16,
),
const BoxShadow(
color: Colors.black12,
offset: Offset(0, 3),
blurRadius: 6,
spreadRadius: -4,
),
];
}
我正在寻找一种方法来定义实现相同变量的 blueTheme 和 brownTheme。
在这里我们可以看到 blueTheme 有 backgroundColor 但没有 brown,这是我的问题。
这里有什么解决方案可以在创建新的 ThemeData(例如新的 greenTheme 变量)时设置强制字段的模板吗?
PS:我在这里留下海拔是为了向您展示可以有其他类型而不仅仅是 ThemeDatas
您可以在不想为应用程序使用颜色的地方使用 Colors.transparent。
示例:`/// 主题常数 class
class AppTheme{
AppTheme._();
/// The default blue app theme
static final ThemeData blueTheme = ThemeData(
primaryColor: Colors.blue,
backgroundColor: Colors.black,
);
/// Brown theme
static final ThemeData brownTheme = ThemeData(
primaryColor: Colors.green,
backgroundColor: Colors.transparent,
);
/// Elevation
static final List<BoxShadow> elevation = [
const BoxShadow(
color: Colors.black12,
offset: Offset(0, 9),
blurRadius: 28,
spreadRadius: 8,
),
const BoxShadow(
color: Colors.black12,
offset: Offset(0, 6),
blurRadius: 16,
),
const BoxShadow(
color: Colors.black12,
offset: Offset(0, 3),
blurRadius: 6,
spreadRadius: -4,
),
];
}
`
好的,所以今天我找到了解决方案
我创建了一个名为 AppThemeInterface 的新 class 并在 ThemeData 类型中设置并在构造函数中设置它。
在尝试了几种方法实现主题数据并重新创建工厂构造函数后,在编译过程中出现错误。
这是我得到的最直接的解决方案。
class AppThemeInterface {
late ThemeData theme;
AppThemeInterface({
required Color primaryColor,
required Color backgroundColor,
}) {
theme = ThemeData(
primaryColor: primaryColor,
backgroundColor: backgroundColor,
);
}
}
然后将 ThemeData 类型更改为我的 AppThemeInterface 并调用“主题”变量
class AppTheme{
AppTheme._();
/// The default blue app theme
static final ThemeData blueTheme = AppThemeInterface ( // <=== here
primaryColor: Colors.blue,
backgroundColor: Colors.black,
).theme;// <=== here
/// Brown theme
static final ThemeData brownTheme = AppThemeInterface ( // <=== here
primaryColor: Colors.green,
backgroundColor: Colors.transparent,
).theme; // <=== here
}
我不确定我的命名,但它按我的预期工作,我希望它能帮助其他需要为 ThemeData 设置必填字段的人,这样他们在创建多个主题时就不会忘记变量。
正在为新的 Flutter 项目设置多个主题。我正在寻找通过实现接口或想法中的东西来更一致地做到这一点。
我目前有这个class
/// Theme constant class
class AppTheme{
AppTheme._();
/// The default blue app theme
static final ThemeData blueTheme = ThemeData(
primaryColor: Colors.blue,
backgroundColor: Colors.black,
);
/// Brown theme
static final ThemeData brownTheme = ThemeData(
primaryColor: Colors.green,
);
/// Elevation
static final List<BoxShadow> elevation = [
const BoxShadow(
color: Colors.black12,
offset: Offset(0, 9),
blurRadius: 28,
spreadRadius: 8,
),
const BoxShadow(
color: Colors.black12,
offset: Offset(0, 6),
blurRadius: 16,
),
const BoxShadow(
color: Colors.black12,
offset: Offset(0, 3),
blurRadius: 6,
spreadRadius: -4,
),
];
}
我正在寻找一种方法来定义实现相同变量的 blueTheme 和 brownTheme。
在这里我们可以看到 blueTheme 有 backgroundColor 但没有 brown,这是我的问题。
这里有什么解决方案可以在创建新的 ThemeData(例如新的 greenTheme 变量)时设置强制字段的模板吗?
PS:我在这里留下海拔是为了向您展示可以有其他类型而不仅仅是 ThemeDatas
您可以在不想为应用程序使用颜色的地方使用 Colors.transparent。
示例:`/// 主题常数 class
class AppTheme{
AppTheme._();
/// The default blue app theme
static final ThemeData blueTheme = ThemeData(
primaryColor: Colors.blue,
backgroundColor: Colors.black,
);
/// Brown theme
static final ThemeData brownTheme = ThemeData(
primaryColor: Colors.green,
backgroundColor: Colors.transparent,
);
/// Elevation
static final List<BoxShadow> elevation = [
const BoxShadow(
color: Colors.black12,
offset: Offset(0, 9),
blurRadius: 28,
spreadRadius: 8,
),
const BoxShadow(
color: Colors.black12,
offset: Offset(0, 6),
blurRadius: 16,
),
const BoxShadow(
color: Colors.black12,
offset: Offset(0, 3),
blurRadius: 6,
spreadRadius: -4,
),
];
}
`
好的,所以今天我找到了解决方案
我创建了一个名为 AppThemeInterface 的新 class 并在 ThemeData 类型中设置并在构造函数中设置它。
在尝试了几种方法实现主题数据并重新创建工厂构造函数后,在编译过程中出现错误。
这是我得到的最直接的解决方案。
class AppThemeInterface {
late ThemeData theme;
AppThemeInterface({
required Color primaryColor,
required Color backgroundColor,
}) {
theme = ThemeData(
primaryColor: primaryColor,
backgroundColor: backgroundColor,
);
}
}
然后将 ThemeData 类型更改为我的 AppThemeInterface 并调用“主题”变量
class AppTheme{
AppTheme._();
/// The default blue app theme
static final ThemeData blueTheme = AppThemeInterface ( // <=== here
primaryColor: Colors.blue,
backgroundColor: Colors.black,
).theme;// <=== here
/// Brown theme
static final ThemeData brownTheme = AppThemeInterface ( // <=== here
primaryColor: Colors.green,
backgroundColor: Colors.transparent,
).theme; // <=== here
}
我不确定我的命名,但它按我的预期工作,我希望它能帮助其他需要为 ThemeData 设置必填字段的人,这样他们在创建多个主题时就不会忘记变量。