Flutter 如何将颤振中的所有设计元素隔离到一个主题数据类。。。你有什么策略和解决方案吗?
如何将颤振中的所有设计元素隔离到一个主题数据类文件中。颜色、装饰、配色方案。我尝试将我的代码分离到设计、页面、业务/类似于 myThemeData文件:Flutter 如何将颤振中的所有设计元素隔离到一个主题数据类。。。你有什么策略和解决方案吗?,flutter,dart,Flutter,Dart,如何将颤振中的所有设计元素隔离到一个主题数据类文件中。颜色、装饰、配色方案。我尝试将我的代码分离到设计、页面、业务/类似于 myThemeData文件: import 'package:flutter/material.dart'; class MyTheme { final BuildContext context; MyTheme({this.context}); ColorScheme get myColorSheme { return Theme.of(cont
import 'package:flutter/material.dart';
class MyTheme {
final BuildContext context;
MyTheme({this.context});
ColorScheme get myColorSheme {
return Theme.of(context).colorScheme.copyWith(
primary: Colors.red,
onPrimary: Colors.black,
primaryVariant: Colors.orange,
background: Colors.red,
onBackground: Colors.black,
secondary: Colors.red,
onSecondary: Colors.white,
secondaryVariant: Colors.deepOrange,
error: Colors.black,
onError: Colors.white,
surface: Colors.white,
onSurface: Colors.black,
brightness: Brightness.light,
);
}
ThemeData get myThemeData {
return ThemeData(
buttonTheme: ButtonThemeData(
textTheme: ButtonTextTheme.primary,
),
hintColor: Colors.red,
brightness: Brightness.dark,
colorScheme: myColorSheme,
);
}
static InputDecoration inputDecoration({String hintText, Icon icon}) {
return InputDecoration(
hintStyle: MyTheme.hintTextStyle,
hintText: hintText,
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white, width: 3),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white54, width: 1),
),
prefixIcon:
IconTheme(data: IconThemeData(color: Colors.white), child: icon),
);
}
static TextStyle get hintTextStyle {
return TextStyle(
fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white30);
}
static TextStyle get textFieldStyle {
return TextStyle();
}
static TextStyle get logoTextStyle {
return TextStyle(
fontSize: 40,
fontWeight: FontWeight.bold,
);
}
}
结果我犯了错误
════════ widgets库捕获到异常═══════════════════════════════════
生成MyApp时引发了以下断言(脏,状态:_MyAppState#84f70):
断言失败:
C:\…\material\theme\u数据。省道:316
colorScheme?.brightness==null | | brightness==null | | colorScheme!。亮度==亮度
事实并非如此
导致错误的相关小部件已被删除
MyApp
lib\main.dart:10
当抛出异常时,这是堆栈
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart sdk/lib/internal/js_dev_runtime/private/ddc_runtime/errors.dart 236:49
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 29:3 assertFailed
包装/颤振/src/材料/主题_数据。dart 316:106新
包/服务/UI/Theme.dart 27:12获取数据
包/服务/main.dart 24:38构建
...
════════════════════════════════════════════════════════════════════════════════ 在定义自己的主题数据时,最好使用
copyWith
方法。大多数情况下,use不会使用所有字段,有些字段是必填字段
那就这样做吧
For Dark theme
ThemeData get myThemeData {
return ThemeData.dark().copyWith()(
buttonTheme: ButtonThemeData(
textTheme: ButtonTextTheme.primary,
),
hintColor: Colors.red,
brightness: Brightness.dark,
colorScheme: myColorSheme,
);
}
For Light theme
ThemeData get myThemeData {
return ThemeData.light().copyWith()(
buttonTheme: ButtonThemeData(
textTheme: ButtonTextTheme.primary,
),
hintColor: Colors.red,
brightness: Brightness.dark,
colorScheme: myColorSheme,
);
}
您还可以看到我如何在我的一个项目中使用主题数据,请参见下文
我的黑色/黑色主题
ThemeData blackTheme(BuildContext context) {
return ThemeData.dark().copyWith(
snackBarTheme: SnackBarThemeData(
backgroundColor: selectedPrimaryColor,
actionTextColor: greyColor,
contentTextStyle: const TextStyle(color: Colors.white),
shape: const ContinuousRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
),
),
accentColor: selectedPrimaryColor,
cardColor: Colors.black,
primaryColor: Colors.black,
brightness: Brightness.dark,
primaryColorLight: Colors.black,
scaffoldBackgroundColor: Colors.black,
dialogBackgroundColor: Colors.black,
canvasColor: Colors.black,
floatingActionButtonTheme: FloatingActionButtonThemeData(
foregroundColor: Colors.white,
backgroundColor: selectedPrimaryColor,
),
dialogTheme: DialogTheme(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
textSelectionTheme: TextSelectionThemeData(
cursorColor: selectedPrimaryColor,
selectionHandleColor: selectedPrimaryColor,
selectionColor: darken(selectedPrimaryColor, 50),
),
textTheme: const TextTheme(
headline5: TextStyle(color: Colors.white),
headline1: TextStyle(color: Colors.white),
headline2: TextStyle(color: Colors.white),
bodyText1: TextStyle(color: Colors.white),
bodyText2: TextStyle(color: Colors.white),
caption: TextStyle(color: Colors.white),
subtitle1: TextStyle(color: Colors.white),
subtitle2: TextStyle(color: Colors.white),
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return greyColor;
}
return selectedPrimaryColor; // Defer to the widget's default.
},
),
/* elevation: MaterialStateProperty.resolveWith<double>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return 0;
}
return 0; // Defer to the widget's default.
},
),*/
foregroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return greyColor;
}
return selectedPrimaryColor; // Defer to the widget's default.
},
),
),
),
bottomSheetTheme: const BottomSheetThemeData(
backgroundColor: Colors.black,
),
textButtonTheme: TextButtonThemeData(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return greyColor;
}
return Colors.white; // Defer to the widget's default.
},
),
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return greyColor;
}
return Colors.black; // Defer to the widget's default.
},
),
),
),
);
}
主题数据blackTheme(构建上下文上下文){
返回主题数据.dark().copyWith(
snackBarTheme:SnackBarThemeData(
背景颜色:选择PrimaryColor,
actionTextColor:greyColor,
contentTextStyle:const TextStyle(颜色:Colors.white),
形状:常量连续矩形边框(
borderRadius:仅限borderRadius(
左上:半径。圆形(10),
右上角:半径。圆形(10),
),
),
),
accentColor:selectedPrimaryColor,
cardColor:Colors.black,
原色:颜色。黑色,
亮度:亮度。暗,
primaryColorLight:颜色。黑色,
脚手架背景颜色:颜色。黑色,
dialogBackgroundColor:Colors.black,
画布颜色:颜色。黑色,
floatingActionButtonTheme:floatingActionButtonTheme数据(
前底色:颜色。白色,
背景颜色:选择PrimaryColor,
),
对话主题:对话主题(
形状:圆形矩形边框(
边界半径:边界半径。圆形(10),
),
),
text选择主题:text选择主题数据(
cursorColor:selectedPrimaryColor,
selectionHandleColor:selectedPrimaryColor,
selectionColor:变暗(selectedPrimaryColor,50),
),
textTheme:const textTheme(
标题行5:TextStyle(颜色:Colors.white),
标题1:TextStyle(颜色:Colors.white),
标题2:TextStyle(颜色:Colors.white),
bodyText1:TextStyle(颜色:Colors.white),
bodyText2:TextStyle(颜色:Colors.white),
描述:TextStyle(颜色:Colors.white),
字幕1:TextStyle(颜色:Colors.white),
字幕2:TextStyle(颜色:Colors.white),
),
elevatedButtonTheme:elevatedButtonTheme数据(
样式:钮扣样式(
backgroundColor:MaterialStateProperty.resolveWith(
(设定状态){
if(states.contains(MaterialState.disabled)){
返回灰色;
}
返回selectedPrimaryColor;//遵从小部件的默认设置。
},
),
/*立面:MaterialStateProperty.resolveWith(
(设定状态){
if(states.contains(MaterialState.disabled)){
返回0;
}
返回0;//遵从小部件的默认设置。
},
),*/
foregroundColor:MaterialStateProperty.resolveWith(
(设定状态){
if(states.contains(MaterialState.disabled)){
返回灰色;
}
返回selectedPrimaryColor;//遵从小部件的默认设置。
},
),
),
),
bottomSheetTheme:const BottomSheetThemeData(
背景颜色:Colors.black,
),
textButtonTheme:textButtonTheme数据(
样式:钮扣样式(
foregroundColor:MaterialStateProperty.resolveWith(
(设定状态){
if(states.contains(MaterialState.disabled)){
返回灰色;
}
return Colors.white;//遵从小部件的默认设置。
},
),
backgroundColor:MaterialStateProperty.resolveWith(
(设定状态){
if(states.contains(MaterialState.disabled)){
返回灰色;
}
return Colors.black;//遵从小部件的默认设置。
},
),
),
),
);
}
我的灯光主题
ThemeData lightTheme(BuildContext context) {
// debugPrint(selectedPrimaryColor.toString());
return ThemeData.light().copyWith(
canvasColor: Colors.white,
cardColor: const Color.fromARGB(255, 255, 255, 255),
accentColor: selectedPrimaryColor,
toggleableActiveColor: selectedPrimaryColor,
primaryColor: selectedPrimaryColor,
dialogTheme: const DialogTheme(
titleTextStyle: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
)),
snackBarTheme: SnackBarThemeData(
backgroundColor: selectedPrimaryColor,
actionTextColor: Colors.white,
contentTextStyle: const TextStyle(
color: Colors.white,
),
shape: const ContinuousRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
),
),
brightness: Brightness.light,
bottomSheetTheme: BottomSheetThemeData(
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
textTheme: TextTheme(
headline5: const TextStyle(color: Colors.black),
headline1: const TextStyle(color: Colors.black),
headline2: const TextStyle(color: Colors.black),
bodyText1: const TextStyle(color: Colors.black),
bodyText2: const TextStyle(color: Colors.black),
caption: const TextStyle(color: Colors.black),
subtitle1: const TextStyle(color: Colors.black),
subtitle2: TextStyle(color: Colors.grey[200]),
),
textSelectionTheme: TextSelectionThemeData(
cursorColor: selectedPrimaryColor,
selectionHandleColor: selectedPrimaryColor,
selectionColor: lighten(selectedPrimaryColor, 65),
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return greyColor;
}
return selectedPrimaryColor; // Defer to the widget's default.
},
),
foregroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return Colors.black;
}
return selectedPrimaryColor; // Defer to the widget's default.
},
),
),
),
floatingActionButtonTheme:
Theme.of(context).floatingActionButtonTheme.copyWith(
backgroundColor: selectedPrimaryColor,
foregroundColor: Colors.white,
),
textButtonTheme: TextButtonThemeData(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return greyColor;
}
return selectedPrimaryColor;
},
),
),
),
);
}
ThemeData lightTheme(构建上下文){
//debugPrint(selectedPrimaryColor.toString());
返回主题数据.light().copyWith(
画布颜色:颜色。白色,
cardColor:const Color.fromARGB(255、255、255、255),
accentColor:selectedPrimaryColor,
toggleableActiveColor:selectedPrimaryColor,
primaryColor:selectedPrimaryColor,
dialogTheme:const dialogTheme(
titleTextStyle:TextStyle(
颜色:颜色,黑色,
fontWeight:fontWeight.bold,
)),
snackBarTheme:SnackBarThemeDa