|
| 1 | +/// |
| 2 | +/// Created by NieBin on 2019/6/11 |
| 3 | +/// Github: https://github.com/nb312 |
| 4 | +/// Email: niebin312@gmail.com |
| 5 | +/// |
| 6 | +import "package:flutter/material.dart"; |
| 7 | +import "package:flutter_widgets/const/_const.dart"; |
| 8 | + |
| 9 | +class DraggablePage extends StatefulWidget { |
| 10 | + @override |
| 11 | + _DragState createState() => _DragState(); |
| 12 | +} |
| 13 | + |
| 14 | +class _DragState extends State<DraggablePage> { |
| 15 | + String _value = ""; |
| 16 | + |
| 17 | + Widget _draggable(String _data) => Draggable<String>( |
| 18 | + child: Text( |
| 19 | + "$_data", |
| 20 | + style: TextStyle(color: RED_LIGHT, fontSize: 30), |
| 21 | + ), |
| 22 | + feedback: Text( |
| 23 | + "$_data", |
| 24 | + style: TextStyle(color: BLUE_DEEP), |
| 25 | + ), |
| 26 | + childWhenDragging: Container(), |
| 27 | + maxSimultaneousDrags: 1, |
| 28 | + onDragCompleted: () { |
| 29 | + print("Complete."); |
| 30 | + }, |
| 31 | + onDragStarted: () { |
| 32 | + print("Start"); |
| 33 | + }, |
| 34 | + onDraggableCanceled: (v, off) { |
| 35 | + print("Cancel"); |
| 36 | + }, |
| 37 | + data: _data, |
| 38 | + ); |
| 39 | + |
| 40 | + Widget _target() => DragTarget<String>( |
| 41 | + builder: (context, List<String> candidateData, rejectedData) => |
| 42 | + Container( |
| 43 | + constraints: BoxConstraints.expand(height: 100), |
| 44 | + color: GREEN, |
| 45 | + alignment: Alignment.center, |
| 46 | + child: Text( |
| 47 | + candidateData.length > 0 ? candidateData[0] : _value, |
| 48 | + style: TextStyle(color: Colors.white, fontSize: 30), |
| 49 | + ), |
| 50 | + ), |
| 51 | + onAccept: (value) { |
| 52 | + print("Accept value= $value"); |
| 53 | + _value = value; |
| 54 | + }, |
| 55 | + onLeave: (value) { |
| 56 | + print("leave = $value"); |
| 57 | + }, |
| 58 | + onWillAccept: (value) { |
| 59 | + print("Will Accept: value = $value"); |
| 60 | + return true; |
| 61 | + }, |
| 62 | + ); |
| 63 | + |
| 64 | + @override |
| 65 | + Widget build(BuildContext context) { |
| 66 | + return Scaffold( |
| 67 | + appBar: AppBar( |
| 68 | + title: Text(PageName.INTER_DRAG), |
| 69 | + ), |
| 70 | + body: Column( |
| 71 | + children: <Widget>[ |
| 72 | + Text(" You should drag the text and put it into the green area," |
| 73 | + "then you can see the text changed."), |
| 74 | + _draggable("Hello"), |
| 75 | + _draggable("World"), |
| 76 | + _target(), |
| 77 | + ], |
| 78 | + )); |
| 79 | + } |
| 80 | +} |
0 commit comments