160 lines
4.5 KiB
Dart
160 lines
4.5 KiB
Dart
import 'dart:math';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:ju_learn/main.dart';
|
|
|
|
// ignore: must_be_immutable
|
|
class QuizPage extends StatefulWidget {
|
|
Vault v;
|
|
Function saveState;
|
|
QuizPage(this.v, this.saveState, {super.key});
|
|
|
|
@override
|
|
// ignore: no_logic_in_create_state, library_private_types_in_public_api
|
|
_QuizPageState createState() => _QuizPageState(v, saveState);
|
|
}
|
|
|
|
class _QuizPageState extends State<QuizPage> {
|
|
int runSeed = Random().nextInt(0xffffff);
|
|
int run = 0;
|
|
Question _currentQuestion = Question("Dummy", ["Dummy aws"], 0, "dummy");
|
|
int askState = -1;
|
|
Vault v;
|
|
Function saveState;
|
|
_QuizPageState(this.v, this.saveState) {
|
|
_currentQuestion.drawer = 0x0fffffff;
|
|
v.softReset();
|
|
_nextQuestion();
|
|
}
|
|
|
|
void _checkAnswer(int selectedIndex) {
|
|
if (selectedIndex == _currentQuestion.correct) {
|
|
_currentQuestion.drawer++;
|
|
} else {
|
|
_currentQuestion.drawer = 0;
|
|
}
|
|
_currentQuestion.lastInRun = run;
|
|
saveState();
|
|
setState(() {
|
|
askState = selectedIndex;
|
|
});
|
|
}
|
|
|
|
void _nextQuestion() {
|
|
Question lowestQ = _currentQuestion;
|
|
for (var a in v.questions) {
|
|
if (a == _currentQuestion) continue;
|
|
int i = a.drawer - lowestQ.drawer;
|
|
if (i > 1) {
|
|
continue;
|
|
}
|
|
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++;
|
|
}
|
|
|
|
/*void _showResultDialog() {
|
|
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();
|
|
},
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}*/
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text('Learning: ${v.name}'),
|
|
),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Text(
|
|
_currentQuestion.quest,
|
|
style: const TextStyle(
|
|
fontSize: 20.0,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
const SizedBox(height: 20.0),
|
|
...() {
|
|
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;
|
|
}(),
|
|
if (askState != -1)
|
|
Text(
|
|
_currentQuestion.explanation,
|
|
style: const TextStyle(
|
|
fontSize: 15.0,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
floatingActionButton: askState != -1
|
|
? FloatingActionButton.extended(
|
|
onPressed: _nextQuestion,
|
|
icon: const Icon(Icons.chevron_right),
|
|
label: const Text("Next"))
|
|
: null,
|
|
);
|
|
}
|
|
}
|