Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

android - How to access one button at a time?

I resolved my current issue regarding locking and unlocking buttons. I am basing my levels on the marks the user got. If they got a score of 25 and above, they will proceed to the next level. Now the problem here is since my quiz is sharing one code with a different JSON file, when the user perfects the score on the first stage, the whole level will unlock which is a no-no. The idea is even they scored perfect, the next stage will be unlocked not the whole level. I tried thinking of ways on how to do it but nothing comes to my mind. Can you help me? please. I really need your help. Thank you.

Here is my Quiz Page:

import 'dart:async';
import 'dart:convert';
import 'dart:developer';

import 'package:baybay_app/Quiz/NextLevel class.dart';
import 'package:baybay_app/Quiz/QuizHome.dart';
import 'package:baybay_app/Quiz/ResultPage.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class Quizjson extends StatelessWidget {
  String roundName;
  bool isComplete = false;
  Quizjson(this.roundName, mark);
  String assettoload;
  int question;


  // a function
  // sets the asset to a particular JSON file
  // and opens the JSON
  setasset() {
    if (roundName == 'Easy Round') {
      assettoload ='assets/Sample.json';
    } else if (roundName == 'Average Round') {
       assettoload = 'assets/Medium.json';

    } else if (roundName == 'Hard Round') {
      assettoload = 'assets/Hard.json';
    }else {
      assettoload = 'assets/Intermediate.json';
    }




  }
  @override
  Widget build(BuildContext context) {

    setasset();
    return FutureBuilder(
        future: DefaultAssetBundle.of(context).loadString(assettoload, cache: false),
        builder: (context, snapshot){
          List mydata = json.decode(snapshot.data.toString());
          if(mydata == null){
            return Scaffold(
              body: Center(
                child: Text(
                  "Loading",
                ),
              ),
            );
          }else{
            return quizpage(mydata: mydata);
          }

        }
    );
  }
}


class quizpage extends StatefulWidget {
  String langname;
  var mydata;
  quizpage({Key key, @required this.mydata}): super(key: key);

  @override
  _quizpageState createState() => _quizpageState(mydata);
}

class _quizpageState extends State<quizpage> {
  var mydata;
  _quizpageState(this.mydata);
  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitDown, DeviceOrientation.portraitUp]);
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Expanded(
            flex: 3,
            child: Container(
            padding: EdgeInsets.all(20.0),
              alignment: Alignment.bottomLeft,
              child: Text(mydata[0][question.toString()])
            ),
          ),
          Expanded(
            flex: 6,
            child: Container(
            child: Column(
              children: [
                Row(
                  children:[
                    ChoiceButton("a"),
                    ChoiceButton("b")
                  ]
                ),
                Row(
                  children:  [
                    ChoiceButton("c"),
                    ChoiceButton("d"),
                  ]
                )
              ]
            ),
            ),
          ),
          Expanded(
            flex: 1,
            child: Container(
                  alignment: Alignment.topCenter,
            child: Center(
              child: Text(
               showtimer,
                style:  TextStyle(
                  fontSize: 20.0
                ),
              ),
            ),
            ),
          ),
        ],
      )
    );
  }






Widget ChoiceButton(String k) {
    return Padding(
      padding: EdgeInsets.symmetric(
          vertical: 10.0,
          horizontal: 10.0),
        child: MaterialButton(
          onPressed: ()=>CheckAnswer(k),
          child: Text(
          mydata[1][question.toString()][k],
          style: TextStyle(
              color: Colors.white
          )),
          color: btncolor[k],
        ),
    );

}
  Color colorsToShow = Colors.brown[700];
  Color right = Colors.greenAccent[700];
  Color wrong = Colors.redAccent[700];
  int mark = 0;
  int question = 1;
  int timer = 30;
  String showtimer = "30";
  bool canceltimer = false;
  bool isComplete = false;

  Map<String,Color> btncolor = {
    "a" : Colors.brown[700],
    "b" : Colors.brown[700],
    "c" : Colors.brown[700],
    "d" : Colors.brown[700],
   };

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

  @override
  void setState(fn){
    if(mounted){
      super.setState(fn);
    }
  }

  void starttimer() async {
    const onesec = Duration(seconds: 1);
    Timer.periodic(onesec, (Timer t){
      setState(() {
        if(timer < 1){
          t.cancel();
          NextQuestion();
        }
        else if(canceltimer==true){
            t.cancel();
        }
        else{
          timer = timer - 1;
        }
        showtimer = timer.toString();
      });
    });

  }

  void NextQuestion(){
    canceltimer = false;
    timer = 30;
    setState(() {
      if(question< 10){
        question++;
      }
      else{
        Navigator.of(context).pushReplacement(MaterialPageRoute(
          builder: (context) => ResultPage(mark: mark),

        ));}
      btncolor["a"] = Colors.brown[700];
      btncolor["b"] = Colors.brown[700];
      btncolor["c"] = Colors.brown[700];
      btncolor["d"] = Colors.brown[700];
      isComplete = true;
    });
    starttimer();
}

  void CheckAnswer(String k) {
    if(mydata[2][question.toString()] == mydata[1][question.toString()][k]){
      mark = mark+5;
      colorsToShow = right;
    }

    else{
      colorsToShow = wrong;
    }
    setState(() {
      btncolor[k] = colorsToShow;
    });

    Timer(Duration(seconds: 2), NextQuestion);
  }

}

