Skip to content

Instantly share code, notes, and snippets.

@manudevcode
Created May 8, 2019 19:55
Show Gist options
  • Save manudevcode/b65fafe1aa0c6dd2ff500cf621e7fb6f to your computer and use it in GitHub Desktop.
Save manudevcode/b65fafe1aa0c6dd2ff500cf621e7fb6f to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Draggable Example',
debugShowCheckedModeBanner: false,
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Draggable Example'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
bool accepted = false;
final simpleBox = (String text, Color color) => Padding(
padding: EdgeInsets.only(left: 10.0, right: 10.0),
child: Container(
width: 90.0,
height: 60.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: color
),
child: Center(
child: Text(text, style: TextStyle(fontSize: 15.0, color: Colors.white, fontWeight: FontWeight.bold),)
),
),
);
final dashedBox = () => Padding(
padding: EdgeInsets.only(left: 10.0, right: 10.0),
child: Container(
width: 90.0,
height: 60.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
border: Border.all(),
color: Colors.transparent
),
),
);
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text("Draggable/DragTarget example", style: TextStyle(fontSize: 19.0),),
SizedBox(height: 50.0,),
// This is de main widget, It is the widget
// that provides the ability to drag towards
// a target in our case a 'DragTarget'
Draggable(
// Here are the data to send to 'Drag Target'
// It can be of any type
data: "Hello!",
// You can set a default widget
child: simpleBox('Dragme!', Colors.blue),
// But also a widget that stays in the container
childWhenDragging: simpleBox("Bye!", Colors.grey),
// And a widget that will appear when you start to drag
feedback: simpleBox('Yupi!', Colors.blue[300])
),
SizedBox(height: 20.0,),
// This is the target that receives the data
DragTarget<String>(
// Called to build the contents of this widget
builder: (context, candidates, rejects) {
return accepted ? simpleBox('Thanks!', Colors.green) : dashedBox();
},
// Called to determine whether this widget is
// interested in receiving a given piece of data
// being dragged over this drag target.
onWillAccept: (value){
return true;
},
// Called when an acceptable piece of data was dropped over this drag target.
onAccept: (value) {
accepted = true;
},
// Called when a given piece of data being
// dragged over this target leaves the target.
onLeave: (value) {
accepted = true;
},
)
],
)
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment