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

目录

  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. drf偏移分页组件-游标分页-自定义过滤器-过滤器插件django-filter

    drf偏移分页组件 LimitOffsetPagination 源码分析:获取参数 pahenations.py from rest_framework.pagination import Limit ...

  2. tensorflowlite 分类模型从训练到安卓部署

    https://blog.csdn.net/qq_33200967/article/details/82773677

  3. ios ktvhttpcache 音视频缓存插件使用

    1.PodFile 文件增加 pod 'KTVHTTPCache',  '~> 2.0.0' 2.在终端 需要先cd到podfile文件所在目录  执行pod install 3.在header ...

  4. 吴裕雄--天生自然 pythonTensorFlow自然语言处理:Seq2Seq模型--训练

    import tensorflow as tf # 1.参数设置. # 假设输入数据已经用9.2.1小节中的方法转换成了单词编号的格式. SRC_TRAIN_DATA = "F:\\Tens ...

  5. FastReport 使用入门 (二)

    上部分  我们将格式大概都画好了 下面 我们将Datatable的每列绑定到  我们添加的table控件上 .然后打开table控件的事件 双击选中 ManualBuild 事件 添加代码 priva ...

  6. selenium自学进度-2014.12.3

    今天把小米系列的视频看到了倒数第二课 他讲得很好,只是我现在是看第一遍,回头还需要照着视频多练练. 成长的道路问题艰辛的,学习的道路问题孤独的,希望自己能够坚持下去. 不要让今天的努力白费,不能让未来 ...

  7. PAT甲级——1140.Look-and-say Sequence (20分)

    Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213 ...

  8. Hbase的极限测试经验之java项目的jar包导入

    Hbase的极限测试的内容是把之前编过的网站的后台数据库改成hbase即可. 我很快就在hbase数据库中建完表,也把关于操作数据库的函数写好了. 当我调试时,发现在jsp中的操作数据库的函数都不能用 ...

  9. OAuth 2.0安全案例回顾

    转载自:http://www.360doc.com/content/14/0311/22/834950_359713295.shtml 0x00 背景 纵观账号互通发展史,可以发现OAuth比起其它协 ...

  10. HTMLTestRunner 报告框架使用

    HTMLTestRunner 报告框架使用 file_path = base_path + '/Report/report.html' with open(file_path, 'wb') as f: ...