简体   繁体   中英

Flutter : Boolean value in Checkbox doesn't change

I want to build a checkbox list which increase when i press a add button. But checkbox isn't working, so i print boolean value of onChanged in checkbox and value of checkbox, i found both are same except first press(checkbox still not working). I changed value of checkbox to 'true', but result was same. Please help me to make a right code.I have copied all of my code please check addList function.

import 'package:flutter/material.dart';

void main() => runApp(FirstPage());

class FirstPage extends StatefulWidget {
  @override
  _FirstPageState createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  List<bool> listBool = [];
  List<Widget> listWidget = [];

  void addList(Text text) {
    int listLength = listWidget.length;
    listBool.add(false);

    listWidget.add(
      Row(
        children: <Widget>[
          Checkbox(
              value: listBool[listLength],
              onChanged: (bool newValue) {
                setState(() {
                  print(newValue);
                  print(listBool[listLength]);
                  listBool[listLength] = newValue;
                });
              }),
          text,
        ],
      ),
    );
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('toDoList'),
          backgroundColor: Colors.teal,
        ),
        body: SafeArea(
          child: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Container(
                    height: 100,
                    width: 100,
                    color: Colors.red,
                  ),
                  Container(
                    width: 100,
                    height: 100,
                    color: Colors.blue,
                  ),
                ],
              ),
              Expanded(
                  child: ListView.builder(
                      itemCount: listWidget.length,
                      itemBuilder: (BuildContext ctxt, int index) {
                        return listWidget[index];
                      })),
              FloatingActionButton(
                child: Icon(Icons.add),
                onPressed: () => addList(Text('shopping')),
              )
            ],
          ),
        ),
      ),
    );
  }
}

Please check below code and check output

import 'package:flutter/material.dart';

class FirstPage extends StatefulWidget {
  @override
  _FirstPageState createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  List<bool> listBool = [];
  List<Widget> listWidget = [];

  void addList() {
    setState(() {
      listBool.add(false);
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('toDoList'),
          backgroundColor: Colors.teal,
        ),
        body: SafeArea(
          child: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Container(
                    height: 100,
                    width: 100,
                    color: Colors.red,
                  ),
                  Container(
                    width: 100,
                    height: 100,
                    color: Colors.blue,
                  ),
                ],
              ),
              Expanded(
                  child: ListView.builder(
                      itemCount: listBool.length,
                      itemBuilder: (BuildContext ctxt, int index) {
                        return Row(
                          children: <Widget>[
                            Checkbox(
                                value: listBool[index],
                                onChanged: (bool newValue) {
                                  setState(() {
                                    print(newValue);
                                    print(listBool[index]);
                                    listBool[index] = newValue;
                                  });
                                }),
                            Text('shopping'),
                          ],
                        );
                      })),
              FloatingActionButton(
                child: Icon(Icons.add),
                onPressed: () => addList(),
              )
            ],
          ),
        ),
      ),
    );
  }
}

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