mercredi 15 septembre 2021

Checkbox in contacts application in flutter

I am new to flutter and I am not getting what's wrong in this code. I am trying to implement an application in which all the contacts of device are retrieved and I want checkbox functionality inside it. If anyone can help me by making changes in my code, it will be a great help.

Image of error

Source Code:

import 'package:flutter/material.dart';
import 'package:contacts_service/contacts_service.dart';
import 'package:contacts_app/smsButton.dart';
import 'package:contacts_app/phoneButton.dart';

class ContactsPage extends StatefulWidget {
  @override
  _ContactsPageState createState() => _ContactsPageState();
}

class _ContactsPageState extends State<ContactsPage> {
  Iterable<Contact> _contacts=[];
  List<bool> value = [];


  @override
  void initState() {
    getContacts();
    super.initState();
    for(int i=0;i<_contacts.length;i++){
      value[i]=false;
    }
  }

  Future<void> getContacts() async {
    //We already have permissions for contact when we get to this page, so we
    // are now just retrieving it
    final Iterable<Contact> contacts = await ContactsService.getContacts();
    setState(() {
      _contacts = contacts;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _contacts != null
          //Build a list view of all contacts, displaying their avatar and
          // display name
          ? ListView.builder(
              itemCount: _contacts?.length ?? 0,
              itemBuilder: (BuildContext context, int index) {
                Contact contact = _contacts?.elementAt(index);
                return ListTile(
                    contentPadding:
                      const EdgeInsets.symmetric(vertical: 2, horizontal: 18),
                  leading: (contact.avatar != null && contact.avatar.isNotEmpty)
                      ? CircleAvatar(
                          backgroundImage: MemoryImage(contact.avatar),
                        )
                      : CircleAvatar(
                          child: Text(contact.initials()),
                          backgroundColor: Theme.of(context).accentColor,
                        ),
                  title: Text(contact.displayName ?? ''),
                  //This can be further expanded to showing contacts detail
                  // onPressed().
                  trailing: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      PhoneButton(phoneNumbers: contact.phones),
                      SmsButton(phoneNumbers: contact.phones),
                      buildCheckbox(index),
                    ],
                  ),

                );
              },
            )
          : Center(child: const CircularProgressIndicator()),
    );
  }

  Widget buildCheckbox(int index)=>Checkbox(
    value: value[index],
    onChanged: (value){
        setState(() {
          this.value[index]=value;
        },
      );

    },
  );
}



Aucun commentaire:

Enregistrer un commentaire