Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 可重排序列表中的弹出菜单位置不正确_Flutter_Dart - Fatal编程技术网

Flutter 可重排序列表中的弹出菜单位置不正确

Flutter 可重排序列表中的弹出菜单位置不正确,flutter,dart,Flutter,Dart,有人知道为什么PopupMenuButton在ReorderableListView中使用时呈现在错误的位置吗?在普通ListView中使用时,它似乎可以正确渲染 以下是一个示例屏幕截图: 对于需要示例代码的用户: import 'package:flutter/material.dart'; void main() { runApp(ListApp()); } class ListApp extends StatefulWidget { @override _ListAppS

有人知道为什么PopupMenuButton在ReorderableListView中使用时呈现在错误的位置吗?在普通ListView中使用时,它似乎可以正确渲染

以下是一个示例屏幕截图:

对于需要示例代码的用户:

import 'package:flutter/material.dart';

void main() {
  runApp(ListApp());
}

class ListApp extends StatefulWidget {
  @override
  _ListAppState createState() => _ListAppState();
}

class _ListAppState extends State<ListApp> {
  List<String> items = [
    "Item 1",
    "Item 2",
    "Item 3",
    "Item 4",
    "Item 5",
    "Item 6",
    "Item 7",
  ];
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Reorder List'),
        ),
        body: Container(
          child: ReorderableListView(
              // child: ListView(

              onReorder: (oldIndex, newIndex) {
                int deleteIndex = oldIndex;
                items.insert(newIndex, items[oldIndex]);

                if (oldIndex > newIndex) {
                  // The old index is now 1 index higher
                  deleteIndex++;
                }
                items.removeAt(deleteIndex);
                setState(() {
                  items = items;
                });
              },

              children: items.map((item) {
                return Card(
                  key: Key(item),
                  child: ListTile(
                    title: Text(item),
                    trailing: PopupMenuButton(
                      itemBuilder: (context) {
                        return [
                          PopupMenuItem(
                            child: Text(item),
                            value: item,
                          )
                        ];
                      },
                    ),
                  ),
                );
              }).toList()),
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(ListApp());
}
类ListApp扩展StatefulWidget{
@凌驾
_ListAppState createState()=>\u ListAppState();
}
类_ListAppState扩展状态{
列表项=[
“项目1”,
“项目2”,
“项目3”,
“项目4”,
“项目5”,
“项目6”,
“项目7”,
];
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
标题:文本(“重新排序列表”),
),
主体:容器(
子:ReorderableListView(
//子:ListView(
onReorder:(旧索引,新索引){
int deleteIndex=oldIndex;
项目。插入(新索引,项目[oldIndex]);
如果(旧索引>新索引){
//旧指数现在高出1个指数
deleteIndex++;
}
项目移除(删除索引);
设置状态(){
项目=项目;
});
},
子项:items.map((item){
回程卡(
键:键(项),
孩子:ListTile(
标题:文本(项目),
拖尾:弹出菜单按钮(
itemBuilder:(上下文){
返回[
PopupMenuItem(
子项:文本(项),
价值:项目,,
)
];
},
),
),
);
}).toList()),
),
),
);
}
}
可能是我使用PopupMenuButton的方式不对,还是这个小部件有问题


我希望我的列表是可排序的,但弹出菜单按钮出现在错误的位置是一个问题。

我认为这是最好的,因为您可以在不点击小部件的情况下获得它


问题似乎应该出在ReorderableListView小部件的代码中,但实际上它隐藏在PopupMenu中。要计算弹出位置,请计算覆盖的渲染框:

final RenderBox overlay=overlay.of(context.context.findenderobject()作为RenderBox;
因为默认情况下,
Overlay.of
方法在层次结构中搜索最近的覆盖,在ReorderableListView的情况下,它返回内部覆盖(为限制拖动手势而创建的覆盖)但是对于ListView它返回根覆盖。因为根导航器用于显示弹出菜单,所以在前一种情况下会出现移位

使用
rootOverlay:true调用
Overlay.of
方法修复了错误:

final RenderBox overlay=overlay.of(context,rootOverlay:true).context.findenderobject()作为RenderBox;

修补flatter SDK是最合适的解决方案。

是的,我一直在胡思乱想,似乎使用偏移量是最好的方法。我最大的担心是列表的大小可能会影响偏移量。我会尝试一下,看看随着列表的增长会发生什么。我个人认为,列表中存在一个bugReorderableListView小部件。列表的大小不会影响偏移量。我在回答之前尝试过。如果你想找出弹出菜单出现的原因,那么你可以点击小部件
ReorderableListView
,并将其与
ListView
进行比较,正如你所说的那样,它与
PopupMenuButton
工具I配合得很好p长按后显示的文本也会定位(更多)不正确,我认为这是一个应该报告的错误。做得好,我有类似的补丁,但不太优雅!如果我奖励你,你会为你的解决方案提出请求吗?谢谢。@kapace不是现在,可能是下周。我必须调查
弹出菜单
选项的所有可能组合,因为它可以独立使用。嗨,我“我也面临同样的问题。在我的情况下,无论我设置了多少偏移量,第一个弹出菜单都会出现在appBar的高度范围内。我使用了两个Tabbar和一个ReorderableListView。我尝试了所有可能的解决方案,但没有成功。@Teh Sun Liu-这个问题,但还不在稳定分支中。请尝试切换到beta频道。”。
             PopupMenuButton(
                  offset: Offset(0, 50), //add offset to fix it
                  itemBuilder: (context) {
                    return [
                      PopupMenuItem(
                        child: Text(item),
                        value: item,
                      )