samedi 15 juin 2019

Checkbox inside the Sliverlist that gets data from firebase is not working

I'm reading carName, carCheckbox form firebase collection, and displaying in Listview. Checkbox is not enabled in the Listview.

1) My output in the ListView looks like <--, carName, SportCarCheckBox [], FamilyCarCheckbox[], --> 2) These are the issues I'm facing: a) Either checkboxes are disabled b) or all the checkBoxes in all the rows and columns are checked or unchecked

   import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
final formKey = new GlobalKey<FormState>();
String carName = '';
void main() => runApp(MyApp());
//====================================
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {return MaterialApp(home: TestListPage(),);}}
//====================================
class TestListPage extends StatefulWidget {
  @override
  _TestListPageState createState() => _TestListPageState();}
//====================================
class _TestListPageState extends State<TestListPage> {
  Stream<int> stream;
  @override
  Widget build(BuildContext context) {
    return Scaffold(appBar: AppBar(title: Text('Title Checkbox'),),
      body: TestListCustomScrollViewWidget(),);}
//====================================
  Widget TestListCustomScrollViewWidget() {
    print ('.............................................');
    return CustomScrollView(scrollDirection: Axis.vertical, slivers: <Widget>[
      SliverAppBar(title: Text('Sliverlist')),
      Form(key: formKey, child: TestListStreamBuilderListWidget(),)]);}

  //================================
  Widget TestListStreamBuilderListWidget() {
    return    StreamBuilder(stream: Firestore.instance.collection('testcrud').orderBy('carName').snapshots(),
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
             if(snapshot.hasData) {return SliverList(
              delegate: SliverChildBuilderDelegate(
                    (context, index) => Container(child: TestListTileWidget(snapshot, index),),
                childCount: snapshot.hasData ? snapshot.data.documents.length : 0,),);}});}

//====================================
  Widget TestListTileWidget(snapshot, index){
//    print('carName:' + snapshot.data.documents[index]['carName']);
    return ListTile(
      enabled: true, leading: Icon(Icons.arrow_back_ios), trailing: Icon(Icons.arrow_forward_ios,),
      title: new Row(
        children: <Widget>[
          new Expanded(child: new Text(snapshot.data.documents[index]['carName'])),
          _checkBox('Sports', snapshot.data.documents[index]['carCheckbox']),
          _checkBox('Family', snapshot.data.documents[index]['carCheckbox']),],),);}
//=======================================
  Widget _checkBox(String category, bool isChecked) {
    return Column(
      children: <Widget>[
        Text('$category',),
        Checkbox(value: isChecked,
          onChanged: (bool value) {setState(() {isChecked = value;});
            print('updated $category value to: $value');},)],);}
//====================================
} // End of Class

The code should allow me to check or uncheck any checkbox. Any help will be appreciable.




Aucun commentaire:

Enregistrer un commentaire