import 'package:flutter/material.dart'; class GradeAverage extends StatefulWidget { const GradeAverage({Key? key}) : super(key: key); @override State createState() => _GradeAverageState(); } class _GradeAverageState extends State { double average = 0; bool gradeSystem = true; List 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("Zurücksetzen?"), content: const Text("Alle Einträge werden entfernt."), 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( enableFeedback: true, initialValue: gradeSystem, icon: const Icon(Icons.more_horiz), itemBuilder: (context) => [true, false].map((e) => PopupMenuItem( 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), Text(gradeSystem ? "Wähle unten die Anzahl deiner jewiligen Noten aus" : "Wähle unten die Anzahl deiner jeweiligen Punkte aus"), 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 Material( child: ListTile( tileColor: grade.isEven ? Colors.transparent : Colors.transparent.withAlpha(50), title: Center( child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Text(getGradeDisplay(grade)), const SizedBox(width: 30), 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("${grades.where(isThis).length}", style: const TextStyle(fontSize: 15, fontWeight: FontWeight.bold)), IconButton( onPressed: () { setState(() { grades.add(grade); }); }, icon: const Icon(Icons.add), color: Theme.of(context).colorScheme.onSurface, ), ], ), ), trailing: Visibility( maintainState: true, maintainAnimation: true, maintainSize: true, visible: grades.any(isThis), child: IconButton( icon: const Icon(Icons.delete), onPressed: () { setState(() { grades.removeWhere(isThis); }); }, ), ), ), ); }, itemCount: gradeSystem ? 6 : 15, ), ), ], ), ); } }