Draggable Widgets

import 'package:flutter/material.dart';

class DragAndDropScreen extends StatefulWidget {
  const DragAndDropScreen({Key? key}) : super(key: key);

  @override
  State<DragAndDropScreen> createState() => _DragAndDropScreenState();
}

class _DragAndDropScreenState extends State<DragAndDropScreen> {
  Color c = Colors.black;
  @override
  Widget build(BuildContext context) {
    final showDraggable = c == Colors.black;
    Size size = MediaQuery.of(context).size;
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            DragTarget<Color>(
              onAccept: (data) => setState(() => c = data),
                builder: (context, _, __) =>
                Container(
                  color: c,
                  width: 200,
                  height: 200,
                )),
            IgnorePointer(
              ignoring: !showDraggable,
              child: Opacity(
                opacity: showDraggable ? 1: 0,
                child: Draggable<Color>(
                  data: Colors.blue,
                  child: Container(
                    color: Colors.green,
                    width: 200,
                    height: 200,
                  ),
                  feedback: Container(
                    color: Colors.orange,
                    width: 200,
                    height: 200,
                  ),
                  childWhenDragging: Container(
                    color: Colors.red,
                    width: 200,
                    height: 200,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}


Sign In or Register to comment.