首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >取消时使拖放不返回

取消时使拖放不返回
EN

Stack Overflow用户
提问于 2020-01-21 17:52:36
回答 1查看 91关注 0票数 1

我正在尝试创建一个可拖的容器,可以在屏幕周围拖动。我所面临的问题是可拖的总是回到它的预防假设。https://imgur.com/a/0ESoCZW,所以你怎么能创造出可拖的,停留在你的手指离开的位置。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-22 04:29:58

可以使用onDraggableCanceled属性更改位置。

我希望下面的例子能澄清你的想法。

代码语言:javascript
复制
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  double width = 100.0, height = 100.0;
  Offset position;

  @override
  void initState() {
    super.initState();
    position = Offset(0.0, height - 20);
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Positioned(
          left: position.dx,
          top: position.dy - height + 20,
          child: Draggable(
            child: Container(
              width: width,
              height: height,
              color: Colors.blue,
              child: Center(
                child: Text(
                  "Drag",
                  style: Theme.of(context).textTheme.headline,
                ),
              ),
            ),
            feedback: Container(
              child: Center(
                child: Text(
                  "currently dragging",
                  style: Theme.of(context).textTheme.headline,
                ),
              ),
              color: Colors.blue[300],
              width: width,
              height: height,
            ),
            onDraggableCanceled: (Velocity velocity, Offset offset) {
              setState(() => position = offset);
            },
          ),
        ),
      ],
    );
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59846661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档