import 'package:dropdown_search/dropdown_search.dart'; import 'package:flutter/material.dart'; import '../../Controller/account_controller.dart'; import '../../Entities/drift_database.dart'; /// Ein Dropdown, mit welchem man das Konto auswählen kann class AccountSelect extends StatefulWidget { /// Erstellt eine neue Instanz dieser Klasse const AccountSelect({super.key}); @override State createState() => _AccountSelectState(); } class _AccountSelectState extends State { final AccountController _accountController = AccountController(); Account? _selected; List _accounts = []; @override void initState() { super.initState(); _selected = _accountController.selected.value; _accounts = _accountController.accounts.value; _accountController.selected.addListener(() { setState(() { if (mounted) { _selected = _accountController.selected.value; } }); }); _accountController.accounts.addListener(() { setState(() { if (mounted) { _accounts = _accountController.accounts.value; } }); }); } @override Widget build(final BuildContext context) { debugPrint(_selected.toString()); debugPrint(_accounts.toString()); if (_selected != null && _accounts.isNotEmpty) { return DropdownSearch( items: (final f, final cs) => _accounts, selectedItem: _selected, onChanged: (final Account? account) { if (account != null) { _accountController.selected = account; } }, itemAsString: (final Account account) => account.name, compareFn: (final Account a1, final Account a2) => a1.id == a2.id, popupProps: const PopupProps.menu( showSearchBox: true, searchFieldProps: TextFieldProps( decoration: InputDecoration( hintText: 'Konto suchen...', contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 8), ), ), ), ); } else if (_selected == null && _accounts.isEmpty) { return const Text(''); } else { return const CircularProgressIndicator(); } } }