Giter VIP home page Giter VIP logo

Comments (1)

yash365 avatar yash365 commented on August 13, 2024

In pubspec.yaml add the folloowing dependency

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2
  image_picker: ^0.4.10
  shared_preferences: ^0.4.3

puzzle_piece.dart:
Code:

import 'package:shared_preferences/shared_preferences.dart';
...
_incrementCounter(BuildContext context) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    int counter = 0;
    setState(() {
      counter = prefs.getInt("counter");
    });
    
    await prefs.setInt("counter", (counter == null) ? 1 : counter + 1);

    if (prefs.getInt("counter") == 9) {
      _gotoNextPage(context);
    }
    else {
    }
  }

_gotoNextPage(BuildContext context) {
    Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) {
      return Next();
    }));
  }

@override
  Widget build(BuildContext context) {
    final imageWidth = MediaQuery.of(context).size.width;
    final imageHeight = MediaQuery.of(context).size.height *
        MediaQuery.of(context).size.width /
        widget.imageSize.width;
    final pieceWidth = imageWidth / widget.maxCol;
    final pieceHeight = imageHeight / widget.maxRow;

    if (top == null) {
      top = Random().nextInt((imageHeight - pieceHeight).ceil()).toDouble();
      top -= widget.row * pieceHeight;
    }
    if (left == null) {
      left = Random().nextInt((imageWidth - pieceWidth).ceil()).toDouble();
      left -= widget.col * pieceWidth;
    }

    return Positioned(
      top: top,
      left: left,
      width: imageWidth,
      child: GestureDetector(
        onTap: () {
          if (isMovable) {
            widget.bringToTop(widget);
            print("$_Tag inside GestureDetector 1");
          }
        },
        onPanStart: (_) {
          if (isMovable) {
            widget.bringToTop(widget);
            print("$_Tag inside GestureDetector 11");
          }
        },
        onPanUpdate: (dragUpdateDetails) {
          if (isMovable) {
            setState(() {
              top += dragUpdateDetails.delta.dy;
              left += dragUpdateDetails.delta.dx;

              if (-10 < top && top < 10 && -10 < left && left < 10) {
                top = 0;
                left = 0;
                isMovable = false;
                widget.sendToBack(widget);

                print("$_Tag inside GestureDetector 111");
              }
            });
          }
        },
        child: GestureDetector(
          onTap: () {
            if (isMovable) {
              widget.bringToTop(widget);
              print("$_Tag inside GestureDetector 2");
            }
          },
          onPanStart: (_) {
            if (isMovable) {
              widget.bringToTop(widget);
              print("$_Tag inside GestureDetector 22");
            }
          },
          onPanUpdate: (dragUpdateDetails) {
            if (isMovable) {
              setState(() {
                top += dragUpdateDetails.delta.dy;
                left += dragUpdateDetails.delta.dx;

                if (-10 < top && top < 10 && -10 < left && left < 10) {
                  top = 0;
                  left = 0;
                  isMovable = false;
                  widget.sendToBack(widget);
                  _incrementCounter(context);   // increment the counter here
                  print("$_Tag inside GestureDetector 222");
                }
              });
            }
          },
          child: ClipPath(
            child: widget.image,
            clipper: PuzzlePieceClipper(
                widget.row, widget.col, widget.maxRow, widget.maxCol),
          ),
        ),
      ),
    );
  }

from flutter_puzzle.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.