关键字:Draggable
import 'package:flutter/material.dart';
class DraggableWiget extends StatefulWidget {
final Offset offset;
final Color widgetColor;
DraggableWiget({Key key,this.offset,this.widgetColor}) : super(key: key);
@override
_DraggableWigetState createState() => _DraggableWigetState();
}
class _DraggableWigetState extends State<DraggableWiget> {
Offset offset = Offset(0.0, 0.0);
@override
void initState() {
// TODO: implement initState
super.initState();
offset = widget.offset;
}
@override
Widget build(BuildContext context) {
return Positioned(
left: offset.dx,
top: offset.dy,
child: Draggable(
data: widget.widgetColor,
child: Container(
width: 100.0,
height: 100.0,
color: widget.widgetColor,
),
feedback: Container(
width: 100.0,
height: 100.0,
color: widget.widgetColor.withOpacity(0.5),
),
onDraggableCanceled: (Velocity velocity,Offset offset){
setState(() {
this.offset = offset;
});
},
));
}
}
关键词:DragTarget
import 'package:flutter/material.dart';
import 'draggabe_widget.dart';
class DraggableDemo extends StatefulWidget {
DraggableDemo({Key key}) : super(key: key);
@override
_DraggableDemoState createState() => _DraggableDemoState();
}
class _DraggableDemoState extends State<DraggableDemo> {
Color _draggableColor = Colors.grey;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
DraggableWiget(
offset: Offset(80.0,80.0),
widgetColor: Colors.redAccent,
),
DraggableWiget(
offset: Offset(180.0,80.0),
widgetColor: Colors.blueAccent,
),
Center(
child: DragTarget(onAccept: (Color color){
_draggableColor = color;
},builder: (context,candidateData,rejectedData){
return Container(
width: 200.0,
height: 200.0,
color: _draggableColor,
);
},),
),
],
),
);
}
}