Ionic3学习笔记(十)实现夜间模式功能
本文为原创文章,转载请标明出处
目录
- 创建主题样式
- 导入
variables.scss
- 创建 provider
- 创建 page
- 在 App 入口处应用主题
- 效果图
1. 创建主题样式
在 ./src/theme
文件夹下创建 theme.light.scss
、theme.dark.scss
2个文件,分别用于日间模式、夜间模式的设置。
theme.light.scss
:
.light-theme {
ion-content {
background-color: #f4f4f4;
}
.item {
background-color: #fff;
}
ion-textarea {
background-color: #fff;
}
.toolbar-background {
background-color: #f8f8f8;
}
.tab-button {
background-color: #f8f8f8;
}
}
theme.dark.scss
:
.dark-theme {
ion-content {
background-color: #555;
}
.item {
background-color: #555;
}
ion-textarea {
background-color: #666;
}
.toolbar-background {
background-color: #444;
}
.tab-button {
background-color: #444;
}
}
这是我的2个主题样式,读者可以自己按需进行编写。
2. 导入 variables.scss
@import "theme.light";
@import "theme.dark";
3. 创建 provider
终端运行:
ionic g provider setting-data
setting-data.ts
:
import {Injectable} from '@angular/core';
import {BehaviorSubject} from "rxjs/BehaviorSubject";
@Injectable()
export class SettingDataProvider {
// true: dark-theme
// false: light-theme
theme: BehaviorSubject<boolean>;
constructor() {
this.theme = new BehaviorSubject(false);
}
setActiveTheme(theme) {
this.theme.next(theme);
}
getActiveTheme() {
return this.theme.asObservable();
}
}
4. 创建 page
终端运行:
ionic g page setting
setting.html
<ion-header>
<ion-navbar>
<ion-title>设置</ion-title>
</ion-navbar>
</ion-header>
<ion-content>
<ion-list>
<ion-list-header>个性化设置</ion-list-header>
<ion-item>
<ion-label>夜间模式</ion-label>
<ion-toggle checked="{{theme}}" (ionChange)="toggleTheme()"></ion-toggle>
</ion-item>
</ion-list>
</ion-content>
setting.ts
import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {SettingDataProvider} from "../../providers/setting-data/setting-data";
@IonicPage()
@Component({
selector: 'page-setting',
templateUrl: 'setting.html',
})
export class SettingPage {
theme: boolean;
constructor(public navCtrl: NavController, public navParams: NavParams, public toastCtrl: ToastController, public settingDataProvider: SettingDataProvider) {
this.getActiveTheme();
}
getActiveTheme() {
this.settingDataProvider.getActiveTheme().subscribe(theme => {
this.theme = theme;
});
}
toggleTheme() {
if (!this.theme) {
this.presentToast('关闭应用后夜间模式将失效');
}
this.settingDataProvider.setActiveTheme(!this.theme);
}
presentToast(message: string) {
let toast = this.toastCtrl.create({message: message, duration: 2000});
toast.present().then(value => {
return value;
});
}
}
5. 在 App 入口处应用主题
app.html
<ion-nav [root]="rootPage" [class]="theme"></ion-nav>
app.component.ts
import {Component} from '@angular/core';
import {Platform} from 'ionic-angular';
import {StatusBar} from '@ionic-native/status-bar';
import {SplashScreen} from '@ionic-native/splash-screen';
import {SettingDataProvider} from "../providers/setting-data/setting-data";
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage: any = 'TabsPage';
theme: string;
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, settingDataProvider: SettingDataProvider) {
settingDataProvider.getActiveTheme().subscribe(theme => {
if (theme) {
this.theme = 'dark-theme';
} else {
this.theme = 'light-theme';
}
});
platform.ready().then(() => {
statusBar.styleDefault();
splashScreen.hide();
});
}
}
6. 效果
如有不当之处,请予指正,谢谢~
Ionic3学习笔记(十)实现夜间模式功能的更多相关文章
- JavaScript学习笔记(十二) 回调模式(Callback Pattern)
函数就是对象,所以他们可以作为一个参数传递给其它函数: 当你将introduceBugs()作为一个参数传递给writeCode(),然后在某个时间点,writeCode()有可能执行(调用)intr ...
- python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL
python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL实战例子:使用pyspider匹配输出带.html结尾的URL:@config(a ...
- [转帖]Linux学习笔记之rpm包管理功能全解
Linux学习笔记之rpm包管理功能全解 https://www.cnblogs.com/JetpropelledSnake/p/11177277.html rpm 的管理命令 之前学习过 yum 的 ...
- go微服务框架kratos学习笔记十(熔断器)
目录 go微服务框架kratos学习笔记十(熔断器) 什么是熔断 熔断器逻辑 kratos Breaker kratos 熔断逻辑 kratos熔断器使用说明 bladmaster client br ...
- jsp学习笔记:mvc开发模式
jsp学习笔记:mvc开发模式2017-10-12 22:17:33 model(javabe)与view层交互 view(视图层,html.jsp) controller(控制层,处理用户提交的信息 ...
- python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例
python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...
- Learning ROS for Robotics Programming Second Edition学习笔记(十) indigo Gazebo rviz slam navigation
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 moveit是书的最后一章,由于对机械臂完全不知,看不懂 ...
- python3.4学习笔记(十八) pycharm 安装使用、注册码、显示行号和字体大小等常用设置
python3.4学习笔记(十八) pycharm 安装使用.注册码.显示行号和字体大小等常用设置Download JetBrains Python IDE :: PyCharmhttp://www. ...
- python3.4学习笔记(十九) 同一台机器同时安装 python2.7 和 python3.4的解决方法
python3.4学习笔记(十九) 同一台机器同时安装 python2.7 和 python3.4的解决方法 同一台机器同时安装 python2.7 和 python3.4不会冲突.安装在不同目录,然 ...
- python3.4学习笔记(十六) windows下面安装easy_install和pip教程
python3.4学习笔记(十六) windows下面安装easy_install和pip教程 easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安 ...
随机推荐
- PIL库参考文档之Image模块
原文: https://pillow-cn.readthedocs.io/zh_CN/latest/reference/Image.html 中文版参考文档不全,所以自己试着翻译了一下,以下~备注部分 ...
- ES6之模块化
本文介绍ES6实现模块化的方法:使用import和export. 导入的时候需不需要加大括号的判断:1.当用export default people导出时,就用 import people 导入(不 ...
- vue-resource HTTP API基础
vue-resource特点 vue-resource插件具有以下特点: 1. 体积小 vue-resource非常小巧,在压缩以后只有大约12KB,服务端启用gzip压缩后只有4.5KB大小,这远比 ...
- python学习笔记(30)——ddt
1.ddt模块包含类的装饰器ddt和两个方法装饰器data ddt.ddt:装饰类,也就是继承TestCase的类. ddt.data:装饰测试方法,参数是一系列的值,用来传递参数 ddt.file_ ...
- Maven配置阿里云问题
现在我们经常会与Maven打交道,无论是工作还是自己的练笔,因为它真的是太强大了.它可以帮助我们管理版本,jar包等等. 但是由于国内的环境问题,我们需要有一个仓库,有些公司会自己搭建私服,那对个人来 ...
- [LC] 373. Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
- JacksonUtil
package org.linlinjava.litemall.core.util; import com.fasterxml.jackson.core.type.TypeReference; imp ...
- springboot支付项目之springboot集成jpa
springboot集成spring-jpa 本文主要内容: 1:spring boot怎么集成spring-jpa以及第一个jpa查询示例 如jpa几个常用注解.lombok注解使用 2:怎么设置i ...
- web接口测试中需要注意的点
1.接口返回 数据格式是否和预期一致.例如:要求返回json格式的数据,json数据的key命名是否正确,对应的value是否与数据库一致. 需要转换的数据是否正确转换,例如时间戳是否按正确转换为时间 ...
- Introduction to Differential Equations,Michael E.Taylor,Page 3,4 注记
此文是对 [Introduction to Differential Equations,Michael E.Taylor] 第3页的一个注记.在该页中,作者给了微分方程$$\frac{dx}{dt} ...