samedi 6 novembre 2021

flutter) How can I change the widget created by the List builder?

I would like to create a list of container widgets where images can be added. And I'm going to upload the added images on firebase.

I tried to add an image after creating a widget through the button, but I couldn't change the widget that was already created.

Make a list so to add widgets.

List<InkWell> _productFormList = [];

This is the container widget I'm trying to create. I can add an image by clicking on this container.

  _productForm() {
    return InkWell(
      onTap: () {
        choosenImageWithPicker();
      },
      child: _imagePickerFile == null
          ? Container(
          color: Colors.grey[300],
          width: 200,
          height: 200,
          child: Center(child: Text("No image")))
          : Container(
        width: 200,
        height: 200,
        child: Image.network(
          _imagePickerFile.path,
        ),
      ),
    );
  }

list builder for creating widgets.

   ListView.separated(
        scrollDirection: Axis.horizontal,
        shrinkWrap: true,
        itemBuilder: (context, index) {
          return _productFormList[index];
        },
        separatorBuilder: (BuildContext context, int index) {
          return SizedBox(
            width: 20,
          );
        },
        itemCount: _productFormList.length,
      )
    

The button to add a list.

  _addButton() {
    return Container(
        width: 200,
        height: 65,
        child: ElevatedButton(
          onPressed: () {
            setState(() {
              _productFormList.add(_productForm());
            });
          },
          child: Text(
            'Add Container',
          ),
        ));
  }

And this is the image pickup method. (For Web)

   Future<void> choosenImageWithPicker() async {
    try {
      final _image = await _picker.getImage(source: ImageSource.gallery);
      setState(() {
        _imagePickerFile = _image;
      });
    } catch (err) {
      print(err);
    }  
   }

How can I change an already created widget?

Thank you.




Aucun commentaire:

Enregistrer un commentaire