vendredi 27 novembre 2020

Flutter I want to put checkbox on each item

I want to set a checkbox for each item while pulling DB data with Flutter, but in the current code like this, clicking the checkbox will check all the items. I tried removing the checkbox from the list, but it didn't work.

It is assumed that the DB data is already included. Please tell me how to resolve.

class Classname extends StatefulWidget {
  Classname({Key key}) : super(key: key);

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

class _ClassnameState extends State<Classname> {
  String test;
  String test2;
  String test3;
  bool isChecked = false;

  @override
  void initState() {
    getData();
    super.initState();
  }

  Future<List<Map>> getData() async {
    String path = join(await getDatabasesPath(), 'dbname.db');

    Database database = await openDatabase(path, version: 1,
        onCreate: (Database db, int version) async {
      await db.execute(
          "CREATE TABLE tablename(id INTEGER PRIMARY KEY, test TEXT, test2 TEXT, test3 TEXT)");
    });

    List<Map> result = await database.rawQuery('SELECT * FROM tablename');
    return result;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder<List<Map>>(
          future: getData(),
          builder: (context, result) {
            return SingleChildScrollView(
              child: Container(                
                child: Column(                    
                    children: List.generate(result.data.length, (index) {
                      var data = result.data[index];
                      test = data['test'];
                      test2 = data['test2'];
                      test3 = data['test3'];
                      return Column(
                        children: [
                          Row(                            
                            children: <Widget>[
                              Checkbox(
                                value: isChecked,
                                onChanged: (bool value) {
                                  setState(() {
                                    isChecked = value;
                                  });
                                },                               
                              ),
                              Container(
                                child: Text(
                                  test,
                                ),
                              ),                              
                              Container(
                                child: Text(test2),
                              ),
                            ],
                          ),
                          Align(
                            alignment: Alignment.centerLeft,                            
                              child: Text(
                                test3,                                
                              ),
                            ),
                          ),
                        ],
                      );
                    }),
                ),
              ),
            );
          },
      ),
    );
  },
}



Aucun commentaire:

Enregistrer un commentaire