【问题标题】:Ionic-3 how to change the ionic theme dynamicallyIonic-3 如何动态改变离子主题
【发布时间】:2018-01-15 13:41:22
【问题描述】:

我需要动态更改我的 ionic 应用主题。 $color 主题值应该从数据库中渲染

给我一​​些想法来缩短这个!

【问题讨论】:

标签: angular ionic-framework ionic3


【解决方案1】:

我们不能动态更改 $color 值,但我们必须通过制作单独的 SCSS 文件来更改主题 这是参考 GitHub 项目 https://github.com/yannbf/ionic3-components/tree/master/src/theme

【讨论】:

    【解决方案2】:

    26-02-2019

    对于Ionic 4CSS 4,这是一个非常简单的任务。请参阅this article

    service.ts

    import { Injectable, Inject } from '@angular/core';
    import { DOCUMENT } from '@angular/common';
    import { DomController } from '@ionic/angular';
    
    interface Theme {
      name: string;
      styles: ThemeStyle[];
    }
    
    interface ThemeStyle {
      themeVariable: string;
      value: string;
    }
    
    @Injectable({
      providedIn: 'root'
    })
    export class ThemeSwitcherService {
    
      private themes: Theme[] = [];
      private currentTheme: number = 0;
    
      constructor(private domCtrl: DomController, @Inject(DOCUMENT) private document) { 
    
        this.themes = [
          {
            name: 'day',
            styles: [
              { themeVariable: '--ion-color-primary', value: '#f8383a'},
              { themeVariable: '--ion-color-primary-rgb', value: '248,56,58'},
              { themeVariable: '--ion-color-primary-contrast', value: '#ffffff'},
              { themeVariable: '--ion-color-primary-contrast-rgb', value: '255,255,255'},
              { themeVariable: '--ion-color-primary-shade', value: '#da3133'},
              { themeVariable: '--ion-color-primary-tint', value: '#f94c4e'},
              { themeVariable: '--ion-item-ios-background-color', value: '#ffffff'},
              { themeVariable: '--ion-item-md-background-color', value: '#ffffff'},
              { themeVariable: '--ion-tabbar-background-color', value: '#fff'},
              { themeVariable: '--ion-tabbar-ios-text-color-active', value: '#000000'},
              { themeVariable: '--ion-tabbar-md-text-color-active', value: '#000000'},
              { themeVariable: '--ion-background-color', value: '#f94c4e'}
            ]
          },
          {
            name: 'night',
            styles: [
              { themeVariable: '--ion-color-primary', value: '#222428'},
              { themeVariable: '--ion-color-primary-rgb', value: '34,34,34'},
              { themeVariable: '--ion-color-primary-contrast', value: '#ffffff'},
              { themeVariable: '--ion-color-primary-contrast-rgb', value: '255,255,255'},
              { themeVariable: '--ion-color-primary-shade', value: '#1e2023'},
              { themeVariable: '--ion-color-primary-tint', value: '#383a3e'},
              { themeVariable: '--ion-item-ios-background-color', value: '#717171'},
              { themeVariable: '--ion-item-md-background-color', value: '#717171'},
              { themeVariable: '--ion-tabbar-background-color', value: '#222428'},
              { themeVariable: '--ion-tabbar-ios-text-color-active', value: '#ffffff'},
              { themeVariable: '--ion-tabbar-md-text-color-active', value: '#ffffff'},
              { themeVariable: '--ion-background-color', value: '#383838'}
            ]
          }
        ]
    
      }
    
      cycleTheme(): void {
    
        if(this.themes.length > this.currentTheme + 1){
          this.currentTheme++;
        } else {
          this.currentTheme = 0;
        }
    
        this.setTheme(this.themes[this.currentTheme].name);
    
      }
    
      setTheme(name): void {
    
        let theme = this.themes.find(theme => theme.name === name);
    
        this.domCtrl.write(() => {
    
          theme.styles.forEach(style => {
            document.documentElement.style.setProperty(style.themeVariable, style.value);
          });
    
        });
    
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      • 2019-04-29
      • 2018-02-10
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多