flutter - 使 Draggable 在取消时不返回
问题描述
我正在尝试创建一个可以在屏幕上拖动的可拖动容器。我面临的问题是 Draggable 总是回到它的 prevouis 位置。https://imgur.com/a/0ESoCZW 那么,您如何创建停留在您手指左手位置的 Draggable。
解决方案
您可以使用onDraggableCanceled属性来更改位置。
我希望下面的例子能清除你的想法。
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);
},
),
),
],
);
}
}
推荐阅读
- reactjs - 如何允许用户保存输入以供进一步审查
- typescript - 使用 TypeORM 和带有文件夹路径的 Typescript 时出错
- c++ - 如何从指针的指针向量中获取指针值?
- tensorflow - 如何为时间序列异常检测提供这个 LSTM-AE?
- javascript - PostMan 将 JSON post 请求中的空正文发送到 Express 服务器
- python - 如何保护我的 ren'py 项目不被复制?
- clickhouse - clickhouse 是否提供任何工具,例如 red gate sql toolbelt
- linux - 在 apache 配置中未显示预期输出
- node.js - 为什么我不能将 @bundled-es-modules/chai 别名为 chai?
- python - 在 Matplotlib 中同时使用 blitting 为一个子图设置动画,而在 Matplotlib 中通过重绘为另一个子图设置动画