dark_theme_util.dart 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. /// 默认黑暗模式下的颜色为[ColorConstants.darkScaffoldBackgroundColor].
  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. static MyAssetImage multiImageColorFit(BuildContext context, String imgPath, {double? width, double? height, Color? darkColor, BoxFit? fit}) {
  20. return MyAssetImage(
  21. imgPath,
  22. width: width,
  23. height: height,
  24. color: MediaQuery.of(context).platformBrightness == Brightness.dark ? darkColor : null,
  25. fit: fit,
  26. );
  27. }
  28. /// 默认黑暗模式下的图片资源不变
  29. /// 如果想自定义黑暗模式下的图片资源,可以直接替换图片
  30. static MyAssetImage multiImagePath(BuildContext context, String imgPath, {double? width, double? height, String? darkImagePath}) {
  31. return MyAssetImage(
  32. MediaQuery.of(context).platformBrightness == Brightness.dark && darkImagePath != null ? darkImagePath : imgPath,
  33. width: width,
  34. height: height,
  35. );
  36. }
  37. }