validity for space name

This commit is contained in:
hannathkadher
2024-11-29 19:03:27 +04:00
parent ae2b4b9b06
commit 42c7577b35
4 changed files with 68 additions and 30 deletions

View File

@ -18,6 +18,7 @@ class CreateCommunityDialog extends StatefulWidget {
class CreateCommunityDialogState extends State<CreateCommunityDialog> {
String enteredName = '';
bool isNameFieldExist = false;
bool isNameEmpty = false;
@override
Widget build(BuildContext context) {
@ -63,10 +64,15 @@ class CreateCommunityDialogState extends State<CreateCommunityDialog> {
TextField(
onChanged: (value) {
setState(() {
enteredName = value.trim(); // Trim whitespace
enteredName = value.trim();
isNameFieldExist = widget.communities.any(
(community) => community.name == enteredName,
);
if (value.isEmpty) {
isNameEmpty = true;
} else {
isNameEmpty = false;
}
});
},
style: const TextStyle(
@ -83,7 +89,7 @@ class CreateCommunityDialogState extends State<CreateCommunityDialog> {
),
border: OutlineInputBorder(
borderSide: BorderSide(
color: isNameFieldExist
color: isNameFieldExist || isNameEmpty
? ColorsManager.red
: ColorsManager.boxColor,
width: 1),
@ -91,16 +97,15 @@ class CreateCommunityDialogState extends State<CreateCommunityDialog> {
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: isNameFieldExist
color: isNameFieldExist || isNameEmpty
? ColorsManager.red
: ColorsManager.boxColor,
width: 1),
: ColorsManager.boxColor, width: 1),
borderRadius: BorderRadius.circular(10),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: BorderSide(
color: isNameFieldExist
color: isNameFieldExist || isNameEmpty
? ColorsManager.red
: ColorsManager.boxColor,
width: 1),
@ -118,17 +123,26 @@ class CreateCommunityDialogState extends State<CreateCommunityDialog> {
?.copyWith(color: ColorsManager.red),
),
),
if (isNameEmpty)
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Text(
'*Name should not be empty.',
style: Theme.of(context)
.textTheme
.bodySmall
?.copyWith(color: ColorsManager.red),
),
),
const SizedBox(height: 24),
// Action buttons
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Expanded(
child: CancelButton(
label: 'Cancel',
onPressed: () => Navigator.of(context).pop(),
),
child: CancelButton(
label: 'Cancel',
onPressed: () => Navigator.of(context).pop(),
),
),
const SizedBox(width: 16),
@ -143,7 +157,7 @@ class CreateCommunityDialogState extends State<CreateCommunityDialog> {
Navigator.of(context).pop();
}
},
backgroundColor: isNameFieldExist || enteredName.isEmpty
backgroundColor: isNameFieldExist || isNameEmpty
? ColorsManager.lightGrayColor
: ColorsManager.secondaryColor,
borderRadius: 10,