dreampad/lib/app/theme/theme_service.dart

83 lines
3.0 KiB
Dart
Raw Permalink Normal View History

2023-11-28 10:44:58 +08:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:dreampad/app/shared/constants/constants.dart';
class ThemeService {
void setTheme(ThemeMode themeMode) {
SpUtil.putString(Constant.theme, themeMode.value);
Get.changeThemeMode(themeMode);
}
ThemeMode getThemeMode() {
final String theme = SpUtil.getString(Constant.theme) ?? 'Light';
switch (theme) {
case 'Dark':
return ThemeMode.dark;
case 'Light':
return ThemeMode.light;
default:
return ThemeMode.system;
}
}
ThemeData getTheme({bool isDarkMode = false}) {
return ThemeData(
fontFamily: 'alph-m',
primaryColor: isDarkMode ? Colours.dark_app_main : Colours.app_main,
colorScheme: ColorScheme.fromSwatch().copyWith(
brightness: isDarkMode ? Brightness.dark : Brightness.light,
secondary: isDarkMode ? Colours.dark_app_main : Colours.app_main,
error: isDarkMode ? Colours.dark_red : Colours.red,
),
// Tab指示器颜色
indicatorColor: isDarkMode ? Colours.dark_app_main : Colours.app_main,
// 页面背景色
scaffoldBackgroundColor:
isDarkMode ? Colours.dark_bg_color : Colors.white,
// 主要用于Material背景色
canvasColor: isDarkMode ? Colours.dark_material_bg : Colors.white,
// 文字选择色(输入框选择文字等)
// 稳定版1.23 变更(https://flutter.dev/docs/release/breaking-changes/text-selection-theme)
textSelectionTheme: TextSelectionThemeData(
selectionColor: Colours.app_main.withAlpha(70),
selectionHandleColor: Colours.app_main,
cursorColor: Colours.app_main,
),
textTheme: TextTheme(
// TextField输入文字颜色
titleMedium: isDarkMode ? TextStyles.textDark : TextStyles.text,
// Text文字样式
bodyMedium: isDarkMode ? TextStyles.textDark : TextStyles.text,
titleSmall:
isDarkMode ? TextStyles.textDarkGray12 : TextStyles.textGray12,
),
inputDecorationTheme: InputDecorationTheme(
hintStyle:
isDarkMode ? TextStyles.textHint14 : TextStyles.textDarkGray14,
),
appBarTheme: AppBarTheme(
elevation: 0.0,
color: isDarkMode ? Colours.dark_bg_color : Colors.white,
systemOverlayStyle:
isDarkMode ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark,
),
dividerTheme: DividerThemeData(
color: isDarkMode ? Colours.dark_line : Colours.line,
space: 0.6,
thickness: 0.6),
cupertinoOverrideTheme: CupertinoThemeData(
brightness: isDarkMode ? Brightness.dark : Brightness.light,
),
visualDensity: VisualDensity
.standard, // https://github.com/flutter/flutter/issues/77142
);
}
}
extension ThemeModeExtension on ThemeMode {
String get value => <String>['System', 'Light', 'Dark'][index];
}