How can I change the background color of ion-item when ion-toggle is enabled

Viewed 26

How can I change the background color of ion-item when ion-toggle is enabled Also showing Available when activating and showing Unavailable when closing

<ion-item>
      <ion-label>
        Status
        <span>Available</span>
        <span>Unavailable</span>
      </ion-label>
      <ion-toggle></ion-toggle>
    </ion-item>
3 Answers

Check the below Stack blitz and let me know if you need any modifications

https://stackblitz.com/edit/ionic-toggle-eg-6apotx?file=pages%2Fhome%2Fhome.html,pages%2Fhome%2Fhome.ts

You only have to set a class on ion-item when your toggle is checked. Then, set --background: (color); inside the css class. EXAMPLE:

HTML:

  <ion-item [class]="myToggle.checked?'checked':''">
    <ion-label>
      Status
      <span>Available</span>
      <span>Unavailable</span>
    </ion-label>
    <ion-toggle #myToggle></ion-toggle>
  </ion-item>
</ion-content>

SCSS:

ion-item.checked{
  --background: red;
}
Related