Skip to content

Commit 5855d2e

Browse files
committed
🎨 [Design] Perfect Loading spinners, buttons to navigate and also restart the current lecture
1 parent 7968574 commit 5855d2e

File tree

2 files changed

+76
-36
lines changed

2 files changed

+76
-36
lines changed

‎lib/ui/screen/home_page.dart‎

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flashcard_project/design_system.dart';
22
import 'package:flashcard_project/domain/flashcard_service.dart';
33
import 'package:flashcard_project/repository/sheet_repo.dart';
4+
import 'package:flashcard_project/ui/screen/lesson_selector_screen.dart';
45
import 'package:flutter/material.dart';
56

67
class HomePage extends StatefulWidget {
@@ -21,6 +22,7 @@ class HomePage extends StatefulWidget {
2122
}
2223

2324
class _HomePageState extends State<HomePage> {
25+
late Future<bool> init;
2426
late FlashcardService _flashcardService;
2527
late List<MapEntry<String, String>> questionAnswerList;
2628
late MapEntry<String, String> questionAnswerHeader;
@@ -32,10 +34,10 @@ class _HomePageState extends State<HomePage> {
3234
initState() {
3335
super.initState();
3436
_flashcardService = FlashcardService(SheetRepo(widget.spreadsheetId));
35-
startLesson();
37+
init = startLesson();
3638
}
3739

38-
Future<void> startLesson() async {
40+
Future<bool> startLesson() async {
3941
Map<String, String> _questionAnswerList =
4042
await _flashcardService.getQuestionsAndAnswers();
4143
questionAnswerList = _questionAnswerList.entries.toList();
@@ -47,6 +49,7 @@ class _HomePageState extends State<HomePage> {
4749
cardFlipped = false;
4850
allCardsFinished = false;
4951
});
52+
return true;
5053
}
5154

5255
@override
@@ -58,40 +61,57 @@ class _HomePageState extends State<HomePage> {
5861
body: Padding(
5962
padding: const EdgeInsets.all(Insets.small),
6063
child: Column(
64+
mainAxisAlignment: MainAxisAlignment.center,
6165
children: [
62-
Expanded(
63-
child: allCardsFinished
64-
? _buildLoadingSpinner()
65-
: IgnorePointer(
66-
ignoring: cardFlipped,
67-
child: InkWell(
68-
onTap: () => setState(() => cardFlipped = !cardFlipped),
69-
child: Card(
70-
child: Center(
71-
child: Column(
72-
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
73-
children: [
74-
Text(
75-
cardFlipped
76-
? questionAnswerHeader.value
77-
: questionAnswerHeader.key,
78-
),
79-
Expanded(
66+
FutureBuilder<bool>(
67+
future: init,
68+
builder: (context, AsyncSnapshot<bool> snapshot) {
69+
if (snapshot.hasData) {
70+
return Expanded(
71+
child: allCardsFinished
72+
? _buildLoadingSpinner()
73+
: IgnorePointer(
74+
ignoring: cardFlipped,
75+
child: InkWell(
76+
onTap: () =>
77+
setState(() => cardFlipped = !cardFlipped),
78+
child: Card(
8079
child: Center(
81-
child: Text(
82-
cardFlipped
83-
? currentQuestionAndAnswer.value
84-
: currentQuestionAndAnswer.key,
80+
child: Column(
81+
mainAxisAlignment:
82+
MainAxisAlignment.spaceEvenly,
83+
children: [
84+
Text(
85+
cardFlipped
86+
? questionAnswerHeader.value
87+
: questionAnswerHeader.key,
88+
),
89+
Expanded(
90+
child: Center(
91+
child: Text(
92+
cardFlipped
93+
? currentQuestionAndAnswer
94+
.value
95+
: currentQuestionAndAnswer
96+
.key,
97+
),
98+
),
99+
),
100+
],
85101
),
86102
),
87103
),
88-
],
104+
),
89105
),
90-
),
91-
),
92-
),
93-
),
94-
),
106+
);
107+
}
108+
if (snapshot.hasError) {
109+
return const Text(
110+
"Sorry something went wrong, please try it later again",
111+
);
112+
}
113+
return const Center(child: CircularProgressIndicator());
114+
}),
95115
cardFlipped
96116
? Row(
97117
children: [
@@ -104,7 +124,7 @@ class _HomePageState extends State<HomePage> {
104124
icon: const Icon(Icons.check_circle),
105125
),
106126
IconButton(
107-
onPressed: startLesson,
127+
onPressed: () => init = startLesson(),
108128
icon: const Icon(Icons.redo),
109129
),
110130
],
@@ -130,10 +150,22 @@ class _HomePageState extends State<HomePage> {
130150
Column _buildLoadingSpinner() {
131151
return Column(
132152
mainAxisAlignment: MainAxisAlignment.center,
133-
children: const [
134-
Text("Congratulations! You finished all your cards."),
135-
SizedBox(height: Insets.large),
136-
Text("Your stats are: 42 % correct answered"),
153+
children: [
154+
const Text("Congratulations! You finished all your cards."),
155+
const SizedBox(height: Insets.medium),
156+
ElevatedButton(
157+
onPressed: () {
158+
init = startLesson();
159+
},
160+
child: const Text("Repeat Lesson"),
161+
),
162+
const SizedBox(height: Insets.medium),
163+
ElevatedButton(
164+
onPressed: () {
165+
LessonSelectorScreen.navigateTo(context);
166+
},
167+
child: const Text("Back to Lecture Selection"),
168+
)
137169
],
138170
);
139171
}

‎lib/ui/screen/lesson_selector_screen.dart‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ class LessonSelectorScreen extends StatefulWidget {
99

1010
@override
1111
State<LessonSelectorScreen> createState() => _LessonSelectorScreenState();
12+
13+
static void navigateTo(BuildContext context) {
14+
Navigator.push(context, MaterialPageRoute(
15+
builder: (context) {
16+
return const LessonSelectorScreen();
17+
},
18+
));
19+
}
1220
}
1321

1422
class _LessonSelectorScreenState extends State<LessonSelectorScreen> {
@@ -65,7 +73,7 @@ class _LessonSelectorScreenState extends State<LessonSelectorScreen> {
6573
);
6674
}
6775
if (snapshot.hasError) return const Text("Something wrong happend");
68-
return const CircularProgressIndicator();
76+
return const Center(child: CircularProgressIndicator());
6977
}),
7078
);
7179
}

0 commit comments

Comments
 (0)