【问题标题】:How to make Cards draggable in Flutter如何让卡片在 Flutter 中可拖动
【发布时间】:2020-12-16 06:17:09
【问题描述】:

我有一个存储在“待办事项”类型列表中的任务列表。我想遍历列表并为每个项目创建一张卡片,并且我希望能够在长按时拖动这些卡片并重新排序。

这是我获得今天任务的方式:

List<Todo> todayTasks = [];
      for (var i = 0; i < _todos.length; i++) {
        if (_todos[i].deadline != null) {
          if (_todos[i].deadline.substring(0, 11) ==
              todayDate.toString().substring(0, 11)) {
            todayTasks.add(_todos[i]);
          }
        }
      }

这就是我创建卡片的方式:

for (var todo in todayTasks)
              new Card(
                shadowColor: Colors.black,
                child: ListTile(
                  onTap: () => model.updateTodo(
                      todo.copy(isCompleted: todo.isCompleted == 1 ? 0 : 1)),
                  contentPadding:
                      EdgeInsets.symmetric(horizontal: 0, vertical: 8.0),
                  leading: CircularCheckBox(
                    onChanged: (value) =>
                        model.updateTodo(todo.copy(isCompleted: value ? 1 : 0)),
                    value: todo.isCompleted == 1 ? true : false,
                    materialTapTargetSize: MaterialTapTargetSize.padded,
                  ),
                  trailing: IconButton(
                    icon: Icon(Icons.delete_outline),
                    onPressed: () => model.removeTodo(todo),
                  ),
                  title: Text(
                    todo.name,
                    style: TextStyle(
                      fontSize: 18.0,
                      fontFamily: 'Roboto',
                      fontWeight: FontWeight.w600,
                      color: todo.isCompleted == 1
                          ? Colors.grey[500]
                          : Colors.black,
                      decoration: todo.isCompleted == 1
                          ? TextDecoration.lineThrough
                          : TextDecoration.none,
                    ),
                  ),
                ),
              ),

【问题讨论】:

标签: flutter dart draggable


【解决方案1】:

我有两件事给你,你可以得到你想要的结果:

  • 你能做的最好的事情就是使用ReorderableListView class,它是可增长的。因此,您可以将项目添加到您的列表中。
  • 您可以选择的另一种聪明方法是使用软件包,这会使您的工作更轻松。有一个包给你,命名为reorderables 0.3.2

请使用指针,你会很高兴:)

【讨论】:

    猜你喜欢
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2020-09-14
    • 2020-08-20
    • 2019-11-04
    • 2022-08-24
    相关资源
    最近更新 更多