0

I have some draggable pictures and 3 different drag targets. With each picture I have passed a position argument which stores whether the picture should be in first dragtarget or second or third. How can i access that position whenever a picture is droped on the target what is the position. is that the correct target. Thankyou.

// ignore_for_file: prefer_const_constructors, file_names, non_constant_identifier_names
import 'dart:async';
import 'package:dyslexia/pages/level2/round3/Q2spell.dart';
import 'package:dyslexia/utilities/nextButton.dart';
import 'package:flutter/material.dart';
import 'package:flutter_tts/flutter_tts.dart';
import '../../../utilities/QuestionWidget.dart';

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

  @override
  State<Q1MTP> createState() => _Q1MTPState();
}

class _Q1MTPState extends State<Q1MTP> {
  String question = "Match the Picture with correct sentence?";
  var changeButton = false;
  var score = 0;
  final FlutterTts flutterTts = FlutterTts();

  // ignore: recursive_getters

  speak(word) async {
    await flutterTts.setLanguage("en-US");
    await flutterTts.setPitch(1);
    await flutterTts.setVolume(10.0);
    await flutterTts.speak(word);
  }

  var letter = "A";

  bool isPlayingMsg = false;
  bool draged1 = false;
  bool draged2 = false;
  bool draged3 = false;

  bool color = false;
  bool correct = false;
  var sentence1 = "The cat is on the mat";
  var sentence2 = "The car is driving on the road";
  var sentence3 = "The cat is running after the mouse";

  double timer = 20.0;
  bool canceltimer = false;
  String showtimer = "30";

  int pos1 = 1;
  int pos2 = 2;
  int pos3 = 3;

  void starttimer() async {
    const onesec = Duration(seconds: 1);
    Timer.periodic(onesec, (Timer t) {
      if (mounted) {
        setState(() {
          if (timer < 1) {
            t.cancel();
            //Navigator.of(context).pushReplacement(
            //  MaterialPageRoute(builder: (context) => Q11sound()));
          } else if (canceltimer == true) {
            t.cancel();
          } else {
            timer = timer - 1;
          }
          showtimer = timer.toString();
        });
      }
    });
  }

  @override
  void initState() {
    starttimer();
    super.initState();
  }

