dark_theme_util.dart 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import 'package:flutter/material.dart';
  2. import '../my_load_image.dart';
  3. import 'package:cs_resources/constants/color_constants.dart';
  4. //设置颜色兼容黑色模式
  5. class DarkThemeUtil {
  6. // =================================== 黑暗模式下颜色值的处理 ↓ ===================================
  7. /// 如果想自定义黑暗模式下的颜色
  8. static Color? multiColors(BuildContext context, Color? lightColor, {Color? darkColor}) {
  9. Color? color;
  10. if (MediaQuery.of(context).platformBrightness == Brightness.dark) {
  11. color = darkColor ?? ColorConstants.darkScaffoldBackgroundColor;
  12. } else {
  13. color = lightColor;
  14. }
  15. return color;
  16. }
  17. // =================================== 黑暗模式下图片相关的处理 ↓ ===================================
  18. /// 默认黑暗模式下的颜色不变.
  19. /// 如果想自定义黑暗模式下的图标颜色填充颜色就行
  20. static MyAssetImage multiImageColorFit(BuildContext context, String imgPath, {double? width, double? height, Color? darkColor, BoxFit? fit}) {
  21. return MyAssetImage(
  22. imgPath,
  23. width: width,
  24. height: height,
  25. color: MediaQuery.of(context).platformBrightness == Brightness.dark ? darkColor : null,
  26. fit: fit,
  27. );
  28. }
  29. /// 默认黑暗模式下的图片资源不变
  30. /// 如果想自定义黑暗模式下的图片资源,可以直接替换图片
  31. static MyAssetImage multiImagePath(BuildContext context, String imgPath, {double? width, double? height, String? darkImagePath}) {
  32. return MyAssetImage(
  33. MediaQuery.of(context).platformBrightness == Brightness.dark && darkImagePath != null ? darkImagePath : imgPath,
  34. width: width,
  35. height: height,
  36. );
  37. }
  38. }