本文为原创文章,转载请标明出处

目录

  1. 创建主题样式
  2. 导入 variables.scss
  3. 创建 provider
  4. 创建 page
  5. 在 App 入口处应用主题
  6. 效果图

1. 创建主题样式

./src/theme 文件夹下创建 theme.light.scsstheme.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学习笔记(十)实现夜间模式功能的更多相关文章

  1. JavaScript学习笔记(十二) 回调模式(Callback Pattern)

    函数就是对象,所以他们可以作为一个参数传递给其它函数: 当你将introduceBugs()作为一个参数传递给writeCode(),然后在某个时间点,writeCode()有可能执行(调用)intr ...

  2. python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL

    python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL实战例子:使用pyspider匹配输出带.html结尾的URL:@config(a ...

  3. [转帖]Linux学习笔记之rpm包管理功能全解

    Linux学习笔记之rpm包管理功能全解 https://www.cnblogs.com/JetpropelledSnake/p/11177277.html rpm 的管理命令 之前学习过 yum 的 ...

  4. go微服务框架kratos学习笔记十(熔断器)

    目录 go微服务框架kratos学习笔记十(熔断器) 什么是熔断 熔断器逻辑 kratos Breaker kratos 熔断逻辑 kratos熔断器使用说明 bladmaster client br ...

  5. jsp学习笔记:mvc开发模式

    jsp学习笔记:mvc开发模式2017-10-12 22:17:33 model(javabe)与view层交互 view(视图层,html.jsp) controller(控制层,处理用户提交的信息 ...

  6. python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例

    python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...

  7. Learning ROS for Robotics Programming Second Edition学习笔记(十) indigo Gazebo rviz slam navigation

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 moveit是书的最后一章,由于对机械臂完全不知,看不懂 ...

  8. python3.4学习笔记(十八) pycharm 安装使用、注册码、显示行号和字体大小等常用设置

    python3.4学习笔记(十八) pycharm 安装使用.注册码.显示行号和字体大小等常用设置Download JetBrains Python IDE :: PyCharmhttp://www. ...

  9. python3.4学习笔记(十九) 同一台机器同时安装 python2.7 和 python3.4的解决方法

    python3.4学习笔记(十九) 同一台机器同时安装 python2.7 和 python3.4的解决方法 同一台机器同时安装 python2.7 和 python3.4不会冲突.安装在不同目录,然 ...

  10. python3.4学习笔记(十六) windows下面安装easy_install和pip教程

    python3.4学习笔记(十六) windows下面安装easy_install和pip教程 easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安 ...

随机推荐

  1. PAT Advanced 1085 Perfect Sequence (25) [⼆分,two pointers]

    题目 Given a sequence of positive integers and another positive integer p. The sequence is said to be ...

  2. oracle学习(二)pl/sql基础

    pl/sql组成:DDL DML DCL pl/sql特点: SQL&PL/SQL编译器集成PL/SQL,支持SQL所有范围的语法 支持CASE语句和表达式 继承和动态方法释放 类型进化.属性 ...

  3. Halcon中将16位的图像转化为8位的图像

    Halcon中Image有多种像素表示方式,这方面网上找到的资料比较少,有一张大恒图像培训的文档中提到过,感觉描述比较准确: 里面有四种类型比较类似:uint2.int1.int2.int4. 区分起 ...

  4. 小白学习之pytorch框架(3)-模型训练三要素+torch.nn.Linear()

    模型训练的三要素:数据处理.损失函数.优化算法    数据处理(模块torch.utils.data) 从线性回归的的简洁实现-初始化模型参数(模块torch.nn.init)开始 from torc ...

  5. UG NX7.5 采用VS2008调试方法

    1.安装NX7.5(x64),这是废话 2.安装visual studio 2008,推荐安装2008,如果是2010应该也可以用,(没有测试,不清楚) 3.复制 UGS\NX 7.5\UGOPEN\ ...

  6. 安装R的h5包

    系统 redhat7 安装H5的包, 依赖系统hdf5包,如下安装完, 发现版本不一致, sudo yum install hdf5-devel 解决办法: 移花接木 sudo ln -s /opt/ ...

  7. Review For Exam

    Review For Exam [2019 福建省赛] 一个很简单的状态压缩DP,结果集体走偏 如何解决连续几日的限制问题?这种东西普通的DP很难写 #include <bits/stdc++. ...

  8. Office、VBA开发方案选择指南

    最近很多朋友向我提出Office的开发方式方面的疑惑,主要是针对特定的系统和Office版本不知道选择哪一种编程语言.创建哪一种类型的项目. 事实确实如此,如果搞不清楚语言的特性和项目类型的特点,很可 ...

  9. php time()时间戳作为文件名产生文件同名的bug

    /*time()函数生成的文件名可能是相同的,因为如果php运行的过程如果足够快,time()函数调用的足够频繁,那么有可能time()生成的时间戳会相同,因为时间戳是以秒为单位,所以如果足够频繁有可 ...

  10. 迅为iTop开发板使用buildroot构建opencv文件系统

    这次我们来介绍使用buildroot构建opencv开发环境,buildroot 是 Linux平台上一个构建嵌入式Linux系统的框架.整个buildroot是由 Makefile脚本和Kconfi ...