Skip to content

Commit

Permalink
fix themes
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenDoXiu committed Jan 4, 2025
1 parent 9e97616 commit 5de190f
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 83 deletions.
10 changes: 10 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ class MyApp extends HookConsumerWidget {
var theme = ref.watch(themesProvider);
var mediaQueryData = MediaQuery.of(context);
mediaQueryData = mediaQueryData.copyWith(textScaler: TextScaler.linear(1));
// 获取 MediaQuery 的 platformBrightness
var platformBrightness = MediaQuery.platformBrightnessOf(context);
var systemBrightness = ref.watch(systemBrightnessProvider);
// 更新 Provider 的值
Future.delayed(Duration.zero, () {
ref
.read(systemBrightnessProvider.notifier)
.updateBrightness(platformBrightness);
});

// return MediaQuery(
// data: mediaQueryData,
// child: MaterialApp(
Expand Down
7 changes: 6 additions & 1 deletion lib/pages/home/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class HomePage extends HookConsumerWidget {
}));
return Scaffold(
key: _scaffoldKey,
backgroundColor: themes.bgColor,
backgroundColor: themes.isDark ? Colors.black : Colors.white,
resizeToAvoidBottomInset: false,
drawer: constraints.maxWidth < 500
? NavBar(
Expand Down Expand Up @@ -384,6 +384,7 @@ class UserAvatarButton extends ConsumerWidget {
var extend = constraints.maxWidth >= 100;
return ContextMenuBuilder(
menu: ContextMenuCard(
width: 250,
menuListBuilder: () async {
var list = ref.read(loginUserListProvider);
var user = ref.read(currentLoginUserProvider);
Expand Down Expand Up @@ -413,12 +414,14 @@ class UserAvatarButton extends ConsumerWidget {
Text(
"@${user?.userInfo.username ?? ""}@${Uri.parse(user?.serverUrl ?? "").host}",
style: const TextStyle(fontSize: 12),
maxLines: 1,
)
] else ...[
Text("@${user?.userInfo.username ?? ""}"),
Text(
Uri.parse(user?.serverUrl ?? "").host,
style: const TextStyle(fontSize: 12),
maxLines: 1,
)
]
],
Expand Down Expand Up @@ -468,12 +471,14 @@ class UserAvatarButton extends ConsumerWidget {
Text(
"@${item.userInfo.username}@${Uri.parse(item.serverUrl).host}",
style: const TextStyle(fontSize: 12),
maxLines: 1,
)
] else ...[
Text("@${item.userInfo.username}"),
Text(
Uri.parse(item.serverUrl).host,
style: const TextStyle(fontSize: 12),
maxLines: 1,
)
]
],
Expand Down
3 changes: 2 additions & 1 deletion lib/status/apis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ Future<MetaDetailedModel?> instanceMeta(InstanceMetaRef ref) async {
meta = await apis.meta.meta();
await instance.setMeta(meta!);
}
var brightness = ref.watch(systemBrightnessProvider);

colors.updateThemes(meta);
colors.updateThemes(meta, brightness);
return meta;
}

Expand Down
158 changes: 92 additions & 66 deletions lib/status/themes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';

import 'package:chinese_font_library/chinese_font_library.dart';
import 'package:flutter/material.dart';
import 'package:moekey/logger.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../apis/models/meta.dart';
Expand Down Expand Up @@ -49,81 +50,106 @@ class ThemeColors extends _$ThemeColors {
return ThemeColorModel();
}

updateThemes(MetaDetailedModel metas) {
updateThemes(MetaDetailedModel metas, Brightness brightness) {
var model = ThemeColorModel();
if (metas.defaultLightTheme != null) {
var themeData = jsonDecode(metas.defaultLightTheme!);
Map<String, dynamic> themes = themeData["props"];
model.isDark = themeData["base"] == "dark";
model.accentColor =
getThemesColor(themes["accent"] ?? "", themes) ?? model.accentColor;
model.bgColor =
getThemesColor(themes["bg"] ?? "", themes) ?? model.bgColor;
model.fgColor =
getThemesColor(themes["fg"] ?? "", themes) ?? model.fgColor;
if (metas.defaultLightTheme != null && metas.defaultDarkTheme != null) {
try {
// 获取系统是否是深色模式

model.accentLightenColor =
getThemesColor(themes["accentLighten"] ?? "", themes) ??
model.accentLightenColor;
model.accentedBgColor =
getThemesColor(themes["accentedBg"] ?? "", themes) ??
model.accentedBgColor;
model.panelColor =
getThemesColor(themes["panel"] ?? "", themes) ?? model.panelColor;
model.navBgColor =
getThemesColor(themes["navBg"] ?? "", themes) ?? model.navBgColor;
model.headerColor =
getThemesColor(themes["header"] ?? "", themes) ?? model.headerColor;
model.dividerColor =
getThemesColor(themes["divider"] ?? "", themes) ?? model.dividerColor;
model.buttonGradateAColor =
getThemesColor(themes["buttonGradateA"] ?? "", themes) ??
model.buttonGradateAColor;
model.buttonGradateBColor =
getThemesColor(themes["buttonGradateB"] ?? "", themes) ??
model.buttonGradateBColor;
model.fgOnAccentColor =
getThemesColor(themes["fgOnAccent"] ?? "", themes) ??
model.fgOnAccentColor;
model.reNoteColor =
getThemesColor(themes["reNote"] ?? "", themes) ?? model.reNoteColor;
var themeData = jsonDecode(metas.defaultLightTheme!);
if (brightness == Brightness.dark) {
model.isDark = true;
themeData = jsonDecode(metas.defaultDarkTheme!);
}
Map<String, dynamic> themes = themeData["props"];
// model.isDark = themeData["base"] == "dark";
model.accentColor =
getThemesColor(themes["accent"] ?? "", themes) ?? model.accentColor;
model.bgColor =
getThemesColor(themes["bg"] ?? "", themes) ?? model.bgColor;
model.fgColor =
getThemesColor(themes["fg"] ?? "", themes) ?? model.fgColor;

model.buttonBgColor = getThemesColor(themes["buttonBg"] ?? "", themes) ??
model.buttonBgColor;
model.buttonHoverBgColor =
getThemesColor(themes["buttonHover"] ?? "", themes) ??
model.buttonHoverBgColor;
model.mentionColor =
getThemesColor(themes["mention"] ?? "", themes) ?? model.mentionColor;
model.shadowColor =
getThemesColor(themes["shadow"] ?? "", themes) ?? model.shadowColor;
model.switchOffBgColor =
getThemesColor(themes["switchOffBg"] ?? "", themes) ??
model.switchOffBgColor;
model.switchOffFgColor =
getThemesColor(themes["switchOffFg"] ?? "", themes) ??
model.switchOffFgColor;
model.switchOnBgColor =
getThemesColor(themes["switchOnBg"] ?? "", themes) ??
model.switchOnBgColor;
model.switchOnFgColor =
getThemesColor(themes["switchOnFg"] ?? "", themes) ??
model.switchOnFgColor;
// Color shadowColor = const Color.fromARGB(25, 0, 0, 0);
// Color switchOffBgColor = const Color.fromARGB(25, 0, 0, 0);
// Color switchOffFgColor = const Color.fromARGB(255, 255, 255, 255);
// Color switchOnBgColor = const Color.fromARGB(255, 134, 179, 0);
// Color switchOnFgColor = const Color.fromARGB(255, 255, 255, 255);
// Color warnColor = const Color.fromARGB(255, 236, 182, 55);
// Color errorColor = const Color.fromARGB(255, 236, 55, 109);
// Color windowHeaderColor = const Color.fromARGB(216, 255, 255, 255);
model.accentLightenColor =
getThemesColor(themes["accentLighten"] ?? "", themes) ??
model.accentLightenColor;
model.accentedBgColor =
getThemesColor(themes["accentedBg"] ?? "", themes) ??
model.accentedBgColor;
model.panelColor =
getThemesColor(themes["panel"] ?? "", themes) ?? model.panelColor;
model.navBgColor =
getThemesColor(themes["navBg"] ?? "", themes) ?? model.navBgColor;
model.headerColor =
getThemesColor(themes["header"] ?? "", themes) ?? model.headerColor;
model.dividerColor = getThemesColor(themes["divider"] ?? "", themes) ??
model.dividerColor;
model.buttonGradateAColor =
getThemesColor(themes["buttonGradateA"] ?? "", themes) ??
model.buttonGradateAColor;
model.buttonGradateBColor =
getThemesColor(themes["buttonGradateB"] ?? "", themes) ??
model.buttonGradateBColor;
model.fgOnAccentColor =
getThemesColor(themes["fgOnAccent"] ?? "", themes) ??
model.fgOnAccentColor;
model.reNoteColor =
getThemesColor(themes["reNote"] ?? "", themes) ?? model.reNoteColor;

model.themeColor = model.accentColor;
model.buttonBgColor =
getThemesColor(themes["buttonBg"] ?? "", themes) ??
model.buttonBgColor;
model.buttonHoverBgColor =
getThemesColor(themes["buttonHover"] ?? "", themes) ??
model.buttonHoverBgColor;
model.mentionColor = getThemesColor(themes["mention"] ?? "", themes) ??
model.mentionColor;
model.shadowColor =
getThemesColor(themes["shadow"] ?? "", themes) ?? model.shadowColor;
model.switchOffBgColor =
getThemesColor(themes["switchOffBg"] ?? "", themes) ??
model.switchOffBgColor;
model.switchOffFgColor =
getThemesColor(themes["switchOffFg"] ?? "", themes) ??
model.switchOffFgColor;
model.switchOnBgColor =
getThemesColor(themes["switchOnBg"] ?? "", themes) ??
model.switchOnBgColor;
model.switchOnFgColor =
getThemesColor(themes["switchOnFg"] ?? "", themes) ??
model.switchOnFgColor;
// Color shadowColor = const Color.fromARGB(25, 0, 0, 0);
// Color switchOffBgColor = const Color.fromARGB(25, 0, 0, 0);
// Color switchOffFgColor = const Color.fromARGB(255, 255, 255, 255);
// Color switchOnBgColor = const Color.fromARGB(255, 134, 179, 0);
// Color switchOnFgColor = const Color.fromARGB(255, 255, 255, 255);
// Color warnColor = const Color.fromARGB(255, 236, 182, 55);
// Color errorColor = const Color.fromARGB(255, 236, 55, 109);
// Color windowHeaderColor = const Color.fromARGB(216, 255, 255, 255);

model.themeColor = model.accentColor;
} catch (e) {
logger.e(e);
}
}
state = model;
}
}

@riverpod
class SystemBrightness extends _$SystemBrightness {
@override
Brightness build() {
return Brightness.light;
}

updateBrightness(Brightness brightness) {
if (state == brightness) return;
state = brightness;
ref.notifyListeners();
}
}

@Riverpod(keepAlive: true)
class Themes extends _$Themes {
@override
Expand Down
12 changes: 11 additions & 1 deletion lib/utils/parse_color.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ Color parseColor(String colorString) {
if (colorString.toLowerCase().startsWith('#')) {
// 解析#xxxxxx格式
final String hex = colorString.substring(1);
if (hex.length == 8) {
// R G B A
final int red = int.parse(hex.substring(0, 2), radix: 16);
final int green = int.parse(hex.substring(2, 4), radix: 16);
final int blue = int.parse(hex.substring(4, 6), radix: 16);
final int alpha = int.parse(hex.substring(6, 8), radix: 16);

return Color.fromRGBO(red, green, blue, alpha / 255.0);
}
if (hex.length == 6) {
final int red = int.parse(hex.substring(0, 2), radix: 16);
final int green = int.parse(hex.substring(2, 4), radix: 16);
Expand Down Expand Up @@ -70,7 +79,8 @@ Color transformColor(

final hslColor = HSLColor.fromColor(color);

final lightness = hslColor.lightness + (lighten / 100.0) - (darken / 100.0);
final lightness = (hslColor.lightness + (lighten / 100.0) - (darken / 100.0))
.clamp(0.0, 1.0);
final saturation = hslColor.saturation * saturate;
final hueAngle = hslColor.hue + hue;

Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/mk_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class MkInput extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
var themes = ref.read(themeColorsProvider);
var themes = ref.watch(themeColorsProvider);
return Material(
color: themes.panelColor,
borderRadius: BorderRadius.circular(6),
Expand Down
30 changes: 18 additions & 12 deletions lib/widgets/mk_scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MkScaffold extends ConsumerWidget {
// logger.d(mediaQueryData);
var topPaddingHeight = mediaQueryData.padding.top;
var body = this.body;

var themes = ref.watch(themeColorsProvider);
return LayoutBuilder(
builder: (context, constraints) {
if (header != null) {
Expand All @@ -30,18 +30,24 @@ class MkScaffold extends ConsumerWidget {
child: body,
);
}
// 颜色与 RGBA(255,255,255,255) 叠加
// 叠加颜色
return Material(
child: Stack(
children: [
body,
if (header != null)
Positioned(
top: 0,
left: 0,
right: 0,
child: header!,
),
],
color: themes.isDark ? Colors.black : Colors.white,
child: Material(
color: themes.bgColor,
child: Stack(
children: [
body,
if (header != null)
Positioned(
top: 0,
left: 0,
right: 0,
child: header!,
),
],
),
),
);
},
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 0.8.5+56
version: 0.8.6+57

environment:
sdk: '>=3.5.0 <4.0.0'
Expand Down

0 comments on commit 5de190f

Please sign in to comment.