Flutter Switch will not work inside AlertBox

Solution for Flutter Switch will not work inside AlertBox
is Given Below:

I am having a problem where when I try to use a switch widget it will not work properly inside of an alert box as in it does not switch over to the second state it just bounces whenever I try to flick it. I am wondering if this is because there is a problem with the switch itself or how I displayed it in the box? Thanks!

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(),
      home: SwitchDemo(),
    );
  }
}

class SwitchDemo extends StatefulWidget {
  const SwitchDemo({Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => new _TabsPageState();
}

class _TabsPageState extends State<SwitchDemo> {
  bool isInstructionView;

  @override
  void initState() {
    super.initState();
    isInstructionView = Global.shared.isInstructionView;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("add data"),
        ),
        body: Container(
          child: TextButton(
              child: Text('Open Alert Box'),
              onPressed: () => {
                    showDialog(
                        context: context,
                        builder: (BuildContext context) {
                          return Padding(
                            padding: EdgeInsets.symmetric(
                              horizontal: MediaQuery.of(context).size.width / 20,
                              vertical:
                                  MediaQuery.of(context).size.height / 20,
                            ),
                            child: AlertDialog(
                              content: Container(
                                child: Switch(
                                  value: isInstructionView,
                                  onChanged: (bool isOn) {
                                    if (isInstructionView == false) {
                                    } else if (isInstructionView == true) {}
                                    setState(() {
                                      isInstructionView = isOn;
                                      Global.shared.isInstructionView = isOn;
                                      isOn = !isOn;
                                    });
                                  },
                                  activeColor: Colors.blue,
                                  inactiveTrackColor: Colors.grey,
                                  inactiveThumbColor: Colors.grey,
                                ),
                              ),
                            ),
                          );
                        })
                  }),
        ));
  }
}

class Global {
  static final shared = Global();
  bool isInstructionView = false;
}

Wrap you AlertDialog with StatefulBuilder.
here is full code:

import 'package:flutter/material.dart';

class SwitchDemo extends StatefulWidget {
  const SwitchDemo({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => new _TabsPageState();
}

class _TabsPageState extends State<SwitchDemo> {
  late bool isInstructionView;

  @override
  void initState() {
    super.initState();
    isInstructionView = Global.shared.isInstructionView;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("add data"),
        ),
        body: Container(
          child: TextButton(
              child: Text('Open Alert Box'),
              onPressed: () => {
                    showDialog(
                      context: context,
                      builder: (BuildContext context) {
                        return Padding(
                          padding: EdgeInsets.symmetric(
                            horizontal: MediaQuery.of(context).size.width / 20,
                            vertical: MediaQuery.of(context).size.height / 20,
                          ),
                          child: StatefulBuilder(builder: (context, setState) {
                            return AlertDialog(
                              content: Container(
                                child: Switch(
                                  value: isInstructionView,
                                  onChanged: (bool isOn) {
                                    print(isInstructionView);
                                    setState(() {
                                      isInstructionView = !isInstructionView;
                                    });
                                  },
                                  activeColor: Colors.blue,
                                  inactiveTrackColor: Colors.grey,
                                  inactiveThumbColor: Colors.grey,
                                ),
                              ),
                            );
                          }),
                        );
                      },
                    )
                  }),
        ));
  }
}

class Global {
  static final shared = Global();
  bool isInstructionView = false;
}

Does it answer your question?
ref: https://stackoverflow.com/a/57240941/10157127