JuLearn/lib/learn.dart

160 lines
4.5 KiB
Dart
Raw Normal View History

2023-06-01 20:23:13 +02:00
import 'dart:math';
2023-06-01 16:11:03 +02:00
import 'package:flutter/material.dart';
import 'package:ju_learn/main.dart';
class QuizPage extends StatefulWidget {
Vault v;
2023-06-01 20:14:34 +02:00
Function saveState;
QuizPage(this.v, this.saveState, {super.key});
2023-06-01 16:11:03 +02:00
@override
2023-06-01 16:38:20 +02:00
// ignore: no_logic_in_create_state, library_private_types_in_public_api
2023-06-01 20:14:34 +02:00
_QuizPageState createState() => _QuizPageState(v, saveState);
2023-06-01 16:11:03 +02:00
}
class _QuizPageState extends State<QuizPage> {
2023-06-01 20:23:13 +02:00
int runSeed = Random().nextInt(0xffffff);
2023-06-01 20:14:34 +02:00
int run = 0;
Question _currentQuestion = Question("Dummy", ["Dummy aws"], 0, "dummy");
int askState = -1;
2023-06-01 16:11:03 +02:00
Vault v;
2023-06-01 20:14:34 +02:00
Function saveState;
_QuizPageState(this.v, this.saveState) {
_currentQuestion.drawer = 0x0fffffff;
v.softReset();
_nextQuestion();
}
2023-06-01 16:11:03 +02:00
void _checkAnswer(int selectedIndex) {
2023-06-01 20:14:34 +02:00
if (selectedIndex == _currentQuestion.correct) {
_currentQuestion.drawer++;
} else {
_currentQuestion.drawer = 0;
}
_currentQuestion.lastInRun = run;
saveState();
2023-06-01 16:11:03 +02:00
setState(() {
2023-06-01 20:14:34 +02:00
askState = selectedIndex;
2023-06-01 16:11:03 +02:00
});
}
void _nextQuestion() {
2023-06-01 20:14:34 +02:00
Question lowestQ = _currentQuestion;
for (var a in v.questions) {
if (a == _currentQuestion) continue;
int i = a.drawer - lowestQ.drawer;
if (i > 1) {
continue;
2023-06-01 16:11:03 +02:00
}
2023-06-01 20:14:34 +02:00
if (i < -1) {
lowestQ = a;
continue;
}
if ((run - a.lastInRun - 10).abs() -
(run - lowestQ.lastInRun - 10).abs() <
0) {
lowestQ = a;
}
}
if (mounted) {
setState(() {
_currentQuestion = lowestQ;
askState = -1;
});
} else {
_currentQuestion = lowestQ;
}
run++;
2023-06-01 16:11:03 +02:00
}
2023-06-01 20:14:34 +02:00
/*void _showResultDialog() {
2023-06-01 16:11:03 +02:00
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Quiz Completed'),
content: Text('Your score: $_score / ${v.questions.length}'),
actions: [
TextButton(
child: const Text('Restart'),
onPressed: () {
setState(() {
_currentQuestionIndex = 0;
_score = 0;
});
Navigator.of(context).pop();
},
),
],
);
},
);
2023-06-01 20:14:34 +02:00
}*/
2023-06-01 16:11:03 +02:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2023-06-01 16:45:42 +02:00
title: Text('Learning: ${v.name}'),
2023-06-01 16:11:03 +02:00
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
2023-06-01 20:14:34 +02:00
_currentQuestion.quest,
2023-06-01 16:11:03 +02:00
style: const TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 20.0),
2023-06-01 20:23:13 +02:00
...() {
var l = _currentQuestion.answers.map(
(option) {
return Container(
margin: const EdgeInsets.symmetric(vertical: 8.0),
child: FilledButton(
onPressed: () => askState == -1
? _checkAnswer(
_currentQuestion.answers.indexOf(option),
)
: _nextQuestion(),
style: FilledButton.styleFrom(
padding: const EdgeInsets.all(16.0),
backgroundColor: askState == -1
? null
: (_currentQuestion.correct ==
_currentQuestion.answers.indexOf(option)
? Colors.green
: Colors.red),
),
child: Text(option)));
},
).toList();
l.shuffle(Random(run + runSeed));
return l;
}(),
2023-06-01 20:14:34 +02:00
if (askState != -1)
2023-06-01 16:38:20 +02:00
Text(
2023-06-01 20:14:34 +02:00
_currentQuestion.explanation,
2023-06-01 16:38:20 +02:00
style: const TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
)
2023-06-01 16:11:03 +02:00
],
),
),
2023-06-02 13:04:50 +02:00
floatingActionButton: askState != -1
? FloatingActionButton.extended(
onPressed: _nextQuestion,
icon: const Icon(Icons.chevron_right),
label: const Text("Next"))
: null,
2023-06-01 16:11:03 +02:00
);
}
}