How can I pass data between a statefulwidget children to his parent in Flutter/Dart

An option would be to add a callback method to _AvailableScreenState:

class _AvailableScreenState extends State<AvailableScreen> {
  bool _isSwitched = false;

  void setIsSwitched(switched) {
    setState(() {
      _isSwitched = switched;
    });
    print(_isSwitched);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: SafeArea(
        child: Container(
          margin: const EdgeInsets.only(left: 10),
          child: Column(
            children: [
              SetAvailableSwitch(setIsSwitched),

Which you can call from SetAvailableSwitch:

class SetAvailableSwitch extends StatefulWidget {
  final void Function(bool) setSwitched;

  const SetAvailableSwitch(this.setSwitched, {Key key}) : super(key: key);

  @override
  _SetAvailableSwitchState createState() => _SetAvailableSwitchState();
}

class _SetAvailableSwitchState extends State<SetAvailableSwitch> {
  bool _isSwitched = false;

  void _changeSwitch(bool value) {
    setState(() {
      if (_isSwitched) {
        _isSwitched = false;
      } else {
        _isSwitched = true;
      }
    });
    widget.setSwitched(_isSwitched);
  }

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top