简体   繁体   中英

How can I create a checkbox function on flutter

I am trying to implement a checkbox for my project but it is not outputting. Below is a sniper of my code

class _MyAppState extends State<MaterialApp> {
  bool _myBoolean = false;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Checkbox(
        value: _myBoolean,
        onChanged: (value) {
          setState(() {
            _myBoolean = value; // rebuilds with new value
          });
        },
      ),
    );
  }
}

Please do assist me on this

Hi 🙂 you were almost there : just add ! to value so isChecked = notValue

like this :

class _MyAppState extends State<MaterialApp> {
  bool _myBoolean = false;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Checkbox(
        value: _myBoolean,
        onChanged: (bool? value) {
          setState(() {
            _myBoolean = value!; // WILL rebuild
          });
        },
      ),
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM