gpt4 book ai didi

android - 可重新排序 ListView :Multiple widgets used the same GlobalKey

转载 作者:IT王子 更新时间:2023-10-29 07:23:39 30 4
gpt4 key购买 nike

我正在开发一个页面使用 ReorderableListView 小部件的应用程序。向上拖动 Tile 时出现错误,但是当我向下拖动任何 Tile 时它工作正常。该错误与多个小部件使用的全局 key 有关,但所有 key 彼此不同。任何帮助,将不胜感激。提前致谢。附上视频链接和代码以供引用。

class GurmatLekh extends StatefulWidget {
@override
_GurmatLekhState createState() => _GurmatLekhState();
}

Widget listTile(
{ String key, Widget widget, String title}) {
BuildContext context;
return Container(
key: Key(key),
decoration: BoxDecoration(
color: theme == 1 ? Colors.black : Colors.white,
borderRadius: BorderRadius.circular(5),
border: Border.all(color: Colors.lightBlue)),
child: ListTile(
onTap: () => Navigator.of(context)
.push(MaterialPageRoute(builder: (_) => widget)),
title: Text(
title,
style: TextStyle(
color: theme == 0 ? Colors.black : Colors.white,
fontFamily: "GurbaniAkhar",
fontSize: 20.0),
),
));
}

class _GurmatLekhState extends State<GurmatLekh> {
List<Widget> list = [
listTile(key: "1", title: "1", widget: listTile1()),//listTile1 is the next Page.
listTile(key: "2", title: "2", widget: listTile2()),//listTile2 is the next Page.
listTile(key: "3", title: "3", widget: listTile3()),//listTile3 is the next Page.
listTile(key: "4", title: "4", widget: listTile4()),//listTile4 is the next Page.
listTile(key: "5", title: "5", widget: listTile5()),//listTile5 is the next Page.
];

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color.fromRGBO(50, 42, 57, 1.0),
appBar: AppBar(
backgroundColor: Colors.lightBlue,
centerTitle: true,
title: Text(
"gurmiq lyK", //gurmat lekh
style: TextStyle(
fontFamily: "Prabhki",
fontSize: 27.0,
fontWeight: FontWeight.bold),
),
),
body: Stack(children: <Widget>[
Opacity(
opacity: 0.6,
child: Container(
color: Colors.teal,
),
),
ReorderableListView(
children: list,
onReorder: (oldIndex, newIndex) {
print(oldIndex);
print(newIndex);

if (newIndex > list.length) newIndex = list.length;
if (oldIndex < newIndex) newIndex--;

Widget widget = list[oldIndex];
list.remove(widget);
list.insert(newIndex, widget);
setState(() {});
},
),
]));
}
}

Here is the video for my error

这是AS控制台报错

