应用中切换深色主题和暗色主题是比较常见的操作,今天我们就来学习一下Flutter中动态的切换主题。

Simple Theme

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
} class _MyAppState extends State<MyApp> {
bool isLight = true; @override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.light().copyWith(
textTheme: TextTheme(bodyText2: TextStyle(fontSize: 20, color: Colors.red)),
),
darkTheme: ThemeData.dark().copyWith(
textTheme: TextTheme(bodyText2: TextStyle(fontSize: 20, color: Colors.blue)),
),
themeMode: isLight ? ThemeMode.light : ThemeMode.dark,
home: Scaffold(
appBar: AppBar(
title: Text("Theme"),
centerTitle: true,
actions: <Widget>[
IconButton(icon: Icon(Icons.repeat), onPressed: () => setState(() => isLight = !isLight)),
],
),
body: Center(child: Text("Hello World")),
),
);
}
}

这样我们可以通过点击Button来实现主题的动态切换,但是一旦我们重启应用,主题就会还原成深色。我们需要在切换主题的时候,保存我们现在的主题。可以使用shared_preferences库,以下是实现思路。

Save config in SharedPreference

  • add package denpendency
shared_preferences: ^0.5.7+3
  • final code
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart'; void main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences preferences = await SharedPreferences.getInstance();
final isLight = preferences.getBool("isLight") ?? true;
runApp(MyApp(isLight));
} class MyApp extends StatefulWidget {
final bool isLight; MyApp(this.isLight); @override
_MyAppState createState() => _MyAppState(isLight);
} class _MyAppState extends State<MyApp> {
bool isLight; _MyAppState(this.isLight); @override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.light().copyWith(
textTheme: TextTheme(bodyText2: TextStyle(fontSize: 20, color: Colors.red)),
),
darkTheme: ThemeData.dark().copyWith(
textTheme: TextTheme(bodyText2: TextStyle(fontSize: 20, color: Colors.blue)),
),
themeMode: isLight ? ThemeMode.light : ThemeMode.dark,
home: Scaffold(
appBar: AppBar(
title: Text("Theme"),
centerTitle: true,
actions: <Widget>[
IconButton(
icon: Icon(Icons.repeat),
onPressed: () async {
setState(() => isLight = !isLight);
SharedPreferences preferences = await SharedPreferences.getInstance();
preferences.setBool("isLight", isLight);
},
)
],
),
body: Center( child: Text("Hello World")),
),
);
}
}

Reference

video: https://v.youku.com/v_show/id_XNDczMTYyMDE4NA==.html

fluter usage---->动态更换Theme的更多相关文章

  1. 动态更换应用Icon

    转:原理1--activity-alias 在AndroidMainifest中,有两个属性: // 决定应用程序最先启动的Activity android.intent.action.MAIN // ...

  2. UGUI动态更换精灵图片

    //动态更换精灵图片 m_headimage.overrideSprite = Resources.Load("texture/"+info.HeadPortrait,typeof ...

  3. WPF通过DynamicResource实现给界面动态更换皮肤

    在我们的程序中有时候需要去实现动态更换皮肤的效果,从而完成一些个性化的设置,那么我们究竟怎样去实现动态换皮肤的效果呢?那么我们经常用到的就是设置不同的Style,并且在主程序的xaml文件中通过Dyn ...

  4. 【转】【iOS】动态更换App图标

    原文网址:http://www.cocoachina.com/ios/20170619/19557.html 前言 动态更换App图标这件事,在用户里总是存在需求的:有些用户喜欢“美化”自己的手机.至 ...

  5. js动态更换img的src问题

    在本地开发测试过程中,通过js动态更换img的src没有问题,图片正常切换,但是放在服务器上后测试发现,图片不显示,解决方法为:在对应onclick事件执行切换图片的js函数后加上一个return f ...

  6. FloatingActionButton动态更换背景色

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/186 FloatingActionButton 动态更换背 ...

  7. 空闲时间研究一个小功能:winform桌面程序如何实现动态更换桌面图标

    今天休息在家,由于天气热再加上疫情原因,就在家里呆着,空闲时想着,在很早以前(约3年前),产品人员跟我提了一个需求,那就是winform桌面程序的图标能否根据节日动态更换,这种需求在移动APP上还是比 ...

  8. Element-UI动态更换主题

    参考:vue-基于elementui换肤[自定义主题] 实践: 需求1.后期维护主题色不更换:  直接在线主题生成工具下载,在APP.VUE引入:(注意Element UI 版本1.3?2.0) 需求 ...

  9. 使用 css/less 动态更换主题色(换肤功能)

    前言 说起换肤功能,前端肯定不陌生,其实就是颜色值的更换,实现方式有很多,也各有优缺点 一.看需求是什么 一般来说换肤的需求分为两种: 1. 一种是几种可供选择的颜色/主题样式,进行选择切换,这种可供 ...

  10. Android 动态更换桌面图标

    每当双 11.12 来临之际,Android 手机 Launcher 中的淘宝.天猫图标就会变成双 11.12 主题的图标.实现了动态切换图标.名称 MainActivity package com. ...

随机推荐

  1. 医学分割 不确定性 2019 MICCAI

    z今天分享一篇发表在MICCAI 2019上的论文: Uncertainty-aware Self-ensembling Model for Semi-supervised 3D Left Atriu ...

  2. VirtualBox_Ubuntu22.10_Terminal无法打开

    https://blog.csdn.net/weixin_43959807/article/details/128872860

  3. 20200922--计算矩阵边缘元素之和(奥赛一本通P91 3二维数组)

    输入一个整数矩阵,计算位于矩阵边缘的元素之和.所谓矩阵边缘的元素,就是就一行和最后一行以及第一列和最后一列的元素. 输入; 第一行分别为矩阵的行数m和列数n(m<100,n<100),两者 ...

  4. C++从键盘读取一行的方法

    从键盘读取一行的方法 cin类中的成员函数getline()和get()--使用数组来处理字符串 cin.getline(数组,要读入的字符数).getline()将丢弃换行符.这个成员函数通过换行符 ...

  5. JAVA、Tomcat服务器

    JAVA如何配置服务器 Tomcat服务器: 1.Web开发中的常见概念: (1)B/S系统和C/S系统 Brower/Server:浏览器 服务器 系统 ----- 网站 Client/Server ...

  6. Nexus系列---【使用docker搭建nexus3仓库】

    1.Docker搭建nexus3私服 如果机器配置比较低,建议指定初始内存大小,默认2G docker run -d \ --restart=always \ --name=nexus3 \ -p 6 ...

  7. doy 20 系统优化

    系统优化 1.yum源的优化 CentOS   base   epel ​自建yum仓库​使用一个较为稳定的仓库​wget -O /etc/yum.repos.d/CentOS-Base.repo h ...

  8. unity shader 描边

    https://zhuanlan.zhihu.com/p/66282034   这个是将整个模型放大 在世界坐标操作 https://blog.csdn.net/ToToTofu/article/de ...

  9. vite+vue3使用unplugin-auto-import 无需手动引入api!

    近期了解到unplugin-auto-import这个插件 用途是无需每个组件内重复的引入vue vue-router等内置方法 下面举个例子 <script setup> import ...

  10. TCP 为什么是 三次 握手 不是两次 不是四次

    ​ 为什么不是两次 (1) 防止 历史 旧数据 连接 客户端连续发送多次 SYN 建⽴连接的报⽂,在⽹络拥堵等情况下: ● ⼀个「旧 SYN 报⽂」⽐「最新的 SYN 」 报⽂早到达了服务端: ● 那 ...