admin管理员组

文章数量:1410737

I'm using a Dismissible widget to allow users to delete tasks from a ListView. However, the red background of the Dismissible is larger than the actual card size, causing extra spacing between the list items.

Here's my current code for rendering each item:

Widget _dismissibleCard(Task task) {
    return Dismissible(
      key: UniqueKey(),
      background: Container(
        alignment: Alignment.centerRight,
        color: Colors.red,
        child: Icon(
          Icons.delete,
          color: white,
          size: 30,
        ),
      ),
      child: CustomCard(
          title: task.taskDetail,
          subtitle: task.taskDetail,
          cardColor: cardColor),
      onDismissed: (direction) async {
        await service.removeTasks(task.key!);
        setState(() {
          taskList.remove(task);
        });
      },
    );
  }

I have tried:

  • Setting a fixed height for Dismissible
  • Wrapping Dismissible with a SizedBox
  • Adjusting padding/margins But none of these have worked. How can I make the red background match the exact height of my CustomCard, so it doesn’t create additional space in the list?

I'm using a Dismissible widget to allow users to delete tasks from a ListView. However, the red background of the Dismissible is larger than the actual card size, causing extra spacing between the list items.

Here's my current code for rendering each item:

Widget _dismissibleCard(Task task) {
    return Dismissible(
      key: UniqueKey(),
      background: Container(
        alignment: Alignment.centerRight,
        color: Colors.red,
        child: Icon(
          Icons.delete,
          color: white,
          size: 30,
        ),
      ),
      child: CustomCard(
          title: task.taskDetail,
          subtitle: task.taskDetail,
          cardColor: cardColor),
      onDismissed: (direction) async {
        await service.removeTasks(task.key!);
        setState(() {
          taskList.remove(task);
        });
      },
    );
  }

I have tried:

  • Setting a fixed height for Dismissible
  • Wrapping Dismissible with a SizedBox
  • Adjusting padding/margins But none of these have worked. How can I make the red background match the exact height of my CustomCard, so it doesn’t create additional space in the list?

Share Improve this question edited Mar 13 at 17:29 Md. Yeasin Sheikh 64k7 gold badges47 silver badges82 bronze badges asked Mar 9 at 3:58 Zehra Gül YıldızZehra Gül Yıldız 133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can remove all the fixed height;

Widget get _listView => ListView.builder(
      itemCount: taskList.length,
      itemBuilder: (context, index) => _dismissibleCard(
        taskList[index],
      ),
    );

Also from _dismissibleCard

 Widget _dismissibleCard(Task task) {
    return Dismissible(
      key: UniqueKey(),
      background: Container(
        alignment: Alignment.centerRight,
        color: Colors.red,
        child: Icon(Icons.delete, color: white),
      ),
      child: SizedBox(
        child: CustomCard(title: task.taskDetail, subtitle: task.taskDetail, cardColor: cardColor),
      ),
      onDismissed: (direction) async {
        await service.removeTasks(task.key!);
        setState(() {
          taskList.remove(task);
        });
      },
    );
  }

本文标签: Flutter Dismissible widget causes extra spacing between ListView itemsStack Overflow