I want to make a draggable floating button and if possible to print the drag position in the console.
Due to the ability to compose Flutter widgets in whatever way you want to, you can simply do it like this:
Draggable(
feedback: FloatingActionButton(child: Icon(Icons.drag_handle), onPressed: () {}),
child: FloatingActionButton(child: Icon(Icons.drag_handle), onPressed: () {}),
childWhenDragging: Container(),
onDragEnd: (details) => print(details.offset))
This Draggable
can be passed as the floatingActionButton
argument to a Scaffold
.
The feedback
parameter controls how the widget that is dragged about in the end will look like and child
specifies how the stationary Draggable
should look like. As you probably only want to see one FloatingActionButton
at a time, you can pass an empty Container
to childWhenDragging
.
The Offset
(position) of the drag will be printed to the console when releasing the drag.
I needed to add a positioned widget to set the position of my button
class _MyHomePageState extends State<MyHomePage> {
Offset position =Offset(20.0, 20.0);
@override
Widget build(BuildContext context) {
return new Scaffold(
body: Stack(
children: <Widget>[
Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage("data_repo/img/bg1.jpg"),
fit: BoxFit.cover,
),
),
),
Positioned(
left : position.dx,
top : position.dy ,
child : Draggable(
feedback: Container(
child : FloatingActionButton(child: Icon(Icons.add), onPressed: () {})
),
child: Container(
child : FloatingActionButton(child: Icon(Icons.add), onPressed: () {}),
),
childWhenDragging: Container(),
onDragEnd: (details){
setState(() {
position = details.offset;
});
print(position);
print(position.dx);
print(position.dy);
}))
],
));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With