Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically switch themes in Ionic Framework

I posted this on the Ionic forum, but I never seem to have luck on their forums, so I thought I'd try here.

I'd like to have options for a "dark" and "light" theme that a user can choose in their settings. What's the best way to go about that? Can I programmatically switch between ionic themes, like dark and stable?

Thanks in advance.

like image 240
JOV Avatar asked May 28 '14 19:05

JOV


People also ask

How do I change themes in ionic?

The fastest way to change the theme of your Ionic app is to set a new value for primary , since Ionic uses the primary color by default to style most components. Colors can be removed from the map if they aren't being used, but primary should not be removed.

How do I turn off dark mode in ionic?

One way to remove the dark theme would be by editing the variables. scss file and removing this style rule: @media (prefers-color-scheme: dark) { ... } That media query is the one that changes all the colors of the CSS custom properties when the users selects the dark theme on the device.

How do you set the dark theme in ionic?

Ionic Dark Theme​ Ionic has a recommended theme for variables to use in order to get a dark mode based on the device running the app. It can be broken down into the following parts: Changing the default Ionic colors for all modes to complement the dark background in the body. dark selector.


2 Answers

You can you ng-style to pass a css options object to an element. This will toggle font color on the element. Following this pattern you would have dark and light theme objects that you toggle between.

<div ng-style="style" class="item">
  This is a basic Card.
  <button ng-click="toggle()">Toggle</button>
</div>

And in your controller

.controller('AppCtrl', function($scope) {
    $scope.style = {
      color: '#000'
    };

    $scope.toggle = function() {
      $scope.style.color = ($scope.style.color === '#000' ? '#fff' : '#000');
    };
});

Demo

like image 195
hannuraina Avatar answered Oct 16 '22 21:10

hannuraina


Here is a simple example where you want to change the color of your header dynamically:

<ion-header-bar ng-class="'bar-' + appTheme">
      <h1 class="title">Ionic - Switch Themes</h1>
</ion-header-bar>

In your controller:

var selectedTheme = $window.localStorage.appTheme;
    if (selectedTheme) {
        $scope.appTheme = selectedTheme;
    } else {
        $scope.appTheme = 'positive';
    }

    $scope.themeChange = function (theme) {
        // save theme locally
        $window.localStorage.appTheme = theme;
        // reload
        $window.location = '';
    }

Live demo and full example @: http://techiedreams.com/ionic-custom-and-dynamic-theming/

like image 38
Chanakya Vadla Avatar answered Oct 16 '22 22:10

Chanakya Vadla