Files
DMZJ_F/lib/app/platform_utils.dart
2026-03-07 17:24:59 +08:00

52 lines
1.6 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:io' show Platform;
import 'package:fluent_ui/fluent_ui.dart' as fluent;
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
/// Windows平台检测与Fluent UI主题工具
class PlatformUtils {
/// Web平台不支持dart:io需先排除
static bool get isWindows => !kIsWeb && Platform.isWindows;
/// 根据当前Material主题生成对应的FluentThemeData
static fluent.FluentThemeData getFluentTheme(BuildContext context) {
final materialTheme = Theme.of(context);
final brightness = materialTheme.brightness;
final primary = materialTheme.colorScheme.primary;
// 根据primary color构建AccentColor渐变色阶
final accent = fluent.AccentColor.swatch({
'darkest': _darken(primary, 0.4),
'darker': _darken(primary, 0.2),
'dark': _darken(primary, 0.1),
'normal': primary,
'light': _lighten(primary, 0.15),
'lighter': _lighten(primary, 0.3),
'lightest': _lighten(primary, 0.5),
});
return fluent.FluentThemeData(
brightness: brightness,
accentColor: accent,
scaffoldBackgroundColor: brightness == Brightness.dark
? const Color(0xff202020)
: const Color(0xfff3f3f3),
);
}
static Color _darken(Color color, double amount) {
final hsl = HSLColor.fromColor(color);
return hsl
.withLightness((hsl.lightness - amount).clamp(0.0, 1.0))
.toColor();
}
static Color _lighten(Color color, double amount) {
final hsl = HSLColor.fromColor(color);
return hsl
.withLightness((hsl.lightness + amount).clamp(0.0, 1.0))
.toColor();
}
}