skip to Main Content

I am new to Flutter, I have created a switch widget with Statefull widget and I am trying to read the value of the switch from the screen to do certain task.

I have built custom switch.
This is my Switch Widget.

class SwitchGlobal extends StatefulWidget {
  final bool currentValue;
  final String controlText;
  final double fontSize;
  final FontWeight fontWeight;

  const SwitchGlobal({
    super.key,
    required this.currentValue,
    required this.controlText,
    required this.fontSize,
    required this.fontWeight,
  });

  @override
  State<SwitchGlobal> createState() =>
      _SwitchGlobalState(currentValue, controlText, fontSize, fontWeight);
}

class _SwitchGlobalState extends State<SwitchGlobal> {
  bool isSwitched = false;
  bool currentValue = false;
  String labelText = "";
  double fontSize = 8;
  FontWeight fontWeight = FontWeight.bold;

  _SwitchGlobalState(
      this.currentValue, this.labelText, this.fontSize, this.fontWeight);

  void toggleSwitch(bool value) {
    setState(() {
      currentValue = !currentValue;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Text(
            labelText,
            style: TextStyle(fontSize: fontSize, fontWeight: fontWeight),
          ),
          Switch(
            onChanged: toggleSwitch,
            value: currentValue,
            activeColor: Colors.green,
            activeTrackColor: Colors.greenAccent,
            inactiveThumbColor: Colors.grey,
            inactiveTrackColor: Colors.grey.shade200,
          ),
        ],
      ),
    );
  }
}

I just wanted to read the value of this switch widget in my screen when a user clicks on a button.

2

Answers


  1. Try to use SwitchGlobal widget property like

    Switch(
       value: widget.currentValue,
    
    Login or Signup to reply.
  2. Please update your switch code with this ;

    class SwitchGlobal extends StatefulWidget {
      final bool currentValue;
      final String controlText;
      final double fontSize;
      final FontWeight fontWeight;
      final Function(bool) voidCallback;
    
      const SwitchGlobal({
        super.key,
        required this.currentValue,
        required this.controlText,
        required this.fontSize,
        required this.fontWeight,
        required this.voidCallback,
      });
    
      @override
      State<SwitchGlobal> createState() => _SwitchGlobalState();
    }
    
    class _SwitchGlobalState extends State<SwitchGlobal> {
      @override
      Widget build(BuildContext context) {
        return Container(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Text(
                widget.controlText,
                style: TextStyle(
                    fontSize: widget.fontSize,
                    fontWeight: widget.fontWeight,
                    color: Colors.white),
              ),
              Switch(
                // onChanged:  voidCallback,
                onChanged: (v) {
                  widget.voidCallback(v);
                },
                value: widget.currentValue,
                activeColor: Colors.green,
                activeTrackColor: Colors.greenAccent,
                inactiveThumbColor: Colors.grey,
                inactiveTrackColor: Colors.grey.shade200,
              ),
            ],
          ),
        );
      }
    }
    

    Use :

    class MyWidget extends StatefulWidget {
      const MyWidget({super.key});
    
      @override
      State<MyWidget> createState() => _MyWidgetState();
    }
    
    class _MyWidgetState extends State<MyWidget> {
      bool currentValue = false;
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.grey,
          body: Column(
            children: [
              const SizedBox(height: 100),
              SwitchGlobal(
                voidCallback: (v) {
                  setState(() {
                    currentValue = !currentValue;
                  });
                },
                currentValue: currentValue,
                fontSize: 24,
                controlText: "hello",
                fontWeight: FontWeight.bold,
              ),
            ],
          ),
        );
      }
    }
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search