Feat: Fügt das Erstellen von Konten hinzu

This commit is contained in:
2025-12-23 01:01:21 +01:00
parent 6bde42c815
commit 533feb2668
3 changed files with 115 additions and 21 deletions

View File

@@ -0,0 +1,74 @@
import 'dart:async';
import 'package:flutter/material.dart';
import '../Entities/account.dart';
import '../Entities/dialog_type_enum.dart';
import '../Pages/Dialog/dialog_action.dart';
import '../Pages/Dialog/dialog_input_field.dart';
import '../Pages/Dialog/dynamic_dialog.dart';
import '../Repositories/account_repository.dart';
/// Steuert die Interaktion mit den Accounts
class AccountController {
/// Erstellt eine neue Instanz dieser Klasse
AccountController() {
_newAccountDialog = DynamicDialog(
title: 'Neues Konto erstellen',
icon: Icons.account_balance_wallet,
inputFields: [
const DialogInputField(id: 'name', label: 'Name', autoFocus: true),
],
actions: [
DialogAction(label: 'Abbruch'),
DialogAction(
label: 'Speichern',
isPrimary: true,
onPressed: _saveNewAccount,
),
],
);
_errorNameEmptyDialog = DynamicDialog(
title: 'Fehler!',
icon: Icons.error,
content: const Text('Der Name des Kontos darf nicht leer sein!'),
dialogType: DialogTypeEnum.error,
);
_accountCreatedDialog = DynamicDialog(
title: 'Account erstellt!',
icon: Icons.check_circle,
content: const Text('Das Konto wurde erfolgreich erstellt!'),
dialogType: DialogTypeEnum.success,
);
}
BuildContext? _buildContext;
final AccountRepository _accountRepository = AccountRepository();
DynamicDialog? _newAccountDialog;
DynamicDialog? _errorNameEmptyDialog;
DynamicDialog? _accountCreatedDialog;
/// Startet den Prozess um ein neues Konto anzulegen
void newAccountHandler(final BuildContext buildContext) {
_buildContext = buildContext;
unawaited(_newAccountDialog?.show(buildContext));
}
Future<void> _saveNewAccount(final Map<String, String> values) async {
if (values['name'] == null || values['name']!.isEmpty) {
if (_buildContext != null) {
await _errorNameEmptyDialog?.show(_buildContext!);
}
} else {
final account = Account()..name = values['name']!;
await _accountRepository.add(account);
await _accountCreatedDialog?.show(_buildContext!);
}
}
}

View File

@@ -10,4 +10,7 @@ class Account {
/// Der Name des Kontos /// Der Name des Kontos
String name = ''; String name = '';
@override
String toString() => '${super.toString()}($id: $name)';
} }

View File

@@ -3,6 +3,8 @@ import 'package:flutter/material.dart';
import 'package:flutter_expandable_fab/flutter_expandable_fab.dart'; import 'package:flutter_expandable_fab/flutter_expandable_fab.dart';
import 'package:routemaster/routemaster.dart'; import 'package:routemaster/routemaster.dart';
import '../Controller/account_controller.dart';
/// Eine Seite, die als Container für die verschiedenen Tabs der App dient. /// Eine Seite, die als Container für die verschiedenen Tabs der App dient.
/// ///
/// Diese Seite enthält eine App-Bar mit einer Kontoauswahl sowie ein /// Diese Seite enthält eine App-Bar mit einer Kontoauswahl sowie ein
@@ -17,6 +19,8 @@ class HomePage extends StatefulWidget {
} }
class _HomePageState extends State<HomePage> { class _HomePageState extends State<HomePage> {
final AccountController _accountController = AccountController();
String selected = 'Konto 1'; String selected = 'Konto 1';
@override @override
@@ -89,29 +93,42 @@ class _HomePageState extends State<HomePage> {
type: ExpandableFabType.up, type: ExpandableFabType.up,
childrenAnimation: ExpandableFabAnimation.none, childrenAnimation: ExpandableFabAnimation.none,
distance: 70, distance: 70,
children: const <Widget>[ children: <Widget>[
Row( _expandableButton(
children: <Widget>[ label: 'Neue Transaktion',
Text('Neue Transaktion'), icon: Icons.add,
SizedBox(width: 20), onPressed: () {},
FloatingActionButton.small(
heroTag: null,
onPressed: null,
child: Icon(Icons.add),
),
],
), ),
Row( _expandableButton(
children: <Widget>[ label: 'Neues Konto',
Text('Neues Konto'), icon: Icons.account_balance_wallet,
SizedBox(width: 20), onPressed: () {
FloatingActionButton.small( _accountController.newAccountHandler(context);
heroTag: null, },
onPressed: null,
child: Icon(Icons.account_balance_wallet),
),
],
), ),
], ],
); );
Widget _expandableButton({
required final String label,
required final IconData icon,
required final VoidCallback onPressed,
}) => GestureDetector(
onTap: onPressed,
child: Row(
children: <Widget>[
Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
decoration: BoxDecoration(borderRadius: BorderRadius.circular(8)),
child: Text(label),
),
const SizedBox(width: 12),
FloatingActionButton.small(
heroTag: null,
onPressed: onPressed,
child: Icon(icon),
),
],
),
);
} }