Compare commits
2 commits
6201966360
...
c38305d38c
Author | SHA1 | Date | |
---|---|---|---|
c38305d38c | |||
9441e21f89 |
3 changed files with 242 additions and 72 deletions
38
js/example.json
Normal file
38
js/example.json
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
{
|
||||||
|
"name": "example",
|
||||||
|
"questions": [
|
||||||
|
{
|
||||||
|
"quest": "Q1",
|
||||||
|
"answers": [
|
||||||
|
"A",
|
||||||
|
"B",
|
||||||
|
"C",
|
||||||
|
"D"
|
||||||
|
],
|
||||||
|
"correct": 0,
|
||||||
|
"explanation": "not close"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"quest": "Q2",
|
||||||
|
"answers": [
|
||||||
|
"A",
|
||||||
|
"B",
|
||||||
|
"C",
|
||||||
|
"D"
|
||||||
|
],
|
||||||
|
"correct": 0,
|
||||||
|
"explanation": "not close"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"quest": "Q3",
|
||||||
|
"answers": [
|
||||||
|
"A",
|
||||||
|
"B",
|
||||||
|
"C",
|
||||||
|
"D"
|
||||||
|
],
|
||||||
|
"correct": 0,
|
||||||
|
"explanation": "not close"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -3,46 +3,69 @@ import 'package:ju_learn/main.dart';
|
||||||
|
|
||||||
class QuizPage extends StatefulWidget {
|
class QuizPage extends StatefulWidget {
|
||||||
Vault v;
|
Vault v;
|
||||||
QuizPage(this.v, {super.key});
|
Function saveState;
|
||||||
|
QuizPage(this.v, this.saveState, {super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
// ignore: no_logic_in_create_state, library_private_types_in_public_api
|
// ignore: no_logic_in_create_state, library_private_types_in_public_api
|
||||||
_QuizPageState createState() {
|
_QuizPageState createState() => _QuizPageState(v, saveState);
|
||||||
v.randomize();
|
|
||||||
return _QuizPageState(v);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class _QuizPageState extends State<QuizPage> {
|
class _QuizPageState extends State<QuizPage> {
|
||||||
int _currentQuestionIndex = 0;
|
int run = 0;
|
||||||
int ask_state = -1;
|
Question _currentQuestion = Question("Dummy", ["Dummy aws"], 0, "dummy");
|
||||||
int _score = 0;
|
int askState = -1;
|
||||||
Vault v;
|
Vault v;
|
||||||
_QuizPageState(this.v);
|
Function saveState;
|
||||||
|
_QuizPageState(this.v, this.saveState) {
|
||||||
|
_currentQuestion.drawer = 0x0fffffff;
|
||||||
|
v.softReset();
|
||||||
|
_nextQuestion();
|
||||||
|
}
|
||||||
|
|
||||||
void _checkAnswer(int selectedIndex) {
|
void _checkAnswer(int selectedIndex) {
|
||||||
//if (selectedIndex == v.questions[_currentQuestionIndex].correct) {
|
if (selectedIndex == _currentQuestion.correct) {
|
||||||
// setState(() {
|
_currentQuestion.drawer++;
|
||||||
// _score++;
|
} else {
|
||||||
// });
|
_currentQuestion.drawer = 0;
|
||||||
//}
|
}
|
||||||
|
_currentQuestion.lastInRun = run;
|
||||||
|
saveState();
|
||||||
setState(() {
|
setState(() {
|
||||||
ask_state = selectedIndex;
|
askState = selectedIndex;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void _nextQuestion() {
|
void _nextQuestion() {
|
||||||
setState(() {
|
Question lowestQ = _currentQuestion;
|
||||||
if (_currentQuestionIndex < v.questions.length - 1) {
|
for (var a in v.questions) {
|
||||||
ask_state = -1;
|
if (a == _currentQuestion) continue;
|
||||||
_currentQuestionIndex++;
|
int i = a.drawer - lowestQ.drawer;
|
||||||
} else {
|
if (i > 1) {
|
||||||
_showResultDialog();
|
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() {
|
/*void _showResultDialog() {
|
||||||
showDialog(
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (BuildContext context) {
|
builder: (BuildContext context) {
|
||||||
|
@ -64,7 +87,7 @@ class _QuizPageState extends State<QuizPage> {
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
@ -78,40 +101,38 @@ class _QuizPageState extends State<QuizPage> {
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
v.questions[_currentQuestionIndex].quest,
|
_currentQuestion.quest,
|
||||||
style: const TextStyle(
|
style: const TextStyle(
|
||||||
fontSize: 20.0,
|
fontSize: 20.0,
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 20.0),
|
const SizedBox(height: 20.0),
|
||||||
...v.questions[_currentQuestionIndex].answers.map(
|
..._currentQuestion.answers.map(
|
||||||
(option) {
|
(option) {
|
||||||
return Container(
|
return Container(
|
||||||
margin: const EdgeInsets.symmetric(vertical: 8.0),
|
margin: const EdgeInsets.symmetric(vertical: 8.0),
|
||||||
child: FilledButton(
|
child: FilledButton(
|
||||||
onPressed: () => ask_state == -1
|
onPressed: () => askState == -1
|
||||||
? _checkAnswer(
|
? _checkAnswer(
|
||||||
v.questions[_currentQuestionIndex].answers
|
_currentQuestion.answers.indexOf(option),
|
||||||
.indexOf(option),
|
|
||||||
)
|
)
|
||||||
: _nextQuestion(),
|
: _nextQuestion(),
|
||||||
style: FilledButton.styleFrom(
|
style: FilledButton.styleFrom(
|
||||||
padding: const EdgeInsets.all(16.0),
|
padding: const EdgeInsets.all(16.0),
|
||||||
backgroundColor: ask_state == -1
|
backgroundColor: askState == -1
|
||||||
? null
|
? null
|
||||||
: (v.questions[_currentQuestionIndex].correct ==
|
: (_currentQuestion.correct ==
|
||||||
v.questions[_currentQuestionIndex].answers
|
_currentQuestion.answers.indexOf(option)
|
||||||
.indexOf(option)
|
|
||||||
? Colors.green
|
? Colors.green
|
||||||
: Colors.red),
|
: Colors.red),
|
||||||
),
|
),
|
||||||
child: Text(option)));
|
child: Text(option)));
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
if (ask_state != -1)
|
if (askState != -1)
|
||||||
Text(
|
Text(
|
||||||
v.questions[_currentQuestionIndex].explanation,
|
_currentQuestion.explanation,
|
||||||
style: const TextStyle(
|
style: const TextStyle(
|
||||||
fontSize: 15.0,
|
fontSize: 15.0,
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
|
|
185
lib/main.dart
185
lib/main.dart
|
@ -28,21 +28,37 @@ class MainPage extends StatefulWidget {
|
||||||
const MainPage({super.key});
|
const MainPage({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_MainPageState createState() => _MainPageState();
|
MainPageState createState() => MainPageState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _MainPageState extends State<MainPage> {
|
class MainPageState extends State<MainPage> {
|
||||||
List<Vault> _vaults = [];
|
List<Vault> _vaults = [];
|
||||||
_MainPageState() {
|
MainPageState() {
|
||||||
loadList();
|
loadList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vault? getByName(String name) {
|
||||||
|
for (var v in _vaults) {
|
||||||
|
if (v.name == name) return v;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
loadList() async {
|
loadList() async {
|
||||||
final prefs = await SharedPreferences.getInstance();
|
final prefs = await SharedPreferences.getInstance();
|
||||||
List<String>? json = prefs.getStringList("vaults");
|
List<String>? json = prefs.getStringList("vaults");
|
||||||
|
List<String>? jsonState = prefs.getStringList("vaultStates");
|
||||||
if (json == null) return;
|
if (json == null) return;
|
||||||
setState(() {
|
setState(() {
|
||||||
_vaults = json.map((e) => Vault.fromJson(jsonDecode(e))).toList();
|
_vaults = json.map((e) => Vault.fromJson(jsonDecode(e))).toList();
|
||||||
});
|
});
|
||||||
|
if (jsonState == null) return;
|
||||||
|
for (var s in jsonState) {
|
||||||
|
var sMap = jsonDecode(s);
|
||||||
|
Vault? v = getByName(sMap["name"]);
|
||||||
|
if (v == null) continue;
|
||||||
|
v.loadState(sMap);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
saveList() async {
|
saveList() async {
|
||||||
|
@ -51,33 +67,99 @@ class _MainPageState extends State<MainPage> {
|
||||||
"vaults", _vaults.map((e) => jsonEncode(e.toJson())).toList());
|
"vaults", _vaults.map((e) => jsonEncode(e.toJson())).toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
askToDeleteVault(Vault v) async {
|
saveStateList() async {
|
||||||
showDialog(
|
final prefs = await SharedPreferences.getInstance();
|
||||||
context: context,
|
prefs.setStringList("vaultStates",
|
||||||
builder: (BuildContext context) {
|
_vaults.map((e) => jsonEncode(e.toStateJson())).toList());
|
||||||
return AlertDialog(
|
}
|
||||||
title: const Text('Delete Vault'),
|
|
||||||
content: Text('Do you want to delete „${v.name}“ Vault?'),
|
_vaultMenu(Vault v) async {
|
||||||
actions: [
|
showModalBottomSheet(
|
||||||
TextButton(
|
context: context,
|
||||||
child: const Text('Delete'),
|
builder: (context) => BottomSheet(
|
||||||
onPressed: () {
|
builder: (context) => Column(children: [
|
||||||
setState(() {
|
Padding(
|
||||||
_vaults.remove(v);
|
padding: const EdgeInsets.all(8),
|
||||||
});
|
child: Text(v.name,
|
||||||
saveList();
|
style: const TextStyle(
|
||||||
Navigator.of(context).pop();
|
fontSize: 20.0,
|
||||||
},
|
fontWeight: FontWeight.bold,
|
||||||
),
|
))),
|
||||||
TextButton(
|
ListTile(
|
||||||
onPressed: () {
|
leading: const Icon(Icons.restore),
|
||||||
Navigator.of(context).pop();
|
title: const Text("Reset Vault"),
|
||||||
},
|
subtitle: const Text("Reset Vault learn Data to default."),
|
||||||
child: const Text('Cancle'))
|
trailing: const Icon(Icons.chevron_right),
|
||||||
],
|
onTap: () {
|
||||||
);
|
showDialog(
|
||||||
},
|
context: context,
|
||||||
);
|
builder: (BuildContext context) {
|
||||||
|
return AlertDialog(
|
||||||
|
title: const Text('Reset Vault'),
|
||||||
|
content:
|
||||||
|
Text('Do you want to reset „${v.name}“ Vault?'),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
child: const Text('Reset'),
|
||||||
|
onPressed: () {
|
||||||
|
setState(() {
|
||||||
|
v.reset();
|
||||||
|
});
|
||||||
|
saveStateList();
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
TextButton(
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
},
|
||||||
|
child: const Text('Cancle'))
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
ListTile(
|
||||||
|
leading: const Icon(Icons.delete),
|
||||||
|
title: const Text("Vault löschen"),
|
||||||
|
subtitle: Text('Do you want to delete „${v.name}“ Vault?'),
|
||||||
|
trailing: const Icon(Icons.chevron_right),
|
||||||
|
onTap: () async {
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (BuildContext context) {
|
||||||
|
return AlertDialog(
|
||||||
|
title: const Text('Delete Vault'),
|
||||||
|
content: Text(
|
||||||
|
'Do you want to delete „${v.name}“ Vault?'),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
child: const Text('Delete'),
|
||||||
|
onPressed: () {
|
||||||
|
setState(() {
|
||||||
|
_vaults.remove(v);
|
||||||
|
});
|
||||||
|
saveList();
|
||||||
|
saveStateList();
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
TextButton(
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
},
|
||||||
|
child: const Text('Cancle'))
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
})
|
||||||
|
]),
|
||||||
|
onClosing: () {},
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
_pickFile() async {
|
_pickFile() async {
|
||||||
|
@ -95,9 +177,8 @@ class _MainPageState extends State<MainPage> {
|
||||||
});
|
});
|
||||||
saveList();
|
saveList();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (kDebugMode) {
|
print(e);
|
||||||
print(e);
|
|
||||||
}
|
|
||||||
AlertDialog(
|
AlertDialog(
|
||||||
title: const Text('Error'),
|
title: const Text('Error'),
|
||||||
content: const Text('An error has occurred.'),
|
content: const Text('An error has occurred.'),
|
||||||
|
@ -114,6 +195,8 @@ class _MainPageState extends State<MainPage> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
|
@ -130,11 +213,11 @@ class _MainPageState extends State<MainPage> {
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
MaterialPageRoute(builder: (context) => QuizPage(e)),
|
MaterialPageRoute(builder: (context) => QuizPage(e, saveStateList)),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
onLongPress: () {
|
onLongPress: () {
|
||||||
askToDeleteVault(e);
|
_vaultMenu(e);
|
||||||
},
|
},
|
||||||
child: Text(e.name),
|
child: Text(e.name),
|
||||||
)))
|
)))
|
||||||
|
@ -153,6 +236,8 @@ class Question {
|
||||||
List<String> answers;
|
List<String> answers;
|
||||||
int correct;
|
int correct;
|
||||||
String explanation;
|
String explanation;
|
||||||
|
int drawer = 0;
|
||||||
|
int lastInRun = 0;
|
||||||
|
|
||||||
Question(this.quest, this.answers, this.correct, this.explanation);
|
Question(this.quest, this.answers, this.correct, this.explanation);
|
||||||
|
|
||||||
|
@ -201,8 +286,21 @@ class Vault {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void randomize() {
|
Map<String, dynamic> toStateJson() {
|
||||||
questions.shuffle(Random());
|
return {"name": name, "drawers": questions.map((d) => d.drawer).toList()};
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset() {
|
||||||
|
for (var q in questions) {
|
||||||
|
q.drawer = 0;
|
||||||
|
q.lastInRun = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void softReset() {
|
||||||
|
for (var q in questions) {
|
||||||
|
q.lastInRun = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
factory Vault.fromJson(Map<String, dynamic> json) {
|
factory Vault.fromJson(Map<String, dynamic> json) {
|
||||||
|
@ -219,4 +317,17 @@ class Vault {
|
||||||
.toList(),
|
.toList(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
loadState(Map<String, dynamic> json) {
|
||||||
|
if (json["name"] != name) throw ErrorDescription("Wrong Vault Error");
|
||||||
|
if (json["drawers"] is! List<dynamic>) {
|
||||||
|
throw ErrorDescription("fromJSON Error: questions");
|
||||||
|
}
|
||||||
|
var stateList = json["drawers"];
|
||||||
|
for (var i = 0; i < stateList.length; i++) {
|
||||||
|
var q = questions.elementAtOrNull(i);
|
||||||
|
if (q != null) {
|
||||||
|
q.drawer = stateList[i] as int;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue