59 lines
1.9 KiB
Dart
59 lines
1.9 KiB
Dart
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;
|
||
}
|