Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.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
Dart 可拖动的物品总是下降约80像素_Dart_Flutter - Fatal编程技术网

Dart 可拖动的物品总是下降约80像素

Dart 可拖动的物品总是下降约80像素,dart,flutter,Dart,Flutter,我对一件拖拉物品有奇怪的行为 每次我把它拖到我的堆栈上,它都会从下降位置下降大约80像素(这是一个近似值,也许用另一部手机它可能是其他东西)。对我来说,x值似乎没问题 代码如下: class GesturePage extends StatefulWidget { @override _GesturePageState createState() => _GesturePageState(); } class _GesturePageState extends State<

我对一件拖拉物品有奇怪的行为

每次我把它拖到我的堆栈上,它都会从下降位置下降大约80像素(这是一个近似值,也许用另一部手机它可能是其他东西)。对我来说,x值似乎没问题

代码如下:

class GesturePage extends StatefulWidget {
  @override
  _GesturePageState createState() => _GesturePageState();
}

class _GesturePageState extends State<GesturePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Gesture"),
      ),
      body: Stack(
        children: <Widget>[
          DragBox(Offset(0.0, 80.0)),
        ],
      ),
    );
  }
}

class DragBox extends StatefulWidget {
  final Offset initPosition;    
  DragBox(this.initPosition);

  @override
  DragBoxState createState() => DragBoxState();
}

class DragBoxState extends State<DragBox> {
  Offset position = Offset(0.0, 0.0);

  @override
  void initState() {
    super.initState();
    position = widget.initPosition;
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
      left: position.dx,
      top: position.dy - 80.0,
      child: Draggable(
        data: widget.itemColor,
        child: Container(
          child: Image.asset('images/item.png'),
        ),
        onDraggableCanceled: (velocity, offset) {
          setState(() {
            position = offset;
          });
        },
        feedback: Container(
          child: Image.asset('images/item.png'),
        ),
      ),
    );
  }
}
class GesturePage扩展StatefulWidget{
@凌驾
_GesturePageState createState()=>\u GesturePageState();
}
类_GesturePageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“手势”),
),
主体:堆栈(
儿童:[
拖曳箱(偏移量(0.0,80.0)),
],
),
);
}
}
类DragBox扩展StatefulWidget{
最终偏移位置;
DragBox(此.initPosition);
@凌驾
DragBoxState createState()=>DragBoxState();
}
类DragBoxState扩展了状态{
偏移位置=偏移(0.0,0.0);
@凌驾
void initState(){
super.initState();
position=widget.initPosition;
}
@凌驾
小部件构建(构建上下文){
返回定位(
左:position.dx,
顶部:position.dy-80.0,
孩子:拖拉(
数据:widget.itemColor,
子:容器(
子项:Image.asset('images/item.png'),
),
Ondraggabelecanceled:(速度、偏移){
设置状态(){
位置=偏移量;
});
},
反馈:集装箱(
子项:Image.asset('images/item.png'),
),
),
);
}
}
有人知道发生了什么吗

此外,是否有一种方法可以在拖动项目时实时查看x和y坐标

编辑:
在源代码中查找80.0。这解决了我的问题,但只针对一种模拟器类型。还有另一个有趣的代码

是的,事情很简单。y轴上的80个差值包括

24 px = For Status bar
56 px = For App bar / Navigation bar

Total = 80 px
这是因为你正在接收全局位置,你需要将其转换为本地位置。这是你应该做的

RenderBox referenceBox = context.findRenderObject();
Offset localPosition = referenceBox.globalToLocal(yourGlobalOffset);
编辑:

  • 将您的
    定位为
    堆栈的子级

  • 用我的代替你的

    onDraggableCanceled: (velocity, offset) {
      setState(() {
        RenderBox renderBox = context.findRenderObject();
        _position = renderBox.globalToLocal(offset);
      });
    },
    

  • 嗨@CopsOnRoad,我有点迷路了。我尝试在onDraggableCanceled中添加两行,将“yourGlobalOffset”更改为“offset”,并将偏移值指定给“position”。这是最糟糕的!我知道我需要使用两条线,但似乎我没有正确使用它们。嗨@CopsOnRoad,它部分工作。我有两张照片。一个在(0.0,0.0)处,另一个在(200.0,0.0)处。如果移动第一个,一切都很完美,但是当将第二个向下移动到Y轴时,Y值是正确的,但是X变得接近0.0!然后,之后,我可以完美地移动两者!有没有想过为什么GlobalTopolocal会改变第二个图像的X值?再次感谢!谢谢@CopsOnRoad。你说对了!我不得不在前面的回答中做一些技巧(
    differenceY
    isChecked
    ,如果你还记得的话),因为我使用了你的帖子,它将
    定位在小部件树的顶部。然后我不得不调试,发现
    定位
    需要放在
    堆栈中