Why Flutter Switch onChanged Switch Back to Initial Place?

42 Views Asked by At

I currently working on the Flutter switch that used for changing light and dark theme and I place it in a drawer, however the switch is back to it initial or original position after I tapped. If it is in dark mode, it should be keep toggle to the right until I swipe back but it toggle back to left side each time I close or re-open my drawer. Everyone can help me fix this problem, appreciate those help.

Code:

  void switchThemeTool() {
    Get.snackbar(
      'Theme Changed',
      'Switching theme',
      colorText: Colors.black,
      backgroundColor:
          Get.isDarkMode ? const Color(0x00303030) : Colors.white10,
    );
    ThemeService().switchTheme();
  }

    bool isSwitched = false;
    
     Widget build(BuildContext context) {
        return Drawer(
          shadowColor: Colors.black,
          surfaceTintColor: Colors.black,
           child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    const Icon(Icons.light_mode, color: Colors.white, size: 36),
                    Switch(
                        value: isSwitched,
                        onChanged: (value) {
                          setState(() {
                            isSwitched = value;
                            switchThemeTool();
                          });
                        }),
                    const Icon(Icons.dark_mode, color: Colors.amber, size: 36),
                  ],
                ),
            );
      }
0

There are 0 best solutions below