Show menu

main.dart

  void main() => runApp(DemoApp());

  class DemoApp extends StatelessWidget {
    const DemoApp();

    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text("Drag and Drop"),
          ),
          body: const Center(
            child: DragWidget(),
          )
        ),
      );
    }
  }
  

game_score.dart

  class GameScore with ChangeNotifier {
    int _score = 0;
    int _turn = 0;
    int _currentValue = _random();

    int get score => _score;
    int get turn => _turn;
    int get currentValue => _currentValue;

    static int _random() => Random().nextInt(100) + 1;

    void addPoints(int pts) {
      _score += pts;
      _turn++;
      _currentValue = _random();

      notifyListeners();
    }

  }
  

drag_widget.dart

  class DragWidget extends StatelessWidget {
    const DragWidget();

    @override
    Widget build(BuildContext context) {
      return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(bottom: 30),
              child: Consumer<GameScore>(
                builder: (context, game, _) {
                  return Text("Total: ${game.score}",
                    style: const  TextStyle(
                      color: Colors.blueAccent,
                      fontSize: 18
                    ),
                  );
                },
              ),
            ),

            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: const <Widget>[
                EvenContainer(),
                NumberContainer(),
                OddContainer(),
              ],
            ),
          ],
        ),
      );
    }

  }
  

number_container.dart

  class NumberContainer extends StatelessWidget {
    const NumberContainer();

    @override
    Widget build(BuildContext context) {
      return Consumer<GameScore>(
        builder: (context, game, _) {
          return Draggable(
            data: game.currentValue,
            feedback: Container(
              width: 60,
              height: 60,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(60),
                color: Colors.black26,
              ),
              child: Center(
                child: Text("${game.currentValue}",
                  style: const TextStyle(
                      color: Colors.white,
                      decoration: TextDecoration.none,
                      fontSize: 15
                  ),
                ),
              ),
            ),
            child: Container(
              width: 60,
              height: 60,
              child: Center(
                child: Text("${game.currentValue}"),
              ),
            ),
          );
        },
      );
    }
  }
  

snack_message.dart

  mixin  SnackMessage {

    void showMessage(BuildContext context, String text) {
      Scaffold.of(context).showSnackBar(
        SnackBar(
          content: Text(text),
          duration: const Duration(milliseconds: 600),
        )
      );
    }

  }
  

odd_number.dart

  class OddContainer extends StatelessWidget with SnackMessage {
    const OddContainer();

    @override
    Widget build(BuildContext context) {

      return Container(
          width: 60,
          height: 60,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10),
            color: Colors.lightGreen,
          ),
          child: DragTarget<int>(
            onAccept: (data) => _onAccept(context, data),
            onWillAccept: _willAccept,
            builder: (context, _, __) {
              return const Rectangle(text: "Odd");
            },
          )
      );
    }

    void _onAccept(BuildContext context, int data) {
      context.read<GameScore>().addPoints(data);
      showMessage(context, "Points: + $data");
    }

    bool _willAccept(int? data) => data != null && data % 2 != 0;

  }
  

even_number.dart

  class EvenContainer extends StatelessWidget with SnackMessage {
    const EvenContainer();

    @override
    Widget build(BuildContext context) {

      return Container(
          width: 60,
          height: 60,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10),
            color: Colors.blueAccent,
          ),
          child: DragTarget<int>(
            onAccept: (data) => _onAccept(context, data),
            onWillAccept: _willAccept,
            builder: (context, _, __) {
              return const Rectangle(text: "Even");
            },
          )
      );
    }

    void _onAccept(BuildContext context, int data) {
      context.read<GameScore>().addPoints(data);
      showMessage(context, "Points: + $data");
    }

    bool _willAccept(int? data) => data != null && data % 2 == 0;

  }
  

number_box.dart

  class Rectangle extends StatelessWidget {
    final String text;
    const Rectangle({
      required this.text
    });

    @override
    Widget build(BuildContext context) {
      return Center(
        child: Text(text,
          style: TextStyle(
            color: Colors.white
          ),
        ),
      );
    }
  }
  

This website and the book are not official Google products. No affiliations are involved. Built with Java 14 and Vert.X

"Flutter and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC"