diff --git a/lib/presentation/views/main_menu/create_group_view.dart b/lib/presentation/views/main_menu/create_group_view.dart index 057a16f..590f342 100644 --- a/lib/presentation/views/main_menu/create_group_view.dart +++ b/lib/presentation/views/main_menu/create_group_view.dart @@ -37,9 +37,23 @@ class _CreateGroupViewState extends State { void initState() { super.initState(); db = Provider.of(context, listen: false); + _searchBarController.addListener(() { + setState(() {}); + }); + _groupNameController.addListener(() { + setState(() {}); + }); loadPlayerList(); } + @override + void dispose() { + _groupNameController.dispose(); + _searchBarController + .dispose(); // Listener entfernen und Controller aufräumen + super.dispose(); + } + void loadPlayerList() { _allPlayersFuture = db.playerDao.getAllPlayers(); _allPlayersFuture.then((loadedPlayers) { @@ -104,40 +118,16 @@ class _CreateGroupViewState extends State { ), hintText: 'Search for players', trailingButtonShown: true, - trailingButtonEnabled: - _searchBarController.text.isNotEmpty, + trailingButtonEnabled: _searchBarController.text + .trim() + .isNotEmpty, onTrailingButtonPressed: () async { - String playerName = _searchBarController.text.trim(); - if (playerName.isEmpty) return; - bool success = await db.playerDao.addPlayer( - player: Player(name: playerName), + addNewPlayerFromSearch( + context, + _searchBarController, + db, + loadPlayerList, ); - if (!context.mounted) return; - if (success) { - loadPlayerList(); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - backgroundColor: CustomTheme.boxColor, - content: Center( - child: Text( - 'Successfully added player $playerName.', - style: const TextStyle(color: Colors.white), - ), - ), - ), - ); - _searchBarController.clear(); - } else { - SnackBar( - backgroundColor: CustomTheme.boxColor, - content: Center( - child: Text( - 'Could not add player $playerName.', - style: const TextStyle(color: Colors.white), - ), - ), - ); - } }, onChanged: (value) { setState(() { @@ -347,3 +337,41 @@ class _CreateGroupViewState extends State { ); } } + +void addNewPlayerFromSearch( + context, + searchBarController, + db, + loadPlayerList, +) async { + String playerName = searchBarController.text.trim(); + bool success = await db.playerDao.addPlayer(player: Player(name: playerName)); + if (!context.mounted) return; + if (success) { + loadPlayerList(); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + backgroundColor: CustomTheme.boxColor, + content: Center( + child: Text( + 'Successfully added player $playerName.', + style: const TextStyle(color: Colors.white), + ), + ), + ), + ); + searchBarController.clear(); + } else { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + backgroundColor: CustomTheme.boxColor, + content: Center( + child: Text( + 'Could not add player $playerName.', + style: const TextStyle(color: Colors.white), + ), + ), + ), + ); + } +} diff --git a/lib/presentation/widgets/custom_search_bar.dart b/lib/presentation/widgets/custom_search_bar.dart index 8a483e5..4b5fd24 100644 --- a/lib/presentation/widgets/custom_search_bar.dart +++ b/lib/presentation/widgets/custom_search_bar.dart @@ -28,23 +28,24 @@ class CustomSearchBar extends StatelessWidget { constraints: constraints ?? const BoxConstraints(maxHeight: 45, minHeight: 45), hintText: hintText, - onChanged: onChanged, + onChanged: trailingButtonEnabled ? onChanged : null, hintStyle: WidgetStateProperty.all(const TextStyle(fontSize: 16)), leading: const Icon(Icons.search), - trailing: trailingButtonShown - ? [ - GestureDetector( - onTap: onTrailingButtonPressed, - child: Icon( - Icons.add_circle, - color: trailingButtonEnabled - ? null - : Colors.grey.withValues(alpha: 0.2), - ), - ), - const SizedBox(width: 5), - ] - : null, + trailing: [ + Visibility( + visible: trailingButtonShown, + child: GestureDetector( + onTap: onTrailingButtonPressed, + child: Icon( + Icons.add_circle, + color: trailingButtonEnabled + ? null + : Colors.grey.withValues(alpha: 0.2), + ), + ), + ), + const SizedBox(width: 5), + ], backgroundColor: WidgetStateProperty.all(CustomTheme.boxColor), side: WidgetStateProperty.all(BorderSide(color: CustomTheme.boxBorder)), shape: WidgetStateProperty.all(