Skip to content

Commit

Permalink
added toast
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiGong-dev committed Jan 15, 2022
1 parent 614cce6 commit 39cfffa
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 22 deletions.
13 changes: 13 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
PODS:
- Flutter (1.0.0)
- fluttertoast (0.0.2):
- Flutter
- Toast
- soundpool (0.0.1):
- Flutter
- Toast (4.0.0)

DEPENDENCIES:
- Flutter (from `Flutter`)
- fluttertoast (from `.symlinks/plugins/fluttertoast/ios`)
- soundpool (from `.symlinks/plugins/soundpool/ios`)

SPEC REPOS:
trunk:
- Toast

EXTERNAL SOURCES:
Flutter:
:path: Flutter
fluttertoast:
:path: ".symlinks/plugins/fluttertoast/ios"
soundpool:
:path: ".symlinks/plugins/soundpool/ios"

SPEC CHECKSUMS:
Flutter: 50d75fe2f02b26cc09d224853bb45737f8b3214a
fluttertoast: 6122fa75143e992b1d3470f61000f591a798cc58
soundpool: c7f4422ca206e77f8900ed3c4ee6a6ff5a0e38a9
Toast: 91b396c56ee72a5790816f40d3a94dd357abc196

PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c

Expand Down
3 changes: 1 addition & 2 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objectVersion = 51;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -70,7 +70,6 @@
CA1593FE1133BFFC6334DBFD /* Pods-Runner.release.xcconfig */,
FE585232C959868BC7B559EB /* Pods-Runner.profile.xcconfig */,
);
name = Pods;
path = Pods;
sourceTree = "<group>";
};
Expand Down
70 changes: 50 additions & 20 deletions lib/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:convert';
import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand All @@ -9,10 +10,16 @@ import 'package:soundpool/soundpool.dart';
import 'package:http/http.dart' as http;
import 'package:html_unescape/html_unescape.dart';
import './question.dart';
import 'package:fluttertoast/fluttertoast.dart';

Future<List<Question>> fetchQuestions(http.Client client) async {
final randomNumber = Random();
// random question number from 5 to 10
int questionNumber = 5 + randomNumber.nextInt(6);
final response = await client.get(Uri.parse(
'https://opentdb.com/api.php?amount=5&category=9&type=boolean'));
'https://opentdb.com/api.php?amount=' +
questionNumber.toString() +
'&category=18&type=boolean'));

return compute(parseQuestions, response.body);
}
Expand Down Expand Up @@ -128,14 +135,39 @@ class MyHomePageState extends State<MyHomePage> {
}
}

void showCorrectToast() {
Fluttertoast.showToast(
msg: "Correct!",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.green,
textColor: Colors.white,
fontSize: 16.0);
}

void showWrongToast() {
Fluttertoast.showToast(
msg:
'Uh-oh, it\'s actually ${questions![currentQuestionIndex].correctAnswer} ',
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
}

Future<void> handleHeadMotionEvent() async {
if (currentMotionType == "tilt left") {
headMotionCount++;
_userAnswer = "True";
if (questions![currentQuestionIndex].correctAnswer == _userAnswer) {
userCorrectCount++;
showCorrectToast();
await pool.play(soundCorrect);
} else {
showWrongToast();
await pool.play(soundWrong);
}
setState(() {
Expand All @@ -151,8 +183,10 @@ class MyHomePageState extends State<MyHomePage> {
_userAnswer = "False";
if (questions![currentQuestionIndex].correctAnswer == _userAnswer) {
userCorrectCount++;
showCorrectToast();
await pool.play(soundCorrect);
} else {
showWrongToast();
await pool.play(soundWrong);
}
setState(() {
Expand Down Expand Up @@ -219,26 +253,20 @@ class MyHomePageState extends State<MyHomePage> {
softWrap: true,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w400,
fontWeight: FontWeight.bold,
color: Colors.purple,
),
),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text(
'''
Your Score is:
$userCorrectCount/${questions!.length}
''',
softWrap: true,
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.w400,
color: Colors.purple,
),
),
],
child: Text(
'Your Score is:'
'$userCorrectCount/${questions!.length}',
softWrap: true,
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.w400,
color: Colors.purple,
),
),
),
actions: <Widget>[
Expand All @@ -249,7 +277,7 @@ class MyHomePageState extends State<MyHomePage> {
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.purple,
color: Color(0xFF753188),
),
),
onPressed: () {
Expand Down Expand Up @@ -288,6 +316,7 @@ class MyHomePageState extends State<MyHomePage> {
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w400,
color: Color(0xFF753188),
// color: Colors.purple,
),
),
Expand Down Expand Up @@ -414,7 +443,7 @@ class MyHomePageState extends State<MyHomePage> {
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.w400,
color: Colors.purple,
color: Color(0xFF753188),
),
),
Text(
Expand All @@ -423,7 +452,7 @@ class MyHomePageState extends State<MyHomePage> {
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.w400,
color: Colors.purple,
color: Color(0xFF753188),
),
)
],
Expand Down Expand Up @@ -482,6 +511,7 @@ class MyHomePageState extends State<MyHomePage> {
width: 200,
height: 70,
child: ElevatedButton(
style: ElevatedButton.styleFrom(primary: const Color(0xFF753188)),
onPressed: () => _startGame(),
child: const Padding(
padding: EdgeInsets.all(15),
Expand Down
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
fluttertoast:
dependency: "direct main"
description:
name: fluttertoast
url: "https://pub.dartlang.org"
source: hosted
version: "8.0.8"
html_unescape:
dependency: "direct main"
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
fluttertoast: ^8.0.8


dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 39cfffa

Please sign in to comment.