Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class _LevelOneState extends State<LevelOnePage> {
- late Timer time;
- int tick = 0;
- @override
- void initState() {
- super.initState();
- time = Timer.periodic(const Duration(seconds: 1), updateTick);
- }
- void updateTick(Timer timer) {
- setState(() {
- tick++;
- if (tick == 15) {
- time.cancel();
- Navigator.of(context)
- .push(MaterialPageRoute(builder: (BuildContext context) {
- return const StartPage();
- }));
- }
- });
- }
- List<String> numberPad = [
- '7',
- '8',
- '9',
- 'C',
- '4',
- '5',
- '6',
- 'DEL',
- '1',
- '2',
- '3',
- '=',
- '0',
- ];
- // Question
- int numberA = 1;
- int numberB = 1;
- // Answer
- String userAnswer = '';
- // On Button Tapped
- void buttonTapped(String button) {
- setState(
- () {
- if (button == '=') {
- //Check if user is correct
- // TODO: YOU NEED TO HAVE A SYMBOL THAT IS BEING USED SOMEPLACE, DOESN"T LOOK LIKE YOU HAVE ONE, JUST A METHOD TO GET ONE
- checkResult( '+', numberA, numberB, double.parse(userAnswer));
- } else if (button == 'C') {
- // CLears answer input
- userAnswer = '';
- } else if (button == 'DEL') {
- // Deletes the last user input
- if (userAnswer.isNotEmpty) {
- userAnswer = userAnswer.substring(0, userAnswer.length - 1);
- }
- // Caps at 3 Numbers
- } else if (userAnswer.length < 3) {
- userAnswer += button;
- }
- },
- );
- }
- bool isAnswerCorrect(String op, int numberA, int numberB, double answer) {
- switch (op) {
- case '+':
- return answer == numberA + numberB;
- case '-':
- return answer == numberA - numberB;
- case '/':
- return answer == numberA / numberB;
- case '*':
- return answer == numberA * numberB;
- default:
- return false;
- }
- }
- //check if user is right
- void checkResult(String op, int numberA, int numberB, double answer) {
- if(isAnswerCorrect(op, numberA, numberB, answer)) {
- // If correct
- showDialog(
- context: context,
- builder: (context) {
- return ResultMessage(
- message: 'Correct ',
- onTap: goToNextQuestion,
- icon: Icons.arrow_forward,
- );
- },
- );
- } else{
- // ignore: avoid_print
- showDialog(
- context: context,
- builder: (context) {
- return ResultMessage(
- message: 'Sorry Try Again',
- onTap: goBackToQuestion,
- icon: Icons.rotate_left);
- },
- );
- }
- }
- // Create Random Number
- var randomNumber = Random();
- // Create Random Symbol
- String createRandomSymbol() {
- var symbols = ['+', '-', '*', '/'];
- symbols.shuffle();
- return symbols.first;
- }
- // GoToNextQuestion
- void goToNextQuestion() {
- //Dimiss
- Navigator.of(context).pop();
- //Value Reset
- setState(() {
- userAnswer = '';
- });
- //New Question
- numberA = randomNumber.nextInt(12);
- numberB = randomNumber.nextInt(12);
- }
- //goBackToQuestion
- void goBackToQuestion() {
- Navigator.of(context).pop();
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- backgroundColor: Colors.lightGreenAccent,
- body: Column(
- children: <Widget>[
- Expanded(
- child: Container(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- children: <Widget>[
- Container(
- padding: const EdgeInsets.all(
- 25,
- ),
- alignment: Alignment.centerRight,
- child: Text(tick.toString(),
- textAlign: TextAlign.right, style: basicTextStyle),
- ),
- ]),
- ),
- ),
- Container(
- padding: const EdgeInsets.all(15),
- alignment: Alignment.bottomCenter,
- child:
- //Question
- Text(
- // ignore: prefer_interpolation_to_compose_strings
- numberA.toString() + ' + ' + numberB.toString() + ' = ',
- style: const TextStyle(fontSize: 32, color: Colors.purpleAccent),
- ),
- ),
- Container(
- height: 55,
- width: 110,
- decoration: BoxDecoration(
- color: Colors.deepPurpleAccent,
- borderRadius: BorderRadius.circular(4),
- ),
- child: Center(
- child: Text(userAnswer, style: basicTextStyle),
- ),
- ),
- Expanded(
- flex: 3,
- child: Container(
- child: GridView.builder(
- itemCount: numberPad.length,
- physics: const NeverScrollableScrollPhysics(),
- gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
- crossAxisCount: 4),
- itemBuilder: (BuildContext context, int index) {
- return MyButton(
- child: numberPad[index],
- onTap: () => buttonTapped(numberPad[index]),
- );
- },
- ),
- ),
- ),
- ],
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement