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. ...
随机推荐
- STM32工程建立
STM32工程建立 对于用keil5建立stm32工程有两种方法,一种在学习过程中比较方便的建立方式:我们称为工程方式一,另一个便是在实际工程中用的最多,也最普遍,在实际过程中用的最多的,我们称为工程 ...
- 分布式中间件MyCat 使用
MySQL 分布式软件MyCAT介绍 目录 MySQL 分布式软件MyCAT介绍 一.MySQL 分布式软件MyCAT介绍 1.1.1 MySQL 分布式软件MyCAT介绍 1.1.2 MyCat 架 ...
- Oracle存储过程 Call使用
在 Oracle 中,可以将存储过程(PROCEDURE)定义在一个包(PACKAGE)中. 要调用包中的存储过程,需要使用包名和存储过程名来引用它们.以下是一个示例: 假设我们有一个名为 my_pa ...
- 【APT】Patchwork APT组织针对巴基斯坦国防官员攻击活动分析
前言 Patchwork(白象.摩诃草.APT-C-09.Dropping Elephant)是一个疑似具有印度国家背景的APT组织,该组织长期针对中国.巴基斯坦等南亚地区国家进行网络攻击窃密活动.本 ...
- [自用初学]c++值传递和引用传递/ *&/ string
https://baijiahao.baidu.com/s?id=1702573193376441989&wfr=spider&for=pc 总结: 1.函数参数传递主要分为值传递和& ...
- 【python】第二模块 步骤一 第三课、数据库的基本查询
第三课.数据库的基本查询 一.课程介绍 1.1 课程介绍 学习目标 数据的简单查询 无条件查询记录,字段的计算和字段的别名 数据的高级查询 数据排序.分页.去除重复记录 数据的有条件查询 条件表达式: ...
- if (()) [[]] [] 条件表达式比较示例
a.b的ASCII码是 097.098ASCII码 参考 http://www.51hei.com/mcu/4342.html 1. if (()) a=3; b=2 时,if (( a > b ...
- 高并发解决方案之 redis 分布式锁
背景:秒杀服务中要写一个定时任务:活动到期时给order微服务发送关闭订单的通知.这需要改变数据库表中的数据,而集群中服务是多节点的方式进行部署,会出现并发执行的情况,所以采用的redis的分布式锁的 ...
- RocketMQ4.x本地源码部署教程
安装前提条件(推荐) 64bit OS, Linux/Unix/Mac (Windows不兼容)64bit JDK 1.8+; 快速开始 http://rocketmq.apache.org/docs ...
- Using Semaphores in Delphi, Part 2: The Connection Pool
Abstract: Semaphores are used to coordinate multiple threads and processes. That semaphores provide ...