前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ionic3学习笔记(十)实现夜间模式功能

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

作者头像
Theo Tsao
发布2018-09-07 16:14:58
7730
发布2018-09-07 16:14:58
举报
文章被收录于专栏:Theo TsaoTheo Tsao

1. 创建主题样式

./src/theme 文件夹下创建 theme.light.scsstheme.dark.scss 2个文件,分别用于日间模式、夜间模式的设置。

theme.light.scss:

代码语言:javascript
复制
.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:

代码语言:javascript
复制
.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

代码语言:javascript
复制
@import "theme.light";
@import "theme.dark";

3. 创建 provider

终端运行:

代码语言:javascript
复制
ionic g provider setting-data

setting-data.ts:

代码语言:javascript
复制
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

终端运行:

代码语言:javascript
复制
ionic g page setting

setting.html

代码语言:javascript
复制
<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

代码语言:javascript
复制
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

代码语言:javascript
复制
<ion-nav [root]="rootPage" [class]="theme"></ion-nav>

app.component.ts

代码语言:javascript
复制
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();
    });
  }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-11-252,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 创建主题样式
  • 2. 导入 variables.scss
  • 3. 创建 provider
  • 4. 创建 page
  • 5. 在 App 入口处应用主题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档