1

I am using this function to get images from the gallery

_getFromGallery() async {
    XFile? pickedFileGal = await ImagePicker().pickImage(
      source: ImageSource.gallery,
      maxWidth: 1800,
      maxHeight: 1800,
    );
    if (pickedFileGal != null) {
      XFile imageFileGal = XFile(pickedFileGal.path);
      setState(() {
        _imageFile = imageFileGal;
      });
    }
  }
late XFile _imageFile;

I need to set the image to the container background, the container has a child column. So I need to set it in BoxDecoration, but I have the image in XFile the container BoxDecoration image does not take Image.file(). So how can I achieve that? Please guide me

Container(
                      decoration: BoxDecoration(
                        image: Image.file(_imageFile), //Error 
                      ),
                      padding: const EdgeInsets.only(top: 20),
                      color: Colors.white,
                      margin: const EdgeInsets.only(
                        bottom: 15,
                      ),
                      child: Column(
                        children: []))
Abubakar
  • 97
  • 8

1 Answers1

2

First the image here needs to be a DecorationImage which then takes ImageProvider<Object>.

Knowing this , the correct way to use this is as follows

 Container(decoration: BoxDecoration(
              image: DecorationImage(image: Image.file('file').image)
            ),),

Xfile to file

Xfile pickedFile;
   Image.file(File(pickedFile.path))
griffins
  • 7,079
  • 4
  • 29
  • 54
  • decoration: BoxDecoration( image: DecorationImage( image: Image.file(_imageFile).image, ), ), I followed the instruction still having this error "The argument type 'XFile' can't be assigned to the parameter type 'File'." – Abubakar Sep 10 '22 at 14:00
  • I have updated my answer with xfile to file.@Abubakar – griffins Sep 10 '22 at 15:25