Flutter Switch onChanged Not Changing. If you want to save it and use in another class or if you want to show latest state for everytime you load you can save the state in a global variable and call it when you load the class. hope it will help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
class Tab_b extends StatefulWidget { @override State<StatefulWidget> createState() => new _TabsPageState(); } class _TabsPageState extends State<Tab_b>{ bool isInstructionView; @override void initState() { isInstructionView = Global.shared.isInstructionView; super.initState(); } @override Widget build(BuildContext context) { return Scaffold( appBar: new AppBar( title: new Text("add data"), ), body: new Container( child: Switch( value: isInstructionView, onChanged: (bool isOn) { print(isOn); setState(() { isInstructionView = isOn; Global.shared.isInstructionView = isOn; isOn =!isOn; print(isInstructionView); }); }, activeColor: Colors.blue, inactiveTrackColor: Colors.grey, inactiveThumbColor: Colors.grey, ), ), ); } } class Global{ static final shared =Global(); bool isInstructionView = false; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.