Reworked Settings

This commit is contained in:
Felix Kirchner
2025-05-03 15:50:08 +02:00
parent 639cc4638c
commit 9e44138851

View File

@@ -1,6 +1,7 @@
import 'package:cabo_counter/services/config_service.dart'; import 'package:cabo_counter/services/config_service.dart';
import 'package:cabo_counter/services/local_storage_service.dart'; import 'package:cabo_counter/services/local_storage_service.dart';
import 'package:cabo_counter/utility/custom_theme.dart'; import 'package:cabo_counter/utility/custom_theme.dart';
import 'package:cabo_counter/utility/globals.dart';
import 'package:cabo_counter/widgets/stepper.dart'; import 'package:cabo_counter/widgets/stepper.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:package_info_plus/package_info_plus.dart'; import 'package:package_info_plus/package_info_plus.dart';
@@ -14,13 +15,10 @@ class SettingsView extends StatefulWidget {
} }
class _SettingsViewState extends State<SettingsView> { class _SettingsViewState extends State<SettingsView> {
late final int _pointLimit; UniqueKey _stepperKey1 = UniqueKey();
late final int _caboPenalty; UniqueKey _stepperKey2 = UniqueKey();
bool _isLoading = true;
@override @override
void initState() { void initState() {
_loadSettings();
super.initState(); super.initState();
} }
@@ -31,9 +29,7 @@ class _SettingsViewState extends State<SettingsView> {
middle: Text('Einstellungen'), middle: Text('Einstellungen'),
), ),
child: SafeArea( child: SafeArea(
child: _isLoading child: Stack(
? const Center(child: CupertinoActivityIndicator())
: Stack(
children: [ children: [
Column( Column(
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
@@ -51,16 +47,17 @@ class _SettingsViewState extends State<SettingsView> {
child: CupertinoListTile( child: CupertinoListTile(
padding: EdgeInsets.zero, padding: EdgeInsets.zero,
title: const Text('Cabo-Strafe'), title: const Text('Cabo-Strafe'),
subtitle: subtitle: const Text('... für falsches Cabo sagen'),
const Text('... für falsches Cabo sagen'),
trailing: Stepper( trailing: Stepper(
initialValue: _caboPenalty, key: _stepperKey1,
initialValue: Globals.caboPenalty,
minValue: 0, minValue: 0,
maxValue: 50, maxValue: 50,
step: 1, step: 1,
onChanged: (value) { onChanged: (newCaboPenalty) {
setState(() { setState(() {
print('Neuer Wert: $value'); ConfigService.setCaboPenalty(newCaboPenalty);
Globals.caboPenalty = newCaboPenalty;
}); });
}, },
), ),
@@ -72,17 +69,33 @@ class _SettingsViewState extends State<SettingsView> {
title: const Text('Punkte-Limit'), title: const Text('Punkte-Limit'),
subtitle: const Text('... hier ist Schluss'), subtitle: const Text('... hier ist Schluss'),
trailing: Stepper( trailing: Stepper(
initialValue: _pointLimit, key: _stepperKey2,
initialValue: Globals.pointLimit,
minValue: 30, minValue: 30,
maxValue: 1000, maxValue: 1000,
step: 10, step: 10,
onChanged: (value) { onChanged: (newPointLimit) {
setState(() { setState(() {
print('Neuer Wert: $value'); ConfigService.setPointLimit(newPointLimit);
Globals.pointLimit = newPointLimit;
}); });
}, },
), ),
)), )),
Padding(
padding: const EdgeInsets.fromLTRB(0, 10, 0, 0),
child: Center(
heightFactor: 0.9,
child: CupertinoButton(
padding: EdgeInsets.zero,
onPressed: () => setState(() {
ConfigService.resetConfig();
_stepperKey1 = UniqueKey();
_stepperKey2 = UniqueKey();
}),
child: const Text('Standard zurücksetzten'),
),
)),
Padding( Padding(
padding: const EdgeInsets.fromLTRB(10, 10, 0, 0), padding: const EdgeInsets.fromLTRB(10, 10, 0, 0),
child: Text( child: Text(
@@ -91,31 +104,35 @@ class _SettingsViewState extends State<SettingsView> {
), ),
), ),
Padding( Padding(
padding: const EdgeInsets.fromLTRB(30, 10, 10, 0), padding: const EdgeInsets.only(top: 30),
child: Center(
heightFactor: 1,
child: Row( child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
CupertinoButton( CupertinoButton(
color: CustomTheme.primaryColor,
sizeStyle: CupertinoButtonSize.medium, sizeStyle: CupertinoButtonSize.medium,
child: Text( child: Text(
'Daten exportieren', 'Daten exportieren',
style: TextStyle(color: CustomTheme.white), style:
TextStyle(color: CustomTheme.backgroundColor),
), ),
onPressed: () async { onPressed: () async {
final success = await LocalStorageService print('Export pressed');
.exportJsonFile(); final success =
await LocalStorageService.exportJsonFile();
if (!success && context.mounted) { if (!success && context.mounted) {
showCupertinoDialog( showCupertinoDialog(
context: context, context: context,
builder: (context) => builder: (context) => CupertinoAlertDialog(
CupertinoAlertDialog(
title: const Text('Fehler'), title: const Text('Fehler'),
content: const Text( content: const Text(
'Datei konnte nicht exportiert werden.'), 'Datei konnte nicht exportiert werden.'),
actions: [ actions: [
CupertinoDialogAction( CupertinoDialogAction(
child: const Text('OK'), child: const Text('OK'),
onPressed: () => onPressed: () => Navigator.pop(context),
Navigator.pop(context),
), ),
], ],
), ),
@@ -123,21 +140,25 @@ class _SettingsViewState extends State<SettingsView> {
} }
}, },
), ),
const SizedBox(
width: 20,
),
CupertinoButton( CupertinoButton(
sizeStyle: CupertinoButtonSize.large, color: CustomTheme.primaryColor,
sizeStyle: CupertinoButtonSize.medium,
child: Text( child: Text(
'Daten importieren', 'Daten importieren',
style: style:
TextStyle(color: CustomTheme.white), TextStyle(color: CustomTheme.backgroundColor),
), ),
onPressed: () async { onPressed: () async {
final success = await LocalStorageService print('Import pressed');
.importJsonFile(); final success =
await LocalStorageService.importJsonFile();
if (!success && context.mounted) { if (!success && context.mounted) {
showCupertinoDialog( showCupertinoDialog(
context: context, context: context,
builder: (context) => builder: (context) => CupertinoAlertDialog(
CupertinoAlertDialog(
title: const Text('Fehler'), title: const Text('Fehler'),
content: const Text( content: const Text(
'Datei konnte nicht importiert werden.'), 'Datei konnte nicht importiert werden.'),
@@ -145,8 +166,7 @@ class _SettingsViewState extends State<SettingsView> {
CupertinoDialogAction( CupertinoDialogAction(
child: const Text('OK'), child: const Text('OK'),
onPressed: () => onPressed: () =>
Navigator.pop( Navigator.pop(context),
context),
), ),
], ],
)); ));
@@ -154,6 +174,7 @@ class _SettingsViewState extends State<SettingsView> {
}), }),
], ],
)), )),
)
], ],
), ),
Positioned( Positioned(
@@ -206,14 +227,4 @@ class _SettingsViewState extends State<SettingsView> {
Future<PackageInfo> _getPackageInfo() async { Future<PackageInfo> _getPackageInfo() async {
return await PackageInfo.fromPlatform(); return await PackageInfo.fromPlatform();
} }
Future<void> _loadSettings() async {
final pointLimit = await ConfigService.getPointLimit();
final caboPenalty = await ConfigService.getCaboPenalty();
setState(() {
_pointLimit = pointLimit;
_caboPenalty = caboPenalty;
_isLoading = false;
});
}
} }