我正在尝试创建一个可拖动的容器,该容器可在屏幕周围拖动。我面临的问题是Draggable总是返回到优先地位。 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);
},
),
),
],
);
}
}
关于flutter - 使Draggable在取消时不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59846661/