I have these containers to select some images. When the user selects the image it covers the whole container so I want the child (ICONS) to be transparent after the image is selected.
InkWell(
onTap: () async {
XFile? image3 = await _picker.pickImage(
source: ImageSource.gallery);
if (image3 == null) {
debugPrint("got null");
return;
}
final image =
Image.memory(await image3.readAsBytes());
decorationImage3 = DecorationImage(
fit: BoxFit.cover,
image: image.image,
);
setState(() {});
},
child: Container(
height: MediaQuery.of(context).size.width / 4 - 25,
width: MediaQuery.of(context).size.width / 4 - 25,
decoration: BoxDecoration(
color: Colors.blue.shade900,
borderRadius: BorderRadius.circular(5),
image: decorationImage3,
),
child: const Icon(Icons.add, color: Colors.white),
),
),
CodePudding user response:
Use this instead
child: (image3 == null)?Icon(Icons.add, color: Colors.white):Sizedbox.shrink();
CodePudding user response:
You can keep track if an image is selected like this :
bool isIMGpicked = false;
...
if (image3 == null) {
setState(() {
isIMGpicked = true;
});
debugPrint("got null");
return;
}
then you can do 1 of these option to make the button disapear
Visibility(child :const Icon(Icons.add, color: Colors.white),visible : isIMGpicked)
or this (but the button will still work) :
Icon(Icons.add, color: isIMGpicked?Colors.transparent:Colors.white)