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的使用。欢迎大家指正、讨论。

相关推荐
2501_9159184110 小时前
iOS 混淆与 IPA 加固一页式行动手册(多工具组合实战 源码成品运维闭环)
android·运维·ios·小程序·uni-app·iphone·webview
RollingPin13 小时前
iOS八股文之 网络
网络·网络协议·ios·https·udp·tcp·ios面试
彩旗工作室1 天前
将iOS/macOS应用上架至App Store
macos·ios·应用商店·appstore
消失的旧时光-19431 天前
Flutter Event Loop
flutter
程序员老刘1 天前
跨平台开发地图:客户端技术选型指南 | 2025年10月
flutter·react native·客户端
傅里叶1 天前
Flutter 工程环境、插件使用、protobuf配置与字体/持久化管理
flutter
傅里叶1 天前
Flutter之《环境与依赖配置》
flutter
大雷神1 天前
【成长纪实】HarmonyOS中ArkTS与Flutter数据类型对比详解
flutter
江东小bug王1 天前
深入解析 iOS 与 macOS 应用程序生命周期(完整指南)
macos·ios
2501_916008891 天前
iOS 发布全流程详解,从开发到上架的流程与跨平台使用 开心上架 发布实战
android·macos·ios·小程序·uni-app·cocoa·iphone