Created
January 28, 2021 20:46
-
-
Save KDCinfo/da9dbe19bbdcf73e5309663a4d50dbcb to your computer and use it in GitHub Desktop.
Flutter: Draggable and DragTarget Annotated
This file contains hidden or 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'; | |
/// Inspiration for how the DragTarget hovering works goes to: | |
/// Stack Overflow A: https://stackoverflow.com/a/53981607/638153 | |
/// Stack Overflow Q: https://stackoverflow.com/questions/53979586/flutter-dragtarget-on-hover | |
final Color darkBlue = Color.fromARGB(255, 18, 32, 45); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: MyWidget(), | |
), | |
), | |
); | |
} | |
} | |
class MyWidget extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
Widget helloText(String showText, Color showColor) => Container( | |
child: Text(showText, style: Theme.of(context).textTheme.headline4), | |
width: 150, | |
height: 150, | |
color: showColor); | |
return Row( | |
mainAxisAlignment: MainAxisAlignment.spaceAround, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
Expanded( | |
child: | |
Column(mainAxisAlignment: MainAxisAlignment.center, children: [ | |
Draggable<int>( | |
data: 1, | |
maxSimultaneousDrags: 1, | |
child: helloText("D-1-C", Colors.red), | |
feedback: helloText("D-1-F", Colors.pink), | |
childWhenDragging: helloText("D-1-CwD", Colors.amber), | |
), | |
Draggable( | |
data: 2, | |
maxSimultaneousDrags: 1, | |
child: helloText("D-2-C", Colors.yellow), | |
feedback: helloText("D-2-F", Colors.green), | |
childWhenDragging: helloText("D-2-CwD", Colors.deepPurple), | |
), | |
Draggable( | |
data: 3, | |
maxSimultaneousDrags: 1, | |
child: helloText("D-3-C", Colors.blue), | |
feedback: helloText("D-3-F", Colors.orange), | |
childWhenDragging: helloText("D-3-CwD", Colors.deepOrange), | |
), | |
]), | |
), | |
Container( | |
height: 300, | |
width: 300, | |
color: Colors.white, | |
child: DragTarget<int>( | |
builder: (context, List<int> data, rejectedData) { | |
print("builder: $data"); | |
if (data.isNotEmpty) { | |
if (data[0] == 1) { | |
return Container( | |
height: 100, | |
width: 100, | |
color: Colors.white, | |
child: helloText("DT-C #1", Colors.green), | |
); | |
} | |
return Container( | |
height: 100, | |
width: 100, | |
color: Colors.white30, | |
child: helloText("DT-C #2 or #3", Colors.lightBlue), | |
); | |
} | |
return Container( | |
height: 100, | |
width: 100, | |
color: Colors.white54, | |
child: helloText("DT-C", Colors.purple), | |
); | |
}, | |
onAccept: (data) { | |
print(data); | |
}, | |
onWillAccept: (data) => true, | |
onLeave: (data) {}, | |
), | |
), | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment