Flutter 日记App-日夜模式切换

get 配置

同样,使用get进行日夜模式开发,先配置.

javascript 复制代码
Widget build(BuildContext context) {
  return GetMaterialApp(
  //主题
    theme: sslUI.currentTheme,
  );
}

颜色配置

日夜模式枚举

scss 复制代码
enum SSLTheme{
  auto(0),
  light(1),
  dark(2);
   //初始化
  const SSLTheme(this.mode);
  final int mode;
  static SSLTheme getType(int mode){
    return SSLTheme.values.firstWhereOrNull((element) => element.mode == mode) ?? SSLTheme.auto;
  }
}

主题颜色类

php 复制代码
class SSLUIManager{
//这里写成单例类
SSLUIManager._internal(){
  listenPlatformModeChange();
}
//工厂模式初始化
static final SSLUIManager colorMan = SSLUIManager._internal();
factory SSLUIManager(){
  return colorMan;
}
以上是单例模式常用的初始化方式

全局的key 用于获取当前的context
  static GlobalKey<NavigatorState> sslNavKey = GlobalKey();
  static const scaffoldBackLight = Color.fromRGBO(220, 220, 220, 1);
  //APPBar主题色
  static const primaryLight = Colors.white;
  //分割线颜色
  static const dividerLight = Color.fromRGBO(230, 230, 230, 1);
  //主文本字体颜色
  static const mainTextLight = Color.fromRGBO(47, 47, 47, 1);
  //二级文本颜色
  static const subTextLight = Color.fromRGBO(153, 153, 153, 1);
  //底部TabBar背景色
  static const tabBackgroundLight = Colors.white;
  //底部TabBar Item 选中色
  static const tabItemSelLight = Color.fromRGBO(17, 17, 17, 1);
  //底部TabBar Item 未选中色
  static const tabItemNorLight = Color.fromRGBO(114, 114, 114, 1);

  static const scaffoldBackDark = Color.fromRGBO(17, 17, 17, 1);
...等等
  var themeMode = SSLTheme.auto;
  var showDarkMode = false;

  set localMode(int mode){
    themeMode = SSLTheme.getType(mode);
  }
   //获取系统当前的主题
  void listenPlatformModeChange(){
    Get.window.onPlatformBrightnessChanged = (){
      if (themeMode == SSLTheme.auto){
        Get.changeTheme(currentTheme);
      }
    };
  }
  ///获取主题色
  Color get priColor{
    if (showDarkMode){
      return primaryDark;
    }
    return primaryLight;
  }
  ///获取Widget主要背景色
  Color get scrollBgColor{
    if (showDarkMode){
      return scaffoldBackDark;
    }
    return scaffoldBackLight;
  }
  ...等等一些方法
 //配置两种主题对应的颜色
 //黑夜模式对应的主题色
static final darkTheme = ThemeData.dark().copyWith(
  scaffoldBackgroundColor: scaffoldBackDark,
    primaryColor: primaryDark,
    dividerColor: dividerDark,
  iconTheme: const IconThemeData(
    color: primaryDark
  ),
  //顶部导航颜色
  appBarTheme: const AppBarTheme(
    backgroundColor: primaryDark,
    titleTextStyle: TextStyle(color: mainTextDark, fontSize: 20)
  ),
  //底部tabbar 颜色
  bottomNavigationBarTheme: const BottomNavigationBarThemeData(
    backgroundColor: tabBackgroundDark,
    selectedItemColor: tabItemSelDark,
    unselectedItemColor: tabItemNorDark
  )
);
//浅色主题对应的主题色
static final lightTheme = ThemeData.light().copyWith(
    scaffoldBackgroundColor: scaffoldBackLight,
    primaryColor: primaryLight,//主题色
    dividerColor: dividerLight,//分割线颜色
    iconTheme: const IconThemeData(
    //icon颜色
        color: primaryLight
    ),
    //顶部Bar 设置
    appBarTheme: const AppBarTheme(
        backgroundColor: primaryLight,
        titleTextStyle: TextStyle(color: mainTextLight, fontSize: 20)
    ),
    //底部Bar设置
    bottomNavigationBarTheme: const BottomNavigationBarThemeData(
        backgroundColor: tabBackgroundLight,
        selectedItemColor: tabItemSelLight,
        unselectedItemColor: tabItemNorLight
    )

);

设置主题

ini 复制代码
void changeThemeMode(int mode) async {
  if (mode != themeMode.mode){
  //本地存储设置的主题类型
    bool success = await shareSetIntData(themeKey, mode);
    if (success){
    //设置当前的主题类型标识
      themeMode = SSLTheme.getType(mode);
      Get.changeTheme(currentTheme);
    }
  }
}
//获取当前的主题
ThemeData get currentTheme{
  debugPrint("ssl current theme mode $themeMode");
  if (themeMode == SSLTheme.auto){
    if (Get.window.platformBrightness == Brightness.light){
      showDarkMode = false;
      return lightTheme;
    }
    showDarkMode = true;
    return darkTheme;
  }
  else if (themeMode == SSLTheme.light){
    showDarkMode = false;
    return lightTheme;
  }
  showDarkMode = true;
  return darkTheme;
}

大概就这些,基本上源码都在这里。整个功能实现较为简单,主要是一些API的使用。欢迎大家指正、讨论。

相关推荐
壹方秘境9 小时前
我用Go语言开发了一个跨平台的HTTPS抓包和调试工具
前端·后端·ios
恋猫de小郭1 天前
Android 限制侧载新进展,谷歌联合国内厂商推验证计划
android·前端·flutter
恋猫de小郭1 天前
解读 Android 17 全新内存限制,有没有“豁免”后门?
android·前端·flutter
程序员老刘4 天前
跨平台开发地图 | 2026年6月
flutter·ai编程·客户端
悟空瞎说5 天前
Flutter 架构详解:新手必懂底层原理
flutter
SoaringHeart5 天前
Flutter最佳实践:IM聊天文字链接自动识别跳转
前端·flutter
恋猫de小郭5 天前
KMP / CMP 鸿蒙版本 Beta 发布,他有什么特别之处?
android·前端·flutter
风华圆舞6 天前
Flutter + 鸿蒙 Intents Kit:页面直达能力的完整接入方案
flutter·ui·华为·harmonyos
韩曙亮6 天前
【Flutter】Flutter 组件 ④ ( 组件渲染 的 三棵树理论 | Widget 树 → Element 树 → RenderObject 树 )
flutter·element·widget·renderobject
初级代码游戏6 天前
easy Photo Clean公测版:快速清理iPhone照片 邀请公测
ios·iphone