lundi 23 novembre 2020

Flutter set independent Checkbox and looping variable into same widget

I want to incorporate Checkbox and looping variable into widget. So I have code like this and no alert but it's not perfect. Because Checkbox worked all list. I want to check only the box that is changed. Premise ... Data has been inserted in DB

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 check = 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) {
    // TODO: implement build
    return Scaffold(
      body: FutureBuilder<List<Map>>(
          future: getData(),
          builder: (context, result) {
            return SingleChildScrollView(
              child: Container(
                padding: EdgeInsets.all(30),
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    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(
                            mainAxisAlignment: MainAxisAlignment.start,
                            mainAxisSize: MainAxisSize.max,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: <Widget>[
                              Checkbox(
                                value: checked,
                                onChanged: (bool value) {
                                  setState(() {
                                    check = value;
                                  });
                                },
                                checkColor: Colors.black,
                                activeColor: Colors.white,
                              ),
                              Container(
                                child: Text(
                                  test,
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.all(15),
                              ),
                              Container(
                                child: Text(test2),
                              ),
                            ],
                          ),
                          Align(
                            alignment: Alignment.centerLeft,
                            child: Padding(
                              padding:  EdgeInsets.all(5),
                              child: Text(
                                test3,
                                overflow: TextOverflow.visible,
                              ),
                            ),
                          ),
                        ],
                      );
                    }),
                ),
              ),
            );
          },
      ),
    );
  },
}

I tried other ways, for example, only Checkbox move before List.generate method, but these didn't work. Please tell how to do.




Aucun commentaire:

Enregistrer un commentaire