0

I am writing code to change Dark/Light Theme using SwitchListTile where I need to use bool type variable.

Issue: The MaterialApp has no Dark/Light Theme bool variable parameter.

Using ValueNotifier and ValueListenableBuilder uses non-bool enum type variable themeNotifier to update Theme.

Here is my code:

This line defines themeNotifier which is not bool type.

  1. static final ValueNotifier<ThemeMode> themeNotifier = ValueNotifier(ThemeMode.light);

This line defines my own isLightThemeMode bool type variable.

  1. static bool isLightThemeMode = (myClassState.themeNotifier.value == ThemeMode.light) ? true : false;

Main Issue: I need to use themeNotifier in the ValueListenableBuilder to change Theme Mode, but there is no bool variable here:

  1. ValueListenableBuilder<ThemeMode>(valueListenable: themeNotifier,builder: (_, ThemeMode currentMode, __)

  2. I should be using bool variable isLightThemeMode in SwitchListTile:

    SwitchListTile(title: Text("Theme",style: TextStyle(fontSize: 11)),value: isLightThemeMode,onChanged: (bool value) {{setState(() {isLightThemeMode = value;});}})

This code does not work.

  1. The following code works, but it does not use bool variable isLightThemeMode.

    SwitchListTile(title: Text("Theme",style: TextStyle(fontSize: 11)),value: myClassState.themeNotifier.value==ThemeMode.light,onChanged: (bool value) {{setState(() {myClassState.themeNotifier.value = value ? ThemeMode.light: ThemeMode.dark;;});}})

Could you please advice how to use bool variable to change Theme.

Thank You

DktPhl2018
  • 155
  • 1
  • 1
  • 8

0 Answers0