我正在尝试创建一个可拖的容器,可以在屏幕周围拖动。我所面临的问题是可拖的总是回到它的预防假设。https://imgur.com/a/0ESoCZW,所以你怎么能创造出可拖的,停留在你的手指离开的位置。
发布于 2020-01-22 04:29:58
可以使用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);
},
),
),
],
);
}
}https://stackoverflow.com/questions/59846661
复制相似问题