You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中如何为Dismissible组件添加垃圾桶图标?

实现Dismissible组件添加垃圾桶图标

你只需要将background里的Text组件替换成Row组件,同时放入垃圾桶图标和文本即可,具体修改后的代码如下:

return Dismissible(
  key: UniqueKey(),
  background: Container(
    alignment: Alignment.center,
    color: Colors.redAccent,
    child: Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: const [
        Icon(
          Icons.delete,
          color: Colors.white,
        ),
        SizedBox(width: 8), // 控制图标和文本的间距
        Text(
          'Delete',
          style: TextStyle(
            color: Colors.white,
          ),
        ),
      ],
    ),
  ),
  onDismissed: (direction) async {
    await deletePost(listpost![index].id.toString());
    setState(() {
      listpost!.removeAt(index);
      fetchAllPosts().then((value) => listpost = value);
    });
  },
);

关键说明

  • 使用Row组件将图标和文本横向排列,通过mainAxisAlignment: MainAxisAlignment.center保证整体居中
  • 用SizedBox设置图标与文本的间距,提升视觉体验
  • 图标使用Flutter内置的Icons.delete,颜色和文本保持一致为白色,也可以根据需求添加size参数调整图标大小

内容的提问来源于stack exchange,提问作者Hana.HD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 13:35:55