Created
October 21, 2019 09:01
-
-
Save k4zek4ge/19c22013b89cf14ac30e2cd4a196c695 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: App(), | |
), | |
); | |
} | |
} | |
class App extends StatefulWidget { | |
@override | |
_AppState createState() => _AppState(); | |
} | |
class _AppState extends State<App> { | |
Color caughtColor = Colors.grey; | |
@override | |
Widget build(BuildContext context) { | |
return Stack( | |
children: <Widget>[ | |
DragBox(Offset(0.0, 0.0), Colors.green), | |
Positioned( | |
left: 100, | |
bottom: 0.0, | |
child: DragTarget(onAccept: (Color color) { | |
caughtColor = color; | |
}, builder: ( | |
BuildContext context, | |
List<dynamic> accepted, | |
List<dynamic> rejected, | |
) { | |
return Container( | |
width: 200, | |
height: 200, | |
color: accepted.isEmpty ? caughtColor : Colors.grey.shade200, | |
); | |
})) | |
], | |
); | |
} | |
} | |
class DragBox extends StatefulWidget { | |
final Offset initPos; | |
final Color itemColor; | |
DragBox(this.initPos, this.itemColor); | |
@override | |
_DragBoxState createState() => _DragBoxState(); | |
} | |
class _DragBoxState extends State<DragBox> { | |
Offset position = Offset(0.0, 0.0); | |
@override | |
void initState() { | |
super.initState(); | |
position = widget.initPos; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Positioned( | |
left: position.dx, | |
top: position.dy, | |
child: Draggable( | |
data: widget.itemColor, | |
child: Container( | |
width: 100, | |
height: 100, | |
color: widget.itemColor, | |
), | |
onDraggableCanceled: (velocity, offset) { | |
setState(() { | |
position = offset; | |
}); | |
}, | |
feedback: Container( | |
width: 120, | |
height: 120, | |
color: widget.itemColor.withOpacity(0.5), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment