fluter usage---->动态更换Theme
应用中切换深色主题和暗色主题是比较常见的操作,今天我们就来学习一下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的更多相关文章
- 动态更换应用Icon
转:原理1--activity-alias 在AndroidMainifest中,有两个属性: // 决定应用程序最先启动的Activity android.intent.action.MAIN // ...
- UGUI动态更换精灵图片
//动态更换精灵图片 m_headimage.overrideSprite = Resources.Load("texture/"+info.HeadPortrait,typeof ...
- WPF通过DynamicResource实现给界面动态更换皮肤
在我们的程序中有时候需要去实现动态更换皮肤的效果,从而完成一些个性化的设置,那么我们究竟怎样去实现动态换皮肤的效果呢?那么我们经常用到的就是设置不同的Style,并且在主程序的xaml文件中通过Dyn ...
- 【转】【iOS】动态更换App图标
原文网址:http://www.cocoachina.com/ios/20170619/19557.html 前言 动态更换App图标这件事,在用户里总是存在需求的:有些用户喜欢“美化”自己的手机.至 ...
- js动态更换img的src问题
在本地开发测试过程中,通过js动态更换img的src没有问题,图片正常切换,但是放在服务器上后测试发现,图片不显示,解决方法为:在对应onclick事件执行切换图片的js函数后加上一个return f ...
- FloatingActionButton动态更换背景色
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/186 FloatingActionButton 动态更换背 ...
- 空闲时间研究一个小功能:winform桌面程序如何实现动态更换桌面图标
今天休息在家,由于天气热再加上疫情原因,就在家里呆着,空闲时想着,在很早以前(约3年前),产品人员跟我提了一个需求,那就是winform桌面程序的图标能否根据节日动态更换,这种需求在移动APP上还是比 ...
- Element-UI动态更换主题
参考:vue-基于elementui换肤[自定义主题] 实践: 需求1.后期维护主题色不更换: 直接在线主题生成工具下载,在APP.VUE引入:(注意Element UI 版本1.3?2.0) 需求 ...
- 使用 css/less 动态更换主题色(换肤功能)
前言 说起换肤功能,前端肯定不陌生,其实就是颜色值的更换,实现方式有很多,也各有优缺点 一.看需求是什么 一般来说换肤的需求分为两种: 1. 一种是几种可供选择的颜色/主题样式,进行选择切换,这种可供 ...
- Android 动态更换桌面图标
每当双 11.12 来临之际,Android 手机 Launcher 中的淘宝.天猫图标就会变成双 11.12 主题的图标.实现了动态切换图标.名称 MainActivity package com. ...
随机推荐
- 31 ModelForm常用功能--扩充
ModelForm自定义字段的显示效果 class ChargeModelForm(BootStrapModelForm, forms.ModelForm): # 静态变量 # charge_type ...
- python批量导出、安装依赖库文件
导出: 在原环境中 pip freeze > fname.txt 安装: 在新环境中 pip install -r fname.txt 其中fname.txt 可以随意命名,其存储安装库文件列 ...
- 【APT】Hades APT组织针对乌克兰发起网络攻击事件分析
背景 Hades一个充满神秘色彩的APT组织,该组织因为2017年12月22日针对韩国平昌冬奥会的攻击活动被首次发现,后来卡巴斯基将该次事件的攻击组织命名为Hades.但是该攻击组织的归属问题却一直未 ...
- [自用初学]c++的构造函数
#include <stdio.h> #include <string.h> class Student { private: int id; char name[32]; p ...
- vue项目浏览器ioc小图标
1 先有一个需要图片,png .jpg ...格式都可以 2 把图片转换成 .con 格式的图标 ,http://www.bitbug.net/使用比特虫转换 ,目标尺寸:16*16 或者 32*3 ...
- 安装 vue devtools 时,npm i 和 npm run build 报错问题
1.如果 npm i 报错,运行如下命令: npm i --legacy-peer-deps 2.如果 npm run build 报错,运行如下命令: npm install -g yarn yar ...
- 11.30linux学习第十一天
今天老刘上课,第7章收尾,第8章开了个头. 7.1.3 磁盘阵列+备份盘 RAID 10磁盘阵列中最多允许50%的硬盘设备发生故障,但是存在这样一种极端情况,即同一RAID 1磁盘阵列中的硬盘设备若 ...
- OSS 解锁
一.解开LSKEYF00的编辑锁定 进入SE30,点击Tips & Tricks按钮,输入以下代码 UPDATE PROGDIR SET EDTX = '' WHERE ...
- 常用的typedef 定义
今天开始学习VC++基础,系统编程栏目下都是WinAPI和MFC的内容,此为浏览博客园时学习的一篇文章,觉得很实用,拿来做笔记. 出处见最底部. 三行代码: typedef char CHAR ...
- ACE Editor 常用Api(转)
ACE 是一个开源的.独立的.基于浏览器的代码编辑器,可以嵌入到任何web页面或JavaScript应用程序中.ACE支持超过60种语言语法高亮,并能够处理代码多达400万行的大型文档.ACE开发团队 ...