diff --git a/frontend/lib/pages/quiz/attend_quiz_page.dart b/frontend/lib/pages/quiz/attend_quiz_page.dart index de8ecf6..74d1638 100644 --- a/frontend/lib/pages/quiz/attend_quiz_page.dart +++ b/frontend/lib/pages/quiz/attend_quiz_page.dart @@ -37,7 +37,7 @@ class _AttendQuizPageState extends AuthState { late String _formId; late String _userId; late String _alias; - late QuizForm _form; + QuizForm? _form; String _aliasError = ''; @@ -93,7 +93,7 @@ class _AttendQuizPageState extends AuthState { _loading = false; _fetchResult = 'success'; }); - await fetchForm(); + // await fetchForm(); } } on http.ClientException { setState(() { @@ -237,7 +237,7 @@ class _AttendQuizPageState extends AuthState { _socketChannel!.stream.listen((event) { var data = jsonDecode(event); if (data["formStatus"] == "FINISHED") { - _form.status = FormStatus.fromString(data["formStatus"]); + _form!.status = FormStatus.fromString(data["formStatus"]); _value = null; _voted = false; _userHasAnsweredCorrectly = false; @@ -246,11 +246,11 @@ class _AttendQuizPageState extends AuthState { if (data["action"] == "FORM_STATUS_CHANGED") { var form = QuizForm.fromJson(data["form"]); setState(() { - _form.status = FormStatus.fromString(data["formStatus"]); + _form!.status = FormStatus.fromString(data["formStatus"]); _value = null; _voted = false; - _form.currentQuestionIndex = form.currentQuestionIndex; - _form.currentQuestionFinished = form.currentQuestionFinished; + _form!.currentQuestionIndex = form.currentQuestionIndex; + _form!.currentQuestionFinished = form.currentQuestionFinished; _scoreboard = getScoreboard(data["form"]); }); } @@ -258,8 +258,8 @@ class _AttendQuizPageState extends AuthState { data["action"] == "OPENED_NEXT_QUESTION") { var form = QuizForm.fromJson(data["form"]); setState(() { - _form.currentQuestionIndex = form.currentQuestionIndex; - _form.currentQuestionFinished = form.currentQuestionFinished; + _form!.currentQuestionIndex = form.currentQuestionIndex; + _form!.currentQuestionFinished = form.currentQuestionFinished; }); if (data["action"] == "OPENED_NEXT_QUESTION") { setState(() { @@ -299,7 +299,7 @@ class _AttendQuizPageState extends AuthState { }, onError: (error) { //TODO: Should there be another error handling for this? setState(() { - _form.status = FormStatus.error; + _form!.status = FormStatus.error; }); }); } @@ -401,27 +401,9 @@ class _AttendQuizPageState extends AuthState { child: CircularProgressIndicator(), ), ); - } else if (_fetchResult == 'success') { - - final int totalQuestions = _form.questions.length; - final double progress = (_form.currentQuestionIndex + 1) / totalQuestions; - - final appBarWithProgress = AppBar( - title: Text(_form.name, - style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold)), - backgroundColor: Theme.of(context).colorScheme.primary, - bottom: PreferredSize( - preferredSize: const Size.fromHeight(10.0), - child: SizedBox( - height: 10.0, - child: LinearProgressIndicator( - value: progress, - backgroundColor: Colors.grey[300], - valueColor: AlwaysStoppedAnimation(Theme.of(context).colorScheme.secondary), - ), - ), - ), - ); + } + + if (_fetchResult == 'success') { final appBar = AppBar( title: const Text( @@ -437,6 +419,7 @@ class _AttendQuizPageState extends AuthState { body: ChooseAlias( onAliasSubmitted: (chosenAlias) async { setState(() { + _loading = true; _alias = chosenAlias; }); bool success = await participate(); @@ -452,6 +435,26 @@ class _AttendQuizPageState extends AuthState { ); } + final int totalQuestions = _form!.questions.length; + final double progress = (_form!.currentQuestionIndex + 1) / totalQuestions; + + final appBarWithProgress = AppBar( + title: Text(_form!.name, + style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold)), + backgroundColor: Theme.of(context).colorScheme.primary, + bottom: PreferredSize( + preferredSize: const Size.fromHeight(10.0), + child: SizedBox( + height: 10.0, + child: LinearProgressIndicator( + value: progress, + backgroundColor: Colors.grey[300], + valueColor: AlwaysStoppedAnimation(Theme.of(context).colorScheme.secondary), + ), + ), + ), + ); + if (_form?.status == FormStatus.finished) { return Scaffold( appBar: appBar, @@ -528,7 +531,7 @@ class _AttendQuizPageState extends AuthState { ); } - if (_form.status != FormStatus.started) { + if (_form!.status != FormStatus.started) { return Scaffold( appBar: appBar, body: Center( @@ -556,7 +559,7 @@ class _AttendQuizPageState extends AuthState { ); } - final element = _form.questions[_form.currentQuestionIndex]; + final element = _form!.questions[_form!.currentQuestionIndex]; return Scaffold( appBar: appBarWithProgress, @@ -569,7 +572,7 @@ class _AttendQuizPageState extends AuthState { child: Column( children: [ const SizedBox(height: 16), - Text(element!.name, + Text(element.name, style: const TextStyle( fontSize: 24, fontWeight: FontWeight.bold)), Text(element.description, @@ -612,7 +615,7 @@ class _AttendQuizPageState extends AuthState { ], ), ), - if (!_form.currentQuestionFinished && !_voted) + if (!_form!.currentQuestionFinished && !_voted) ElevatedButton( child: const Text('Senden'), onPressed: () { @@ -631,7 +634,7 @@ class _AttendQuizPageState extends AuthState { }); }, ), - if (!_form.currentQuestionFinished && _voted) + if (!_form!.currentQuestionFinished && _voted) Container( margin: const EdgeInsets.only( top: 0.0,