Here is my Quiz Home:

import 'package:baybay_app/Quiz/QuizTracingSound.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:baybay_app/Quiz/QuizTracingSound.dart';

class QuizHome extends StatefulWidget {
  int mark = 0;
  QuizHome({ @required this.mark });
  @override
  _QuizHomeState createState() => _QuizHomeState(mark);
}

class _QuizHomeState extends State<QuizHome> {

createAlertDialouge(BuildContext context){
  return showDialog(context: context, builder: (context){
    return AlertDialog(
      title: Text('Not Complete'),
      content: Text('Please score 25 above mark'),
      actions: [
        Center(
          child: RaisedButton(
            onPressed: (){Navigator.of(context).pop();},
            color: Colors.grey[100],
          ),
        ),

      ],
      shape: RoundedRectangleBorder(side: BorderSide(width: 16.0, color: Colors.red.shade100)),
      backgroundColor: Colors.lightBlue.shade100,
    );
  });
}

createAlertDialougeOne(BuildContext context){
  return showDialog(context: context, builder: (context){
    return AlertDialog(
      title: Text('Not Complete'),
      content: Text('Please score 35 and above mark'),
      actions: [
        Center(
          child: RaisedButton(
            onPressed: (){Navigator.of(context).pop();},
            color: Colors.grey[100],
          ),
        ),

      ],
      shape: RoundedRectangleBorder(side: BorderSide(width: 16.0, color: Colors.red.shade100)),
      backgroundColor: Colors.lightBlue.shade100,
    );
  });
}

createAlertDialougeTwo(BuildContext context){
  return showDialog(context: context, builder: (context){
    return AlertDialog(
      title: Text('Not Complete'),
      content: Text('Please score 35 and above mark'),
      actions: [
        Center(
          child: RaisedButton(
            onPressed: (){Navigator.of(context).pop();},
            color: Colors.grey[100],
          ),
        ),

      ],
      shape: RoundedRectangleBorder(side: BorderSide(width: 16.0, color: Colors.red.shade100)),
      backgroundColor: Colors.lightBlue.shade100,
    );
  });
}

  int mark = 0 ;
  _QuizHomeState(this.mark);
  String langname;
  bool isComplete = false;
@override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitDown, DeviceOrientation.portraitUp]);
    List <String> images = [
      'assets/Ba.gif',
      'assets/GA.PNG',
      'assets/HA.PNG',
      'assets/SA.PNG'
    ];



    return Scaffold(
      appBar: AppBar(
        title: Text('Quiz Home '),
      ),
      body:ListView(
           children: <Widget>[
         FlatButton(
           child: CustomCard('Easy Round', images[0], des[1], isComplete, Colors.green, mark),

             onPressed: () {
               { Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context)=> Quizjson("Easy Round", mark = 5),
               ));}
             }

          ),
      SizedBox(
        height: 20.0,
      ),
      FlatButton(
          child: CustomCard('Average Round', images[1], des[1], isComplete, lockOrNot, mark),
         onPressed: _onPressedOne,
      ),
      SizedBox(
        height: 20.0,
      ),
      FlatButton(
        onPressed: _onPressedTwo,
          child: CustomCard('Hard Round', images[2],des[2], isComplete, btncolor, mark)
      ),
      SizedBox(
        height: 20.0,
      ),
      FlatButton(
        onPressed: _onPressedThree,
          child: CustomCard('Intermediate Round', images[3],des[3], isComplete, btncolor, mark )
      ),

           ],
      )

    );
  }
  List <String> des = [
    "The easy round lets you test your knowledge in determining letters. Are you up for the challenge? Click here!!!",
    "Do you want to step up your baybayin game? Let's see if you can distinguish words! Click here!!!",
    "Do you know how to construct sentences with the use of Baybayin? Click here!!!",
    "Masters of baybayin can only enter this quiz! Are you one of them? Click Here!!!",

  ];

Color btncolor;
 Widget CustomCard(String roundName,images,String des, bool isComplete, Color btncolor, int mark ) {




    return Material(
               color: btncolor,
              elevation: 10.0,
              borderRadius: BorderRadius.circular(20.0),
              child: Container(
                child: Column(
                 children: <Widget>[
                   Text(
                    '$mark'
                   ),
                   Padding(
                     padding: EdgeInsets.symmetric(
                       vertical: 10.0,
                     ),
                     child: Material(
                       elevation: 5.0,
                       borderRadius: BorderRadius.circular(100.0),
                       child: Container(
                         height: 200.0,
                         width: 200.0,
                         child:ClipOval(
                           child: Image(
                             image: AssetImage(images),
                           ),
                         )
                       ),
                     ),

                   ),
                   Center(
                     child: Text( roundName,

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...