  // double line = ((timer.toDouble() - 1.0) / (20.0 - 1.0));

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: true,
        backgroundColor: Colors.cyan,
        title: Text("AD&DY"),
      ),
      body: Padding(
        padding: const EdgeInsets.all(14.0),
        child: SingleChildScrollView(
          child: Column(children: [
            /*
            LinearPercentIndicator(
              width: MediaQuery.of(context).size.width - 60,
              animation: true,
              lineHeight: 20.0,
              animationDuration: 2000,
              percent: ((timer.toDouble() - 1.0) / (20.0 - 1.0)),
              center: Text(timer.toString()),
              trailing: Icon(Icons.timer),
              linearStrokeCap: LinearStrokeCap.roundAll,
              progressColor: Colors.greenAccent,
            ),
            */
            SizedBox(height: MediaQuery.of(context).size.height * 0.02),
            QuestionWidget(question: question),
            SizedBox(height: MediaQuery.of(context).size.height * 0.05),
            Divider(
              thickness: 1.0,
            ),
            SizedBox(height: MediaQuery.of(context).size.height * 0.05),
            Column(
              children: [
                Container(
                  decoration: BoxDecoration(
                      gradient: LinearGradient(
                    begin: Alignment.topRight,
                    end: Alignment.bottomLeft,
                    colors: const [
                      Colors.cyan,
                      Colors.white,
                    ],
                  )),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      draggablePic("assets/images/$sentence3.png", pos3),
                      draggablePic("assets/images/$sentence1.jpg", pos1),
                      draggablePic("assets/images/$sentence2.jpg", pos2),
                    ],
                  ),
                ),
                SizedBox(height: MediaQuery.of(context).size.height * 0.05),
                Wrap(
                  spacing: 10,
                  runSpacing: 15,
                  children: [
                    Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          option(sentence1),
                          DragTarget<Image>(
                            builder: (
                              BuildContext context,
                              List<dynamic> accepted,
                              List<dynamic> rejected,
                            ) {
                              return Material(
                                borderRadius: BorderRadius.circular(80),
                                child: Container(
                                    height: 50,
                                    width: 50,
                                    color: Color.fromARGB(255, 255, 255, 255),
                                    child: Container(
                                      decoration: BoxDecoration(
                                          gradient: LinearGradient(
                                        begin: Alignment.topRight,
                                        end: Alignment.bottomLeft,
                                        colors: const [
                                          Colors.white,
                                          Colors.cyan,
                                        ],
                                      )),
                                    )),
                              );
                            },
                            onAccept: (dragW) {
                              setState(() {
                                draged1 = true;
                              });
                            },
                          ),
                        ]),
                    Divider(
                      thickness: 3,
                      indent: 50,
                      endIndent: 50,
                    ),
                    Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          option(sentence2),
                          DragTarget<Image>(
                            builder: (
                              BuildContext context,
                              List<dynamic> accepted,
                              List<dynamic> rejected,
                            ) {
                              return Material(
                                borderRadius: BorderRadius.circular(80),
                                child: Container(
                                    height: 50,
                                    width: 50,
                                    color: Color.fromARGB(255, 255, 255, 255),
                                    child: Container(
                                      decoration: BoxDecoration(
                                          gradient: LinearGradient(
                                        begin: Alignment.topRight,
                                        end: Alignment.bottomLeft,
                                        colors: const [
                                          Colors.white,
                                          Colors.cyan,
                                        ],
                                      )),
                                    )),
                              );
                            },
                            onAccept: (dragW) {
                              setState(() {});
                            },
                          ),
                        ]),
                    Divider(
                      thickness: 3,
                      indent: 50,
                      endIndent: 50,
                    ),
                    Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          option(sentence3),
                          DragTarget<Image>(
                            builder: (
                              BuildContext context,
                              List<dynamic> accepted,
                              List<dynamic> rejected,
                            ) {
                              return Material(
                                borderRadius: BorderRadius.circular(80),
                                child: Container(
                                    height: 50,
                                    width: 50,
                                    color: Color.fromARGB(255, 255, 255, 255),
                                    child: Container(
                                      decoration: BoxDecoration(
                                          gradient: LinearGradient(
                                        begin: Alignment.topRight,
                                        end: Alignment.bottomLeft,
                                        colors: const [
                                          Colors.white,
                                          Colors.cyan,
                                        ],
                                      )),
                                    )),
                              );
                            },
                            onAccept: (dragW) {
                              setState(() {});
                            },
                          ),
                        ]),
                    Divider(
                      thickness: 3,
                      indent: 50,
                      endIndent: 50,
                    ),
                  ],
                ),
              ],
            ),
            SizedBox(height: MediaQuery.of(context).size.height * 0.1),
            nextButton(changeButton: changeButton, Location: Q2spell()),
          ]),
        ),
      ),
    );
  }

  Widget option(sentence) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        Material(
          elevation: 3,
          child: Container(
            color: Color.fromARGB(255, 153, 209, 216),
            height: MediaQuery.of(context).size.height * 0.07,
            width: MediaQuery.of(context).size.width * 0.6,
            child: Center(child: Text(sentence)),
          ),
        )
      ],
    );
  }

  Widget draggablePic(pic, position) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[
        Draggable<Image>(
            // Data is the value this Draggable stores.
            //data: position,
            feedback: Image(
              image: AssetImage(pic),
              height: 90,
              width: 90,
            ),
            childWhenDragging: Material(
              borderRadius: BorderRadius.circular(10),
              child: Container(
                height: 50.0,
                width: 70.0,
                color: Color.fromARGB(255, 255, 255, 255),
              ),
            ),
            // onDragEnd: changecolor(),
            child: Image(
              image: AssetImage(pic),
              height: 80,
              width: 80,
            ))
      ],
    );
  }
}

0 Answers0