123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:get/get.dart';
- import '../../constants/color_constants.dart';
- class ThemeConfig {
- static ThemeData createTheme({
- required Brightness brightness,
- required Color background,
- required Color primaryText,
- Color? secondaryText,
- required Color accentColor,
- Color? divider,
- Color? buttonBackground,
- required Color buttonText,
- Color? cardBackground,
- Color? disabled,
- required Color error,
- }) {
- final baseTextTheme = brightness == Brightness.dark ? Typography.blackMountainView : Typography.whiteMountainView;
- return ThemeData(
- brightness: brightness,
- canvasColor: background,
- cardColor: background,
- dividerColor: divider,
- backgroundColor: background,
- primaryColor: accentColor,
- toggleableActiveColor: accentColor,
- errorColor: error,
- unselectedWidgetColor: hexToColor('#DADCDD'),
- dividerTheme: DividerThemeData(
- color: divider,
- space: 1,
- thickness: 1,
- ),
- cardTheme: CardTheme(
- color: cardBackground,
- margin: EdgeInsets.zero,
- clipBehavior: Clip.antiAliasWithSaveLayer,
- ),
- textSelectionTheme: TextSelectionThemeData(
- selectionColor: accentColor,
- selectionHandleColor: accentColor,
- cursorColor: accentColor,
- ),
- appBarTheme: AppBarTheme(
- color: cardBackground,
- titleTextStyle: TextStyle(
- color: secondaryText,
- fontSize: 18,
- ),
- iconTheme: IconThemeData(
- color: secondaryText,
- ),
- ),
- iconTheme: IconThemeData(
- color: secondaryText,
- size: 16.0,
- ),
- buttonTheme: ButtonThemeData(
- textTheme: ButtonTextTheme.primary,
- colorScheme: ColorScheme(
- brightness: brightness,
- primary: accentColor,
- secondary: accentColor,
- surface: background,
- background: background,
- error: error,
- onPrimary: buttonText,
- onSecondary: buttonText,
- onSurface: buttonText,
- onBackground: buttonText,
- onError: buttonText,
- ),
- padding: const EdgeInsets.all(16.0),
- ),
- cupertinoOverrideTheme: CupertinoThemeData(
- brightness: brightness,
- primaryColor: accentColor,
- ),
- inputDecorationTheme: InputDecorationTheme(
- errorStyle: TextStyle(color: error),
- labelStyle: TextStyle(
- fontFamily: 'Rubik',
- fontWeight: FontWeight.w600,
- fontSize: 16.0,
- color: primaryText.withOpacity(0.5),
- ),
- hintStyle: TextStyle(
- color: secondaryText,
- fontSize: 13.0,
- fontWeight: FontWeight.w300,
- ),
- ),
- textTheme: TextTheme(
- headline1: baseTextTheme.headline1!.copyWith(
- color: primaryText,
- fontSize: 34.0,
- fontWeight: FontWeight.bold,
- ),
- headline2: baseTextTheme.headline2!.copyWith(
- color: primaryText,
- fontSize: 22,
- fontWeight: FontWeight.bold,
- ),
- headline3: baseTextTheme.headline3!.copyWith(
- color: secondaryText,
- fontSize: 20,
- fontWeight: FontWeight.w600,
- ),
- headline4: baseTextTheme.headline4!.copyWith(
- color: primaryText,
- fontSize: 18,
- fontWeight: FontWeight.w600,
- ),
- headline5: baseTextTheme.headline5!.copyWith(
- color: primaryText,
- fontSize: 16,
- fontWeight: FontWeight.w700,
- ),
- headline6: baseTextTheme.headline6!.copyWith(
- color: primaryText,
- fontSize: 14,
- fontWeight: FontWeight.w700,
- ),
- bodyText1: baseTextTheme.bodyText1!.copyWith(
- color: secondaryText,
- fontSize: 15,
- ),
- bodyText2: baseTextTheme.bodyText2!.copyWith(
- color: primaryText,
- fontSize: 12,
- fontWeight: FontWeight.w400,
- ),
- button: baseTextTheme.button!.copyWith(
- color: primaryText,
- fontSize: 12.0,
- fontWeight: FontWeight.w700,
- ),
- caption: baseTextTheme.caption!.copyWith(
- color: primaryText,
- fontSize: 11.0,
- fontWeight: FontWeight.w300,
- ),
- overline: baseTextTheme.overline!.copyWith(
- color: secondaryText,
- fontSize: 11.0,
- fontWeight: FontWeight.w500,
- ),
- subtitle1: baseTextTheme.subtitle1!.copyWith(
- color: primaryText,
- fontSize: 16.0,
- fontWeight: FontWeight.w700,
- ),
- subtitle2: baseTextTheme.subtitle2!.copyWith(
- color: secondaryText,
- fontSize: 11.0,
- fontWeight: FontWeight.w500,
- ),
- ),
- );
- }
- static ThemeData get lightTheme => createTheme(
- brightness: Brightness.light,
- background: ColorConstants.lightScaffoldBackgroundColor,
- cardBackground: ColorConstants.secondaryAppColor,
- primaryText: Colors.black,
- secondaryText: Colors.black,
- accentColor: ColorConstants.secondaryAppColor,
- divider: ColorConstants.secondaryAppColor,
- buttonBackground: Colors.black38,
- buttonText: ColorConstants.secondaryAppColor,
- disabled: ColorConstants.secondaryAppColor,
- error: Colors.red,
- );
- static ThemeData get darkTheme => createTheme(
- brightness: Brightness.dark,
- background: ColorConstants.darkScaffoldBackgroundColor,
- cardBackground: ColorConstants.secondaryDarkAppColor,
- primaryText: Colors.white,
- secondaryText: Colors.white,
- accentColor: ColorConstants.secondaryDarkAppColor,
- divider: Colors.black45,
- buttonBackground: Colors.white,
- buttonText: ColorConstants.secondaryDarkAppColor,
- disabled: ColorConstants.secondaryDarkAppColor,
- error: Colors.red,
- );
- //亮色模式模式下的状态栏与导航栏设置 (默认黑色状态栏文本,白色导航栏)
- static SystemUiOverlayStyle get systemUiOverlayStyleLightThemeBlack {
- return SystemUiOverlayStyle.light.copyWith(
- statusBarColor: Colors.transparent,
- //顶部导航栏的状态栏背景颜色
- statusBarBrightness: Brightness.light,
- //顶部状态栏颜色模式
- statusBarIconBrightness: Brightness.dark,
- //顶部状态栏Icon与文本颜色模式
- systemNavigationBarDividerColor: Colors.transparent,
- //底部导航栏分割线颜色
- systemNavigationBarColor: Colors.white,
- //底部导航栏背景颜色
- systemNavigationBarIconBrightness: Brightness.dark, //底部导航栏三大金刚键的颜色模式
- );
- }
- //亮色模式模式下的状态栏与导航栏设置 (设置白色状态栏文本,白色导航栏)
- static SystemUiOverlayStyle get systemUiOverlayStyleLightThemeWhite {
- return SystemUiOverlayStyle.light.copyWith(
- statusBarColor: Colors.transparent,
- statusBarBrightness: Brightness.dark,
- statusBarIconBrightness: Brightness.light,
- systemNavigationBarDividerColor: Colors.transparent,
- systemNavigationBarColor: Colors.white,
- systemNavigationBarIconBrightness: Brightness.dark,
- );
- }
- //黑暗模式下,统一使用白色状态栏文本,黑色导航栏
- static SystemUiOverlayStyle get systemUiOverlayStyleDarkTheme {
- return SystemUiOverlayStyle.dark.copyWith(
- statusBarColor: Colors.transparent,
- statusBarBrightness: Brightness.dark,
- statusBarIconBrightness: Brightness.light,
- );
- }
- // ===================================== 切换样式 ↓ =====================================
- //强制更新整个App的样式
- static void _updateTheme(bool isDark) {
- Future.delayed(const Duration(milliseconds: 500), () {
- Get.forceAppUpdate();
- if (isDark) {
- if (GetPlatform.isAndroid) {
- //指定全局页面的状态栏,导航栏等设置
- SystemChrome.setSystemUIOverlayStyle(ThemeConfig.systemUiOverlayStyleDarkTheme);
- }
- } else {
- if (GetPlatform.isAndroid) {
- //指定全局页面的状态栏,导航栏等设置
- SystemChrome.setSystemUIOverlayStyle(ThemeConfig.systemUiOverlayStyleLightThemeBlack);
- }
- }
- });
- }
- static void changeThemeLight() {
- //如果是深色模式才切换亮色模式
- Get.changeThemeMode(ThemeMode.light);
- Get.changeTheme(ThemeConfig.lightTheme);
- _updateTheme(false);
- }
- static void changeThemeDark() {
- //如果是亮色模式才切换深色模式
- Get.changeThemeMode(ThemeMode.dark);
- Get.changeTheme(ThemeConfig.darkTheme);
- _updateTheme(true);
- }
- }
|