Is there any checkbox controller like TextEditingController in flutter?

Viewed 3547

In my flutter project, I have used 4 checkboxes on a page. I want to display some lists on next page according to which checkboxes are selected. So how can I do this?Is there any way like TextEditingController?

1 Answers
class MyWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => MyWidgetState();
}

class MyWidgetState extends State<MyWidget> {
  bool monVal = false;
  bool tuVal = false;
  bool wedVal = false;
  @override
  Widget build(BuildContext context) {
    return Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
      checkbox(
          title: "Mon",
          initValue: monVal,
          onChanged: (sts) => setState(() => monVal = sts)),
      checkbox(
          title: "Tue",
          initValue: tuVal,
          onChanged: (sts) => setState(() => tuVal = sts)),
      FlatButton(
          child: Text("Print"),
          onPressed: () {
            print("Mon is ${monVal ? 'checked' : 'unchecked'}");
            print("Tue is ${tuVal ? 'checked' : 'unchecked'}");
          })
    ]);
  }

  Widget checkbox(
      {String title, bool initValue, Function(bool boolValue) onChanged}) {
    return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(title),
          Checkbox(value: initValue, onChanged: (b) => onChanged(b))
        ]);
  }
}
Related