mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-15 09:45:25 +00:00
added validation to create space
This commit is contained in:
@ -36,6 +36,8 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
|
|||||||
String enteredName = '';
|
String enteredName = '';
|
||||||
List<SelectedProduct> selectedProducts = [];
|
List<SelectedProduct> selectedProducts = [];
|
||||||
late TextEditingController nameController;
|
late TextEditingController nameController;
|
||||||
|
bool isOkButtonEnabled = false;
|
||||||
|
bool isNameFieldInvalid = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@ -43,6 +45,7 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
|
|||||||
selectedIcon = widget.icon ?? Assets.location;
|
selectedIcon = widget.icon ?? Assets.location;
|
||||||
nameController = TextEditingController(text: widget.name ?? '');
|
nameController = TextEditingController(text: widget.name ?? '');
|
||||||
selectedProducts = widget.selectedProducts.isNotEmpty ? widget.selectedProducts : [];
|
selectedProducts = widget.selectedProducts.isNotEmpty ? widget.selectedProducts : [];
|
||||||
|
isOkButtonEnabled = enteredName.isNotEmpty || widget.name!.isNotEmpty;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -113,6 +116,14 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
|
|||||||
controller: nameController,
|
controller: nameController,
|
||||||
onChanged: (value) {
|
onChanged: (value) {
|
||||||
enteredName = value;
|
enteredName = value;
|
||||||
|
setState(() {
|
||||||
|
if (value.isNotEmpty) {
|
||||||
|
isOkButtonEnabled = true;
|
||||||
|
isNameFieldInvalid = false;
|
||||||
|
} else {
|
||||||
|
isOkButtonEnabled = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
style: const TextStyle(color: Colors.black),
|
style: const TextStyle(color: Colors.black),
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
@ -126,8 +137,9 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
|
|||||||
fillColor: ColorsManager.boxColor,
|
fillColor: ColorsManager.boxColor,
|
||||||
enabledBorder: OutlineInputBorder(
|
enabledBorder: OutlineInputBorder(
|
||||||
borderRadius: BorderRadius.circular(10),
|
borderRadius: BorderRadius.circular(10),
|
||||||
borderSide: const BorderSide(
|
borderSide: BorderSide(
|
||||||
color: ColorsManager.boxColor,
|
color:
|
||||||
|
isNameFieldInvalid ? ColorsManager.red : ColorsManager.boxColor,
|
||||||
width: 1.5,
|
width: 1.5,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -140,6 +152,17 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
if (isNameFieldInvalid)
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.only(top: 8.0),
|
||||||
|
child: Text(
|
||||||
|
'*Space name should not be empty.',
|
||||||
|
style: Theme.of(context)
|
||||||
|
.textTheme
|
||||||
|
.bodySmall
|
||||||
|
?.copyWith(color: ColorsManager.red),
|
||||||
|
),
|
||||||
|
),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
if (selectedProducts.isNotEmpty)
|
if (selectedProducts.isNotEmpty)
|
||||||
_buildSelectedProductsButtons(widget.products ?? [])
|
_buildSelectedProductsButtons(widget.products ?? [])
|
||||||
@ -210,13 +233,22 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
|
|||||||
Expanded(
|
Expanded(
|
||||||
child: DefaultButton(
|
child: DefaultButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
|
if (enteredName.trim().isEmpty || widget.name!.isEmpty) {
|
||||||
|
setState(() {
|
||||||
|
isNameFieldInvalid = true;
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
String newName = enteredName.isNotEmpty ? enteredName : (widget.name ?? '');
|
String newName = enteredName.isNotEmpty ? enteredName : (widget.name ?? '');
|
||||||
if (newName.isNotEmpty) {
|
if (newName.isNotEmpty) {
|
||||||
widget.onCreateSpace(newName, selectedIcon, selectedProducts);
|
widget.onCreateSpace(newName, selectedIcon, selectedProducts);
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
backgroundColor: ColorsManager.secondaryColor,
|
borderRadius: 10,
|
||||||
|
backgroundColor:
|
||||||
|
isOkButtonEnabled ? ColorsManager.secondaryColor : ColorsManager.grayColor,
|
||||||
foregroundColor: ColorsManager.whiteColors,
|
foregroundColor: ColorsManager.whiteColors,
|
||||||
child: const Text('OK'),
|
child: const Text('OK'),
|
||||||
),
|
),
|
||||||
|
Reference in New Issue
Block a user