[英]How can I remove the icon when the validation is null in flutter?
I have a function that receives a list in the function parameter and I am displaying that list.我有一个接收函数参数列表的函数,我正在显示该列表。
I have a validation in the Text, when it is null it shows me a message that says "no data" or otherwise it shows me the value of the list.我在文本中有一个验证,当它为空时,它会向我显示一条消息,上面写着“无数据”,否则它会向我显示列表的值。
What I want to remove the cancel icon when it is null and only appear when I have a value to display.我想在取消图标为空时删除它,并且仅在我有要显示的值时出现。
Help would be greatly appreciated.帮助将不胜感激。
Code and Image ::代码和图像 ::
Widget SfuluilderSuggestions(List <SDM> historialMenuPrincipal){
return Container(
child:StatefulBuilder(
builder:(context,setState)
{
return Container(
child: ListView.builder(
itemCount: historialMenuPrincipal.length,
itemBuilder: (context,i)
{
contentPadding: EdgeInsets.symmetric(vertical: 12,horizontal: 16);
leading:CircleAvatar(
radius: 32,
backgroundImage: NetworkImage(
"https://2.bp.blogspot.com/-3ZzNt8ZjQk/WR9W4Fn4II/AAAAAAAJw/_inTVynhS6V75IZ-461-pda7WyrTStwCEw/s1600/A.jpg"),
);
return
ListTile(
title: Text(historialMenuPrincipal[i] == null ? "no data":historialMenuPrincipal[i].email ),
trailing: IconButton(
icon: Icon(Icons.cancel,color: Colors.black,),
onPressed: () {
setState(() {
historialMenuPrincipal.remove(historialMenuPrincipal[i]);
});
},
),
);
}
),
);
}
)
);
}
You can check if the text is null -您可以检查文本是否为空 -
trailing: Text(historialMenuPrincipal[i] != null ? IconButton(
icon: Icon(Icons.cancel,color: Colors.black,),
onPressed: () {
setState(() {
historialMenuPrincipal.remove(historialMenuPrincipal[i]);
});
},
) : Padding(padding:EdgeInsets.zero),
While historialMenuPrincipal
contains data, you can remove only when data is available.虽然
historialMenuPrincipal
包含数据,但您只能在数据可用时删除。 You can pass null
on trailing
.您可以在
trailing
传递null
。
trailing:
historialMenuPrincipal.contains(historialMenuPrincipal[i])
? IconButton(...)
: null
If you want to skip the generating ListTile
, you can check on return level and provide SizedBox
or better filter data while generating the list.如果您想跳过生成
ListTile
,您可以在生成列表时检查返回级别并提供SizedBox
或更好的过滤数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.