Advertisement
Guest User

stack-based-solution

a guest
Mar 16th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.67 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. void main() => runApp(new MyApp());
  4.  
  5. class MyApp extends StatelessWidget {
  6.   @override
  7.   Widget build(BuildContext context) {
  8.     return new MaterialApp(
  9.       home: new Scaffold(
  10.         body: new FillBlanks(),
  11.         backgroundColor: Colors.green,
  12.       ),
  13.     );
  14.   }
  15. }
  16.  
  17. class FillBlanks extends StatefulWidget {
  18.   @override
  19.   _AppState createState() => new _AppState();
  20. }
  21.  
  22. class _AppState extends State<FillBlanks> {
  23.   Color caughtcolor = Colors.grey;
  24.   @override
  25.   Widget build(BuildContext context) {
  26.     return new Stack(
  27.       children: <Widget>[
  28.         new DragBox(new Offset(30.0, 20.0), 'P', Colors.blueAccent),
  29.         new DragBox(new Offset(120.0, 20.0), 'S', Colors.orange),
  30.         new DragBox(new Offset(230.0, 20.0), 'T', Colors.pink),
  31.         new DragBox(new Offset(330.0, 20.0), 'L', Colors.yellow),
  32.         new Positioned(
  33.           //   First position
  34.           bottom: 20.0,
  35.           left: 30.0,
  36.           child: new DragTarget(
  37.             onAccept: (Color color) {
  38.               caughtcolor = color;
  39.             },
  40.             builder: (
  41.               BuildContext context,
  42.               List<dynamic> accepted,
  43.               List<dynamic> rejected,
  44.             ) {
  45.               return new Container(
  46.                 width: 60.0,
  47.                 height: 60.0,
  48.                 decoration: new BoxDecoration(
  49.                   color: accepted.isEmpty ? caughtcolor : Colors.grey.shade200,
  50.                 ),
  51.                 child: new Center(
  52.                   child: new Text("A"),
  53.                 ),
  54.               );
  55.             },
  56.           ),
  57.         ),
  58.         new Positioned(
  59.           left: 130.0,
  60.           bottom: 20.0,
  61.           child: new DragTarget(
  62.             onAccept: (Color color) {
  63.               caughtcolor = color;
  64.             },
  65.             builder: (
  66.               BuildContext context,
  67.               List<dynamic> accepted,
  68.               List<dynamic> rejected,
  69.             ) {
  70.               return new Container(
  71.                 width: 60.0,
  72.                 height: 60.0,
  73.                 decoration: new BoxDecoration(
  74.                   color: accepted.isEmpty ? caughtcolor : Colors.grey.shade200,
  75.                 ),
  76.                 child: new Center(
  77.                   child: new Text("B"),
  78.                 ),
  79.               );
  80.             },
  81.           ),
  82.         ),
  83.         new Positioned(
  84.           left: 230.0,
  85.           bottom: 20.0,
  86.           child: new DragTarget(
  87.             onAccept: (Color color) {
  88.               caughtcolor = color;
  89.             },
  90.             builder: (
  91.               BuildContext context,
  92.               List<dynamic> accepted,
  93.               List<dynamic> rejected,
  94.             ) {
  95.               return new Container(
  96.                 width: 60.0,
  97.                 height: 60.0,
  98.                 decoration: new BoxDecoration(
  99.                   color: accepted.isEmpty ? caughtcolor : Colors.grey.shade200,
  100.                 ),
  101.                 child: new Center(
  102.                   child: new Text("C"),
  103.                 ),
  104.               );
  105.             },
  106.           ),
  107.         ),
  108.         new Positioned(
  109.           left: 330.0,
  110.           bottom: 20.0,
  111.           child: new DragTarget(
  112.             onAccept: (Color color) {
  113.               caughtcolor = color;
  114.             },
  115.             builder: (
  116.               BuildContext context,
  117.               List<dynamic> accepted,
  118.               List<dynamic> rejected,
  119.             ) {
  120.               return new Container(
  121.                 width: 60.0,
  122.                 height: 60.0,
  123.                 decoration: new BoxDecoration(
  124.                   color: accepted.isEmpty ? caughtcolor : Colors.grey.shade200,
  125.                 ),
  126.                 child: new Center(
  127.                   child: new Text("D"),
  128.                 ),
  129.               );
  130.             },
  131.           ),
  132.         ),
  133.       ],
  134.     );
  135.   }
  136. }
  137.  
  138. class DragBox extends StatefulWidget {
  139.   final Offset initPos;
  140.   final String label;
  141.   final Color itemcolor;
  142.  
  143.   DragBox(this.initPos, this.label, this.itemcolor);
  144.   @override
  145.   _DragBoxState createState() => new _DragBoxState();
  146. }
  147.  
  148. class _DragBoxState extends State<DragBox> {
  149.   Offset position = new Offset(0.0, 0.0);
  150.   @override
  151.   void initState() {
  152.     // TODO: implement initState
  153.     position = widget.initPos;
  154.     super.initState();
  155.   }
  156.  
  157.   @override
  158.   Widget build(BuildContext context) {
  159.     return new Positioned(
  160.         left: position.dx,
  161.         top: position.dy,
  162.         child: new Draggable(
  163.           data: widget.itemcolor,
  164.           child: new Container(
  165.             width: 60.0,
  166.             height: 60.0,
  167.             color: widget.itemcolor,
  168.             child: new Center(
  169.               child: new Text(
  170.                 widget.label,
  171.                 style: new TextStyle(
  172.                   color: Colors.white,
  173.                   decoration: TextDecoration.none,
  174.                   fontSize: 20.0,
  175.                 ),
  176.               ),
  177.             ),
  178.           ),
  179.           onDraggableCanceled: (velocity, offset) {
  180.             //setState(() {
  181.             //  position = offset;  To make it so that the drag boxes do not stay when dropped.
  182.             //});
  183.           },
  184.           feedback: new Container(
  185.             width: 60.0,
  186.             height: 60.0,
  187.             color: widget.itemcolor.withOpacity(0.5),
  188.             child: new Center(
  189.               child: new Text(
  190.                 widget.label,
  191.                 style: new TextStyle(
  192.                   color: Colors.white,
  193.                   decoration: TextDecoration.none,
  194.                   fontSize: 18.0,
  195.                 ),
  196.               ),
  197.             ),
  198.           ),
  199.         ));
  200.   }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement