flutter 多个小部件使用的ReorderableListViewChildGlobalKey

fumotvh3  于 2023-04-13  发布在  Flutter
关注(0)|答案(1)|浏览(113)

我知道每个项目都需要一个密钥来唯一标识它,特别是因为它们可以移动。我得到的错误是:
多个小部件使用了键[_ReorderableListViewChildGlobalKey ValueKey#46dc3
我的列表中的每一项都已经在我的可关闭小部件中被唯一标识。

Widget mainCardWidget(BuildContext context) {
    return ReorderableListView(
      onReorder: onReorder,
      physics: const BouncingScrollPhysics(
          parent: AlwaysScrollableScrollPhysics()),
      children: _getListItems(),
    );
  }

  void onReorder(int oldIndex, int newIndex) {
    setState(() {
      if (newIndex > oldIndex) {
        newIndex -= 1;
      }
      final Rung element = _items.removeAt(oldIndex);
      _items.insert(newIndex, element);
    });
  }

  List<Widget> _getListItems() => _items
      .asMap()
      .map((i, item) => MapEntry(i, _buildTenableListTile(item, i)))
      .values
      .toList();

  Widget _buildTenableListTile(Rung item, int index) {
    return Dismissible(
      key: Key(item.rungId),
      onDismissed: (direction) {
        setState(() {
          _items.removeAt(index);
          _removeditems.add(item);
        });
      },
      background: Container(color: Colors.red),
      child: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Card(child: cardWithInfoPage(item, context, index)),
      ),
    );
  }
camsedfj

camsedfj1#

我收到了同样的错误,主要原因是我在initState中填写了我的item.id,所以我把责任转移到了我的StatefullObject之外,它对我很有效。

void main() {
  runApp(
    MaterialApp(
      home: ReorderableListModelView(
        items: [
          ItemModel(iconReorderable: Icons.menu, replaced: Icons.ac_unit, text: "Teste 1", id: 0),
          ItemModel(iconReorderable: Icons.menu, replaced: Icons.ac_unit, text: "Teste 2", id: 1),
          ItemModel(iconReorderable: Icons.menu, replaced: Icons.ac_unit, text: "Teste 3", id: 2),
          ItemModel(iconReorderable: Icons.menu, replaced: Icons.ac_unit, text: "Teste 4", id: 3),
        ],
      ),
    ),
  );
}

class ReorderableListModelView extends StatefulWidget {
  final List<ItemModel> items;
  const ReorderableListModelView({super.key, required this.items});

  @override
  State<ReorderableListModelView> createState() => _ReorderableListModelViewState();
}

class _ReorderableListModelViewState extends State<ReorderableListModelView> {
  final items = <ItemModel>[];

  @override
  void initState() {
    setState(() {
      items.addAll(widget.items);
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: ReorderableListView(
        shrinkWrap: true,
        onReorder: (oldIndex, newIndex) {
          setState(() {
            final item = items[oldIndex];
            items.removeAt(oldIndex);
            items.insert(newIndex, item);
          });
        },
        children: [
          for (int i = 0; i < items.length; i++)
            ListTile(
              key: ValueKey<String>(items[i].id.toString()),
              title: Text(items[i].text),
            )
        ],
      ),
    );
  }
}

相关问题