150 lines
5.0 KiB
Dart
150 lines
5.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
class GradeAverage extends StatefulWidget {
|
|
const GradeAverage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<GradeAverage> createState() => _GradeAverageState();
|
|
}
|
|
|
|
class _GradeAverageState extends State<GradeAverage> {
|
|
double average = 0;
|
|
bool gradeSystem = true;
|
|
List<int> grades = List.empty(growable: true);
|
|
|
|
String getGradeDisplay(int grade) {
|
|
if(gradeSystem) {
|
|
return "Note $grade";
|
|
} else {
|
|
return "$grade Punkt${grade > 1 ? "e" : ""}";
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if(grades.isNotEmpty) {
|
|
average = grades.reduce((a, b) => a + b) / grades.length;
|
|
} else {
|
|
average = 0;
|
|
}
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text("Notendurschnittsrechner"),
|
|
actions: [
|
|
IconButton(onPressed: () {
|
|
showDialog(context: context, builder: (context) {
|
|
return AlertDialog(
|
|
title: const Text("Noten Zurücksetzen?"),
|
|
actions: [
|
|
TextButton(onPressed: () {
|
|
Navigator.of(context).pop();
|
|
}, child: const Text("Abbrechen")),
|
|
TextButton(onPressed: () {
|
|
grades.clear();
|
|
setState(() {});
|
|
Navigator.of(context).pop();
|
|
}, child: const Text("Zurücksetzen"))
|
|
],
|
|
);
|
|
});
|
|
}, icon: const Icon(Icons.delete_forever)),
|
|
PopupMenuButton<bool>(
|
|
enableFeedback: true,
|
|
initialValue: gradeSystem,
|
|
icon: const Icon(Icons.read_more),
|
|
itemBuilder: (context) => [true, false].map((e) => PopupMenuItem<bool>(
|
|
value: e,
|
|
enabled: e != gradeSystem,
|
|
child: Row(
|
|
children: [
|
|
Text(e ? "Notensystem" : "Punktesystem"),
|
|
],
|
|
),
|
|
)).toList(),
|
|
|
|
onSelected: (e) {
|
|
void switchSystem() => setState(() {
|
|
grades.clear();
|
|
gradeSystem = e;
|
|
});
|
|
|
|
if(grades.isNotEmpty) {
|
|
showDialog(context: context, builder: (context) {
|
|
return AlertDialog(
|
|
title: const Text("Notensystem wechseln"),
|
|
content: const Text("Beim wechsel des Notensystems werden alle Einträge zurückgesetzt."),
|
|
actions: [
|
|
TextButton(onPressed: () {
|
|
Navigator.of(context).pop();
|
|
}, child: const Text("Abbrechen")),
|
|
TextButton(onPressed: () {
|
|
switchSystem();
|
|
Navigator.of(context).pop();
|
|
}, child: const Text("Fortfahren")),
|
|
],
|
|
);
|
|
});
|
|
} else {
|
|
switchSystem();
|
|
}
|
|
|
|
|
|
},
|
|
),
|
|
],
|
|
),
|
|
body: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const SizedBox(height: 30),
|
|
Text(average.toStringAsFixed(2), style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold)),
|
|
const SizedBox(height: 10),
|
|
const Divider(),
|
|
const SizedBox(height: 10),
|
|
Expanded(
|
|
child: ListView.builder(
|
|
itemBuilder: (context, index) {
|
|
var grade = gradeSystem ? index + 1 : 14 - index + 1;
|
|
bool isThis(int e) => e == grade;
|
|
return ListTile(
|
|
tileColor: grade.isEven ? Colors.transparent : Colors.transparent.withAlpha(50),
|
|
title: Center(
|
|
child: Row(
|
|
children: [
|
|
IconButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
if(!grades.any(isThis)) return;
|
|
grades.removeAt(grades.indexWhere(isThis));
|
|
});
|
|
},
|
|
icon: const Icon(Icons.remove),
|
|
color: Theme.of(context).colorScheme.onSurface,
|
|
),
|
|
Text(getGradeDisplay(grade)),
|
|
IconButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
grades.add(grade);
|
|
});
|
|
},
|
|
icon: const Icon(Icons.add),
|
|
color: Theme.of(context).colorScheme.onSurface,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
trailing: Text("Anzahl: ${grades.where(isThis).length}"),
|
|
);
|
|
},
|
|
itemCount: gradeSystem ? 6 : 15,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|