Ionic3学习笔记(十)实现夜间模式功能


声明:本文转载自https://my.oschina.net/metaphors/blog/1579864,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

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

目录

  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. 效果

light theme dark theme

如有不当之处,请予指正,谢谢~

本文发表于2017年11月26日 10:33
(c)注:本文转载自https://my.oschina.net/metaphors/blog/1579864,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 2155 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1