更改图标颜色或删除它



我有这些容器来选择一些图像。当用户选择图像时,它覆盖了整个容器,所以我希望子元素(ICONS)在图像被选中后是透明的。

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),
),
),

用这个代替

child: (image3 == null)?Icon(Icons.add, color: Colors.white):Sizedbox.shrink();

如果图像被选中,您可以像这样跟踪:

bool isIMGpicked = false;
...
if (image3 == null) {
setState(() {
isIMGpicked = true;
});

debugPrint("got null");
return;
}

那么你可以选择其中的一个来让按钮消失

Visibility(child :const Icon(Icons.add, color: Colors.white),visible : isIMGpicked)

或此(但按钮仍将工作):

Icon(Icons.add, color: isIMGpicked?Colors.transparent:Colors.white)

最新更新