应用中切换深色主题和暗色主题是比较常见的操作,今天我们就来学习一下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. Day11-数组的使用

    数组的使用 一.数组的使用 package com.array; public class ArrayDemo03 { public static void main(String[] args) { ...

  2. 第一讲:selenium快速入门

    一.selenium目前住主流的web自动化测试框架: 1.资料丰富 资料丰富       2.测试岗位招聘要求,上板率非常之高       3.支持多语言(iava/ pythan/ go /js) ...

  3. 通用CSS命名惯例

    通用的 CSS 命名惯例 在参与规模庞大.历时漫长.且参与人数众多的项目时,要确保每一行代码都像是同一个人编写的:这就要求所有开发者,都遵守相同的代码规范.在先前的文章前端项目开发规范意见,从宏观角度 ...

  4. jmeter dubbo测试

    一.环境准备 1.安装jmeter 2.安装dubbo插件,下载地址jmeter-plugins-dubbo, 将jar包放入${JMETER_HOME}\lib\ext路径下,重启即可 二.添加一个 ...

  5. 实验8 python库应用编程与体验

    3.实验任务3 from turtle import * def square(size=50, rgb='orange'): '''绘制正方形 参数size指定边长 参数rgb指定画笔颜色 如果没有 ...

  6. axis2 WebService 请求参数xml格式

    方法定义 public class GetBillInfoService { public String getBillList(String xmlData, String temp ){} 传入接 ...

  7. Collectors类的静态工厂方法-《Java 8实战》笔记

  8. kvm虚拟机添加在线快照

    1-1.创建默认快照--一般为一串数字 virsh snapshot-create 虚拟机名称 1-2.创建自定义名称快照 virsh snapshot-create-as 虚拟机名称 快照名称 2. ...

  9. redis基础-redis事务

    学习总结 原文:https://juejin.im/post/5d29ac845188252cc75e2d5c redis事务: redis是否有事务? redis是有事务的.命令如下: Redis事 ...

  10. [转]sublime text 4注册

    1.打开浏览器进入网站https://hexed.it2.打开sublime text4安装目录选择文件sublime_text.exe3.搜索80 78 05 00 0f 94 c1更改为c6 40 ...