无法在Flutter中重新排序列表

3j86kqsm  于 2023-05-23  发布在  Flutter
关注(0)|答案(2)|浏览(174)

我正在尝试创建一个可以使用Flutter/Dart重新订购的成分列表。我使用ReorderableListViewReorderableDragStartListener。我可以拖动这些项目,但它们不会重新排序。他们只是回到了原来的地方。数据(成分)来自一个sqlite查询(我使用的是sqflite库)。
这是我的代码:

@override
Widget build(BuildContext context) {
  List<Map<String, dynamic>> ingredientsOrderable = List<Map<String, dynamic>>.from(ingredients);

  return Scaffold(
    body: SafeArea(
      child: ReorderableListView(
        onReorder: (int oldIndex, int newIndex) {
          setState(() {
            if (newIndex > oldIndex) {
              newIndex -= 1;
            }
            final ingredient = ingredientsOrderable.removeAt(oldIndex);
            ingredientsOrderable.insert(newIndex, ingredient);
          });
        },
        children: ingredientsOrderable.asMap().entries.map((entry) {
          final index = entry.key;
          final ingredient = entry.value;
          return ReorderableDragStartListener(
            index: index,
            key: Key(ingredient["ID"].toString()),
            child: ListTile(
              onTap: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) =>
                        IngredientDetailPage(widget.db, ingredient["ID"]),
                  ),
                );
              },
              title: Text(ingredient["Name"]),
            ),
          );
        }).toList(),
      ),
    ),
  );
}
i34xakig

i34xakig1#

您的列表需要在State中,而不是在build()中。每次页面刷新时(重新排序时必须刷新),都将列表重新初始化为原始顺序。啊!

4si2a6ki

4si2a6ki2#

算了,我忘了把名单复制回原件。还有其他事情,但基本上就是这样。

@override
    Widget build(BuildContext context) {
      List<Map<String, dynamic>> ingredientsOrderable = List<Map<String, dynamic>>.from(ingredients);

      return Scaffold(
        body: SafeArea(
          child: ReorderableListView(
            key: Key('reorderable_list'),
            onReorder: (int oldIndex, int newIndex) {
              setState(() {
                if (newIndex > oldIndex) {
                  newIndex -= 1;
                }
                final ingredient = ingredientsOrderable.removeAt(oldIndex);
                ingredientsOrderable.insert(newIndex, ingredient);
                ingredients = List<Map<String, dynamic>>.from(ingredientsOrderable);
              });
            },
            children: ingredientsOrderable.asMap().entries.map((entry) {
              final index = entry.key;
              final ingredient = entry.value;
              final key = Key(ingredient["ID"].toString());

              return ListTile(
                key: key,
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) =>
                          IngredientDetailPage(widget.db, ingredient["ID"]),
                    ),
                  );
                },
                title: Text(ingredient["Name"]),
                leading: ReorderableDragStartListener(
                  index: index,
                  key: key,
                  child: Icon(Icons.drag_handle),
                ),
              );
            }).toList(),
          ),
        ),
      );
    }

相关问题