gpt4 book ai didi

flutter - 在 flutter 中跟踪 x 和 y 位置的可拖动元素

转载 作者:行者123 更新时间:2023-12-02 02:41:58 29 4
gpt4 key购买 nike

我正在捕获图像并想对该图像进行一些计算,为此我需要获取图像上某些点的位置。为此,我使用了可拖动元素,我的想法是拖动一个元素到图像上,获取可拖动元素的 x 和 y 位置进行计算。

我使用了以下代码

body: Container(
child: Stack(
children: <Widget>[
Image.file(File(widget.imagePath)),
Draggable(
axis: Axis.vertical,

child: Text('_________', style: TextStyle(color: Colors.blue, fontSize: 90) ),
feedback:Text('____',style: TextStyle(color: Colors.blue, fontSize: 90)) ,
childWhenDragging: Text(''),

),

SizedBox(height: 50.0,),

],
),
)

问题是
1.当一个元素被拖动时,该元素的位置应该随着触摸而移动,当前拖动时,元素会移动几个像素。
2. 元素位置没有转移到最终拖动位置,当我移除触摸元素时回到原始位置。

请帮我解决一下这个。

最佳答案

使用 Draggable像这样,

  • 您需要能够设置元素的位置,例如 Positioned和它的 left , top .
  • 然后,您需要使用 dragDetails 获取拖动的结束坐标。来自 onDragEnd: (dragDetails) { }

  • 这是一个简单的示例,可以让您开始:
    Positioned(
    left: _x,
    top: _y,
    child: Draggable(
    child: FlutterLogo(size: _logoSize),
    feedback: FlutterLogo(size: _logoSize),
    childWhenDragging: Container(),
    onDragEnd: (dragDetails) {
    setState(() {
    _x = dragDetails.offset.dx;
    // if applicable, don't forget offsets like app/status bar
    _y = dragDetails.offset.dy - appBarHeight - statusBarHeight;
    });
    },
    ),
    )

    enter image description here

    也是 Draggable 的一个很好的介绍 medium/A Deep Dive Into Draggable and DragTarget in Flutter

    (如果你不想使用 Draggable,你也可以使用 GestureDetector 来完成同样的事情,但要多做一点工作)

    关于flutter - 在 flutter 中跟踪 x 和 y 位置的可拖动元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58910018/

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