Compare commits
No commits in common. "c38305d38c19d61dc413f21f621a7cc968f7b0c7" and "62019663605cc6a52a32c58432358bd831a3f8f5" have entirely different histories.
c38305d38c
...
6201966360
3 changed files with 72 additions and 242 deletions
|
@ -1,38 +0,0 @@
|
|||
{
|
||||
"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,69 +3,46 @@ import 'package:ju_learn/main.dart';
|
|||
|
||||
class QuizPage extends StatefulWidget {
|
||||
Vault v;
|
||||
Function saveState;
|
||||
QuizPage(this.v, this.saveState, {super.key});
|
||||
QuizPage(this.v, {super.key});
|
||||
|
||||
@override
|
||||
// ignore: no_logic_in_create_state, library_private_types_in_public_api
|
||||
_QuizPageState createState() => _QuizPageState(v, saveState);
|
||||
_QuizPageState createState() {
|
||||
v.randomize();
|
||||
return _QuizPageState(v);
|
||||
}
|
||||
}
|
||||
|
||||
class _QuizPageState extends State<QuizPage> {
|
||||
int run = 0;
|
||||
Question _currentQuestion = Question("Dummy", ["Dummy aws"], 0, "dummy");
|
||||
int askState = -1;
|
||||
int _currentQuestionIndex = 0;
|
||||
int ask_state = -1;
|
||||
int _score = 0;
|
||||
Vault v;
|
||||
Function saveState;
|
||||
_QuizPageState(this.v, this.saveState) {
|
||||
_currentQuestion.drawer = 0x0fffffff;
|
||||
v.softReset();
|
||||
_nextQuestion();
|
||||
}
|
||||
_QuizPageState(this.v);
|
||||
|
||||
void _checkAnswer(int selectedIndex) {
|
||||
if (selectedIndex == _currentQuestion.correct) {
|
||||
_currentQuestion.drawer++;
|
||||
} else {
|
||||
_currentQuestion.drawer = 0;
|
||||
}
|
||||
_currentQuestion.lastInRun = run;
|
||||
saveState();
|
||||
//if (selectedIndex == v.questions[_currentQuestionIndex].correct) {
|
||||
// setState(() {
|
||||
// _score++;
|
||||
// });
|
||||
//}
|
||||
setState(() {
|
||||
askState = selectedIndex;
|
||||
ask_state = 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;
|
||||
});
|
||||
if (_currentQuestionIndex < v.questions.length - 1) {
|
||||
ask_state = -1;
|
||||
_currentQuestionIndex++;
|
||||
} else {
|
||||
_currentQuestion = lowestQ;
|
||||
_showResultDialog();
|
||||
}
|
||||
run++;
|
||||
});
|
||||
}
|
||||
|
||||
/*void _showResultDialog() {
|
||||
void _showResultDialog() {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
|
@ -87,7 +64,7 @@ class _QuizPageState extends State<QuizPage> {
|
|||
);
|
||||
},
|
||||
);
|
||||
}*/
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
@ -101,38 +78,40 @@ class _QuizPageState extends State<QuizPage> {
|
|||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text(
|
||||
_currentQuestion.quest,
|
||||
v.questions[_currentQuestionIndex].quest,
|
||||
style: const TextStyle(
|
||||
fontSize: 20.0,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20.0),
|
||||
..._currentQuestion.answers.map(
|
||||
...v.questions[_currentQuestionIndex].answers.map(
|
||||
(option) {
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(vertical: 8.0),
|
||||
child: FilledButton(
|
||||
onPressed: () => askState == -1
|
||||
onPressed: () => ask_state == -1
|
||||
? _checkAnswer(
|
||||
_currentQuestion.answers.indexOf(option),
|
||||
v.questions[_currentQuestionIndex].answers
|
||||
.indexOf(option),
|
||||
)
|
||||
: _nextQuestion(),
|
||||
style: FilledButton.styleFrom(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
backgroundColor: askState == -1
|
||||
backgroundColor: ask_state == -1
|
||||
? null
|
||||
: (_currentQuestion.correct ==
|
||||
_currentQuestion.answers.indexOf(option)
|
||||
: (v.questions[_currentQuestionIndex].correct ==
|
||||
v.questions[_currentQuestionIndex].answers
|
||||
.indexOf(option)
|
||||
? Colors.green
|
||||
: Colors.red),
|
||||
),
|
||||
child: Text(option)));
|
||||
},
|
||||
),
|
||||
if (askState != -1)
|
||||
if (ask_state != -1)
|
||||
Text(
|
||||
_currentQuestion.explanation,
|
||||
v.questions[_currentQuestionIndex].explanation,
|
||||
style: const TextStyle(
|
||||
fontSize: 15.0,
|
||||
fontWeight: FontWeight.bold,
|
||||
|
|
133
lib/main.dart
133
lib/main.dart
|
@ -28,37 +28,21 @@ class MainPage extends StatefulWidget {
|
|||
const MainPage({super.key});
|
||||
|
||||
@override
|
||||
MainPageState createState() => MainPageState();
|
||||
_MainPageState createState() => _MainPageState();
|
||||
}
|
||||
|
||||
class MainPageState extends State<MainPage> {
|
||||
class _MainPageState extends State<MainPage> {
|
||||
List<Vault> _vaults = [];
|
||||
MainPageState() {
|
||||
_MainPageState() {
|
||||
loadList();
|
||||
}
|
||||
|
||||
Vault? getByName(String name) {
|
||||
for (var v in _vaults) {
|
||||
if (v.name == name) return v;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
loadList() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
List<String>? json = prefs.getStringList("vaults");
|
||||
List<String>? jsonState = prefs.getStringList("vaultStates");
|
||||
if (json == null) return;
|
||||
setState(() {
|
||||
_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 {
|
||||
|
@ -67,73 +51,13 @@ class MainPageState extends State<MainPage> {
|
|||
"vaults", _vaults.map((e) => jsonEncode(e.toJson())).toList());
|
||||
}
|
||||
|
||||
saveStateList() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
prefs.setStringList("vaultStates",
|
||||
_vaults.map((e) => jsonEncode(e.toStateJson())).toList());
|
||||
}
|
||||
|
||||
_vaultMenu(Vault v) async {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (context) => BottomSheet(
|
||||
builder: (context) => Column(children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: Text(v.name,
|
||||
style: const TextStyle(
|
||||
fontSize: 20.0,
|
||||
fontWeight: FontWeight.bold,
|
||||
))),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.restore),
|
||||
title: const Text("Reset Vault"),
|
||||
subtitle: const Text("Reset Vault learn Data to default."),
|
||||
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 {
|
||||
askToDeleteVault(Vault v) async {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: const Text('Delete Vault'),
|
||||
content: Text(
|
||||
'Do you want to delete „${v.name}“ Vault?'),
|
||||
content: Text('Do you want to delete „${v.name}“ Vault?'),
|
||||
actions: [
|
||||
TextButton(
|
||||
child: const Text('Delete'),
|
||||
|
@ -142,8 +66,6 @@ class MainPageState extends State<MainPage> {
|
|||
_vaults.remove(v);
|
||||
});
|
||||
saveList();
|
||||
saveStateList();
|
||||
Navigator.of(context).pop();
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
),
|
||||
|
@ -156,10 +78,6 @@ class MainPageState extends State<MainPage> {
|
|||
);
|
||||
},
|
||||
);
|
||||
})
|
||||
]),
|
||||
onClosing: () {},
|
||||
));
|
||||
}
|
||||
|
||||
_pickFile() async {
|
||||
|
@ -177,8 +95,9 @@ class MainPageState extends State<MainPage> {
|
|||
});
|
||||
saveList();
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print(e);
|
||||
|
||||
}
|
||||
AlertDialog(
|
||||
title: const Text('Error'),
|
||||
content: const Text('An error has occurred.'),
|
||||
|
@ -195,8 +114,6 @@ class MainPageState extends State<MainPage> {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
|
@ -213,11 +130,11 @@ class MainPageState extends State<MainPage> {
|
|||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => QuizPage(e, saveStateList)),
|
||||
MaterialPageRoute(builder: (context) => QuizPage(e)),
|
||||
);
|
||||
},
|
||||
onLongPress: () {
|
||||
_vaultMenu(e);
|
||||
askToDeleteVault(e);
|
||||
},
|
||||
child: Text(e.name),
|
||||
)))
|
||||
|
@ -236,8 +153,6 @@ class Question {
|
|||
List<String> answers;
|
||||
int correct;
|
||||
String explanation;
|
||||
int drawer = 0;
|
||||
int lastInRun = 0;
|
||||
|
||||
Question(this.quest, this.answers, this.correct, this.explanation);
|
||||
|
||||
|
@ -286,21 +201,8 @@ class Vault {
|
|||
};
|
||||
}
|
||||
|
||||
Map<String, dynamic> toStateJson() {
|
||||
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;
|
||||
}
|
||||
void randomize() {
|
||||
questions.shuffle(Random());
|
||||
}
|
||||
|
||||
factory Vault.fromJson(Map<String, dynamic> json) {
|
||||
|
@ -317,17 +219,4 @@ class Vault {
|
|||
.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