【发布时间】:2021-08-02 22:33:20
【问题描述】:
我从配色方案和文本主题创建了它们。这是我的主题文件。
import 'package:google_fonts/google_fonts.dart';
import 'package:intellaview_mobile/constants/constants.dart';
extension AppTextStyleVariants on TextStyle {
TextStyle get italic => copyWith(fontStyle: FontStyle.italic);
TextStyle get regular => copyWith(fontWeight: FontWeight.normal);
TextStyle get semiBold => copyWith(fontWeight: FontWeight.w600);
TextStyle get bold => copyWith(fontWeight: FontWeight.bold);
TextStyle colorize(Color color) => copyWith(color: color);
}
extension NamedTextStylesFromTextTheme on TextTheme {
TextStyle get listItem1 => headline4!;
TextStyle get listItem2 => headline5!;
TextStyle get button1 => button!;
TextStyle get button2 => headline6!;
}
class AppTheme {
static final TextTheme _appTextTheme = TextTheme(
headline1: GoogleFonts.openSans(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: AppColors.primary,
),
headline2: GoogleFonts.openSans(
fontWeight: FontWeight.bold,
fontSize: 16.0,
color: AppColors.primary,
),
headline3: GoogleFonts.openSans(
fontWeight: FontWeight.bold,
fontSize: 14.0,
color: AppColors.primary,
),
// listItem1
headline4: GoogleFonts.openSans(
// semiBold
fontWeight: FontWeight.normal,
fontSize: 16.0,
color: AppColors.primary,
),
// listItem2
headline5: GoogleFonts.openSans(
fontWeight: FontWeight.w600,
fontSize: 14.0,
color: AppColors.primary,
),
// button2
headline6: GoogleFonts.openSans(
fontWeight: FontWeight.bold,
fontSize: 12.0,
color: AppColors.primary,
),
subtitle1: GoogleFonts.openSans(
// semiBold
fontWeight: FontWeight.w600,
fontSize: 16.0,
color: AppColors.primary,
),
subtitle2: GoogleFonts.openSans(
// semiBold
fontWeight: FontWeight.w600,
fontSize: 12.0,
color: AppColors.primary,
),
bodyText1: GoogleFonts.openSans(
fontWeight: FontWeight.bold,
fontSize: 12.0,
color: AppColors.primary,
),
bodyText2: GoogleFonts.openSans(
fontWeight: FontWeight.normal,
fontSize: 12.0,
color: AppColors.primary,
),
button: GoogleFonts.openSans(
fontWeight: FontWeight.w600,
fontSize: 14.0,
color: AppColors.primary.shade500,
),
overline: GoogleFonts.openSans(
// semiBold
fontWeight: FontWeight.w600,
fontSize: 10.0,
color: AppColors.primary.shade500,
),
caption: GoogleFonts.openSans(
fontWeight: FontWeight.w600,
fontSize: 8.0,
color: AppColors.primary.shade300,
),
);
static final TextTheme _onPrimaryTextTheme = _appTextTheme.apply(
bodyColor: AppColors.onPrimary,
displayColor: AppColors.onPrimary,
);
static final ColorScheme _appColorScheme = ColorScheme(
brightness: Brightness.dark,
primary: AppColors.primary.shade800,
primaryVariant: AppColors.primary.shade500,
secondary: AppColors.secondary.shade500,
secondaryVariant: AppColors.secondary.shade800,
background: Colors.white,
surface: Colors.white,
error: AppColors.error,
onPrimary: Colors.white,
onSecondary: AppColors.primary.shade800,
onBackground: AppColors.primary.shade800,
onSurface: AppColors.primary.shade800,
onError: Colors.white,
);
static ThemeData? finalAppTheme;
static DividerThemeData _dividerThemeData(ThemeData themeData) {
return themeData.dividerTheme.copyWith(space: 1, thickness: 1);
}
static final AppBarTheme _appBarTheme = AppBarTheme(
color: AppColors.primary,
brightness: Brightness.dark,
iconTheme: IconThemeData(color: AppColors.onPrimary),
actionsIconTheme: IconThemeData(color: AppColors.onPrimary),
textTheme: _onPrimaryTextTheme,
titleTextStyle: GoogleFonts.openSans(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.white,
),
elevation: 4,
);
static ThemeData get app {
if (null == finalAppTheme) {
ThemeData themeData = ThemeData.from(
colorScheme: _appColorScheme,
textTheme: _appTextTheme,
);
finalAppTheme = themeData.copyWith(
buttonColor: themeData.colorScheme.secondary,
// selectedRowColor: AppColors.selectedRowColor,
appBarTheme: _appBarTheme,
dividerTheme: _dividerThemeData(themeData),
canvasColor: AppColors.primary,
disabledColor: AppColors.primary.shade100,
);
}
return finalAppTheme!;
}
}
我用那个主题创建了我的材料应用程序。
@override
Widget build(BuildContext context) {
return MaterialApp(
title: Strings.lang.kIntellaViewMobile,
theme: AppTheme.app,
initialRoute: LoginScreen.route,
routes: {
AddEditSwitch.route: (context) => AddEditSwitch(),
AlertsScreen.route: (context) => AlertsScreen(),
BladesScreen.route: (context) => BladesScreen(),
ConnectionsScreen.route: (context) => ConnectionsScreen(),
DashboardScreen.route: (context) => DashboardScreen(),
SwitchesScreen.route: (context) => SwitchesScreen(),
EnableBluetoothScreen.route: (context) => EnableBluetoothScreen(),
FactoryResetScreen.route: (context) => FactoryResetScreen(),
HealthCheckScreen.route: (context) => HealthCheckScreen(),
IntellaViewHomePage.route: (context) => IntellaViewHomePage(title: Strings.lang.kIntellaViewMobile),
LoginScreen.route: (context) => LoginScreen(),
MaintenanceScreen.route: (context) => MaintenanceScreen(),
SwitchInfoScreen.route: (context) => SwitchInfoScreen(),
UsersScreen.route: (context) => UsersScreen(),
},
navigatorObservers: [AppState.instance],
);
}
}
但是,我在意想不到的地方得到了奇怪的颜色和结果。
例如。当我的 Scaffold : Drawer 的孩子被重建时,它带有一个一切都是白色的主题。我的原色消失了。我的画布颜色消失了。颤动从哪里得到这些看似随机的?是否有指南告诉我颤振从哪里获取各种小部件的主题信息。我知道抽屉没有使用我的主题,那么抽屉主题是从哪里来的?
【问题讨论】:
-
Flutter 2.0.6 • 通道稳定 • github.com/flutter/flutter.git 框架 • 修订版 1d9032c7e1(13 天前) • 2021-04-29 17:37:58 -0700 引擎 • 修订版 05e680e202 工具 • Dart 2.12。 3
-
嗨!你能提供你的抽屉的代码吗?