dreampad/lib/app/shared/utils/theme_utils.dart
2023-11-28 10:45:09 +08:00

59 lines
1.9 KiB
Dart
Raw 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:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../constants/constants.dart';
import 'device_utils.dart';
class ThemeUtils {
static bool isDark(BuildContext context) {
return Theme.of(context).brightness == Brightness.dark;
}
static Color? getDarkColor(BuildContext context, Color darkColor) {
return isDark(context) ? darkColor : null;
}
static Color? getIconColor(BuildContext context) {
return isDark(context) ? Colours.dark_text : null;
}
static Color getStickyHeaderColor(BuildContext context) {
return isDark(context) ? Colours.dark_bg_gray_ : Colours.bg_gray_;
}
static Color getDialogTextFieldColor(BuildContext context) {
return isDark(context) ? Colours.dark_bg_gray_ : Colours.bg_gray;
}
static Color? getKeyboardActionsColor(BuildContext context) {
return isDark(context) ? Colours.dark_bg_color : Colors.grey[200];
}
/// 设置StatusBar、NavigationBar样式。(仅针对安卓)
/// 本项目在android MainActivity中已设置不需要覆盖设置。
static void setSystemBarStyle({bool? isDark}) {
if (Device.isAndroid) {
final bool isDarkMode =
isDark ?? window.platformBrightness == Brightness.dark;
debugPrint('isDark: $isDarkMode');
final SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(
/// 透明状态栏
statusBarColor: Colors.transparent,
systemNavigationBarColor:
isDarkMode ? Colours.dark_bg_color : Colors.white,
systemNavigationBarIconBrightness:
isDarkMode ? Brightness.light : Brightness.dark,
);
SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
}
}
}
extension ThemeExtension on BuildContext {
bool get isDark => ThemeUtils.isDark(this);
Color get backgroundColor => Theme.of(this).scaffoldBackgroundColor;
Color get dialogBackgroundColor => Theme.of(this).canvasColor;
}