I/flutter ( 6893): The following assertion was thrown building LongPressDraggable<Key>(state:
I/flutter ( 6893): _DraggableState<Key>#a6138):
I/flutter ( 6893): Multiple widgets used the same GlobalKey.
I/flutter ( 6893): The key [GlobalObjectKey ValueKey<String>#c5ac5] was used by multiple widgets. The parents of those
I/flutter ( 6893): widgets were:
I/flutter ( 6893): - Listener(listeners: [down], behavior: deferToChild, renderObject: RenderPointerListener#ef3da
I/flutter ( 6893): relayoutBoundary=up4 NEEDS-LAYOUT)
I/flutter ( 6893): - Listener(listeners: [down], behavior: deferToChild, renderObject: RenderPointerListener#78330
I/flutter ( 6893): relayoutBoundary=up3)
I/flutter ( 6893): A GlobalKey can only be specified on one widget at a time in the widget tree.
I/flutter ( 6893):
I/flutter ( 6893): When the exception was thrown, this was the stack:
I/flutter ( 6893): #0 GlobalKey._debugReserveFor.<anonymous closure> (package:flutter/src/widgets/framework.dart:172:11)
I/flutter ( 6893): #1 GlobalKey._debugReserveFor (package:flutter/src/widgets/framework.dart:190:6)
I/flutter ( 6893): #2 Element.updateChild.<anonymous closure> (package:flutter/src/widgets/framework.dart:2724:13)
I/flutter ( 6893): #3 Element.updateChild (package:flutter/src/widgets/framework.dart:2727:6)
I/flutter ( 6893): #4 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
I/flutter ( 6893): #5 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #6 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 6893): #7 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 6893): #8 StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
I/flutter ( 6893): #9 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #10 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
I/flutter ( 6893): #11 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #12 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 6893): #13 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 6893): #14 StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
I/flutter ( 6893): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #16 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 6893): #17 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 6893): #18 StatelessElement.update (package:flutter/src/widgets/framework.dart:3781:5)
I/flutter ( 6893): #19 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #20 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4585:32)
I/flutter ( 6893): #21 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4975:17)
I/flutter ( 6893): #22 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #23 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
I/flutter ( 6893): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #25 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 6893): #26 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 6893): #27 ProxyElement.update (package:flutter/src/widgets/framework.dart:3990:5)
I/flutter ( 6893): #28 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #29 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
I/flutter ( 6893): #30 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #31 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
I/flutter ( 6893): #32 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #33 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
I/flutter ( 6893): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #35 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
I/flutter ( 6893): #36 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #37 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 6893): #38 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 6893): #39 StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
I/flutter ( 6893): #40 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #41 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
I/flutter ( 6893): #42 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #43 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
I/flutter ( 6893): #44 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #45 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
I/flutter ( 6893): #46 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #47 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4867:14)
I/flutter ( 6893): #48 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #49 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 6893): #50 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 6893): #51 StatelessElement.update (package:flutter/src/widgets/framework.dart:3781:5)
I/flutter ( 6893): #52 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #53 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 6893): #54 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 6893): #55 StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
I/flutter ( 6893): #56 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #57 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 6893): #58 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 6893): #59 StatefulElement.update (package:flutter/src/widgets/framework.dart:3878:5)
I/flutter ( 6893): #60 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #61 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 6893): #62 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 6893): #63 StatelessElement.update (package:flutter/src/widgets/framework.dart:3781:5)
I/flutter ( 6893): #64 Element.updateChild (package:flutter/src/widgets/framework.dart:2742:15)
I/flutter ( 6893): #65 _LayoutBuilderElement._layout.<anonymous closure> (package:flutter/src/widgets/layout_builder.dart:118:18)
I/flutter ( 6893): #66 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2266:19)
I/flutter ( 6893): #67 _LayoutBuilderElement._layout (package:flutter/src/widgets/layout_builder.dart:107:11)
I/flutter ( 6893): #68 RenderObject.invokeLayoutCallback.<anonymous closure> (package:flutter/src/rendering/object.dart:1730:58)
I/flutter ( 6893): #69 PipelineOwner._enableMutationsToDirtySubtrees (package:flutter/src/rendering/object.dart:799:15)
I/flutter ( 6893): #70 RenderObject.invokeLayoutCallback (package:flutter/src/rendering/object.dart:1730:13)
I/flutter ( 6893): #71 _RenderLayoutBuilder.performLayout (package:flutter/src/widgets/layout_builder.dart:205:5)
I/flutter ( 6893): #72 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 6893): #73 RenderStack.performLayout (package:flutter/src/rendering/stack.dart:510:15)
I/flutter ( 6893): #74 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 6893): #75 __RenderTheatre&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 6893): #76 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 6893): #77 RenderStack.performLayout (package:flutter/src/rendering/stack.dart:510:15)
I/flutter ( 6893): #78 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 6893): #79 MultiChildLayoutDelegate.layoutChild (package:flutter/src/rendering/custom_layout.dart:142:11)
I/flutter ( 6893): #80 _ScaffoldLayout.performLayout (package:flutter/src/material/scaffold.dart:339:7)
I/flutter ( 6893): #81 MultiChildLayoutDelegate._callPerformLayout (package:flutter/src/rendering/custom_layout.dart:212:7)
I/flutter ( 6893): #82 RenderCustomMultiChildLayoutBox.performLayout (package:flutter/src/rendering/custom_layout.dart:356:14)
I/flutter ( 6893): #83 RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1509:7)
I/flutter ( 6893): #84 PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:768:18)
I/flutter ( 6893): #85 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:281:19)
I/flutter ( 6893): #86 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:677:13)
I/flutter ( 6893): #87 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:219:5)
I/flutter ( 6893): #88 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
I/flutter ( 6893): #89 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9)
I/flutter ( 6893): #90 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:842:5)
I/flutter ( 6893): #91 _invoke (dart:ui/hooks.dart:154:13)
I/flutter ( 6893): #92 _drawFrame (dart:ui/hooks.dart:143:3)
I/flutter ( 6893): ════════════════════════════════════════════════════════════════════════════════════════════════════

最佳答案

对于 future 的访客,如果有人遇到同样的问题,那么这很好地解决了我的问题。 Github issue link

我将 onReorder 方法更改为以下方法。基本上我将这个过程延迟了 100 毫秒并且它运行良好。

onReorder: (oldIndex, newIndex) {
if (newIndex > list.length) newIndex = list.length;
if (oldIndex < newIndex) newIndex--;


Future.delayed(Duration(milliseconds: 100), (){
setState(() {
final Widget item = list.removeAt(oldIndex);
list.insert(newIndex, item);
});
});
},

关于android - 可重新排序 ListView :Multiple widgets used the same GlobalKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54171563/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com