0
I am listing some values that are returned from a PHP API. In this list I am using a Checkbox for each information.
When I click on any Checkbox, all items in the list are selected. How do I pick a specific item?
Follow the code below:
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class ListaCNPJ extends StatefulWidget {
  String cpf;
  String perfil;
  ListaCNPJ({this.cpf, this.perfil});
  @override
  _ListaCNPJState createState() => _ListaCNPJState();
}
class _ListaCNPJState extends State<ListaCNPJ> {
  bool _lista = false;
  //TextEditingController _controllerCNPJ = TextEditingController();
  Future getContactData(String cpf, String perfil) async {
    const URL = 'http://localhost/camera/api.php';
    var response = await http.get(
        URL + "?action=consultarCNPJ"
            "&cpf=$cpf"
            "&perfil=$perfil"
    );
    return json.decode(response.body);
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Lista de CNPJs"),
        centerTitle: true,
      ),
      body: FutureBuilder(
        future: getContactData(widget.cpf, widget.perfil),
        builder: (context, snapshot){
          print("resposta: "+snapshot.data.toString());
          if(snapshot.hasError){
            print(snapshot.error);
          }
          return snapshot.hasData ? Column(
            children: [
              Expanded(
                  child: ListView.builder(
                  itemCount: snapshot.data.length,
                  itemBuilder: (context, index){
                    List list = snapshot.data;
                    return ListTile(
                        title: CheckboxListTile(
                            title: Text(list[index]['tb_empresa']),
                            subtitle: Text(list[index]['tb_cnpj']),
                            value: false, // _lista[index]["realizada"],
                            onChanged: (valor){
                              setState(() {
                                _lista = valor;
                              });
                            },
                        )
                    );
                  }
                )
              )
            ],
          ) :
          Center(child: CircularProgressIndicator());
        },
      ),
    );
  }
}
						
This article here can help you Uncomplicated Flutter - Checkbox
– Matheus Ribeiro
Matheus did what is on the link you gave me worked out, thank you very much
– teste