2 Commits

Author SHA1 Message Date
bce4cdcb2d Enable player creation via search bar in CreateGroupView
Some checks failed
Pull Request Pipeline / test (pull_request) Successful in 2m0s
Pull Request Pipeline / lint (pull_request) Failing after 2m5s
2025-11-20 16:53:38 +01:00
fa0e9a5dfd add trailing button functionality to CustomSearchBar 2025-11-20 16:53:14 +01:00
2 changed files with 59 additions and 0 deletions

View File

@@ -37,6 +37,10 @@ class _CreateGroupViewState extends State<CreateGroupView> {
void initState() { void initState() {
super.initState(); super.initState();
db = Provider.of<AppDatabase>(context, listen: false); db = Provider.of<AppDatabase>(context, listen: false);
loadPlayerList();
}
void loadPlayerList() {
_allPlayersFuture = db.playerDao.getAllPlayers(); _allPlayersFuture = db.playerDao.getAllPlayers();
_allPlayersFuture.then((loadedPlayers) { _allPlayersFuture.then((loadedPlayers) {
setState(() { setState(() {
@@ -99,6 +103,41 @@ class _CreateGroupViewState extends State<CreateGroupView> {
minHeight: 45, minHeight: 45,
), ),
hintText: 'Search for players', hintText: 'Search for players',
trailingButtonShown: true,
trailingButtonEnabled:
_searchBarController.text.isNotEmpty,
onTrailingButtonPressed: () async {
String playerName = _searchBarController.text;
if (playerName.isEmpty) return;
bool success = await db.playerDao.addPlayer(
player: Player(name: playerName),
);
if (success) {
loadPlayerList();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: CustomTheme.boxColor,
content: Center(
child: Text(
'Successfully added player $playerName.',
style: TextStyle(color: Colors.white),
),
),
),
);
_searchBarController.clear();
} else {
SnackBar(
backgroundColor: CustomTheme.boxColor,
content: Center(
child: Text(
'Could not add player $playerName.',
style: TextStyle(color: Colors.white),
),
),
);
}
},
onChanged: (value) { onChanged: (value) {
setState(() { setState(() {
if (value.isEmpty) { if (value.isEmpty) {

View File

@@ -6,11 +6,17 @@ class CustomSearchBar extends StatelessWidget {
final String hintText; final String hintText;
final ValueChanged<String>? onChanged; final ValueChanged<String>? onChanged;
final BoxConstraints? constraints; final BoxConstraints? constraints;
final bool trailingButtonEnabled;
final bool trailingButtonShown;
final VoidCallback? onTrailingButtonPressed;
const CustomSearchBar({ const CustomSearchBar({
super.key, super.key,
required this.controller, required this.controller,
required this.hintText, required this.hintText,
this.trailingButtonShown = false,
this.trailingButtonEnabled = true,
this.onTrailingButtonPressed,
this.onChanged, this.onChanged,
this.constraints, this.constraints,
}); });
@@ -25,6 +31,20 @@ class CustomSearchBar extends StatelessWidget {
onChanged: onChanged, onChanged: onChanged,
hintStyle: WidgetStateProperty.all(const TextStyle(fontSize: 16)), hintStyle: WidgetStateProperty.all(const TextStyle(fontSize: 16)),
leading: const Icon(Icons.search), leading: const Icon(Icons.search),
trailing: trailingButtonShown
? [
GestureDetector(
onTap: onTrailingButtonPressed,
child: Icon(
Icons.add_circle,
color: trailingButtonEnabled
? null
: Colors.grey.withValues(alpha: 0.2),
),
),
SizedBox(width: 5),
]
: null,
backgroundColor: WidgetStateProperty.all(CustomTheme.boxColor), backgroundColor: WidgetStateProperty.all(CustomTheme.boxColor),
side: WidgetStateProperty.all(BorderSide(color: CustomTheme.boxBorder)), side: WidgetStateProperty.all(BorderSide(color: CustomTheme.boxBorder)),
shape: WidgetStateProperty.all( shape: WidgetStateProperty.all(