diff --git a/lib/pages/spaces_management/widgets/community_structure_widget.dart b/lib/pages/spaces_management/widgets/community_structure_widget.dart index 7322caaa..90937f28 100644 --- a/lib/pages/spaces_management/widgets/community_structure_widget.dart +++ b/lib/pages/spaces_management/widgets/community_structure_widget.dart @@ -270,6 +270,7 @@ class _CommunityStructureAreaState extends State { builder: (BuildContext context) { return CreateSpaceDialog( products: widget.products, + parentSpace: parentIndex != null? spaces[parentIndex] : null, onCreateSpace: (String name, String icon, List selectedProducts) { setState(() { // Set the first space in the center or use passed position diff --git a/lib/pages/spaces_management/widgets/dialogs/create_space_dialog.dart b/lib/pages/spaces_management/widgets/dialogs/create_space_dialog.dart index 5953951c..eba5096c 100644 --- a/lib/pages/spaces_management/widgets/dialogs/create_space_dialog.dart +++ b/lib/pages/spaces_management/widgets/dialogs/create_space_dialog.dart @@ -4,6 +4,7 @@ import 'package:syncrow_web/pages/common/buttons/cancel_button.dart'; import 'package:syncrow_web/pages/common/buttons/default_button.dart'; import 'package:syncrow_web/pages/spaces_management/model/product_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/selected_product_model.dart'; +import 'package:syncrow_web/pages/spaces_management/model/space_model.dart'; import 'package:syncrow_web/pages/spaces_management/widgets/add_device_type_widget.dart'; import 'package:syncrow_web/pages/spaces_management/widgets/dialogs/icon_selection_dialog.dart'; import 'package:syncrow_web/pages/spaces_management/widgets/hoverable_button.dart'; @@ -18,9 +19,11 @@ class CreateSpaceDialog extends StatefulWidget { final String? icon; final bool isEdit; final List selectedProducts; + final SpaceModel? parentSpace; const CreateSpaceDialog( {super.key, + this.parentSpace, required this.onCreateSpace, this.products, this.name, @@ -39,6 +42,7 @@ class CreateSpaceDialogState extends State { late TextEditingController nameController; bool isOkButtonEnabled = false; bool isNameFieldInvalid = false; + bool isNameFieldExist = false; @override void initState() { @@ -47,7 +51,6 @@ class CreateSpaceDialogState extends State { nameController = TextEditingController(text: widget.name ?? ''); selectedProducts = widget.selectedProducts.isNotEmpty ? widget.selectedProducts : []; isOkButtonEnabled = enteredName.isNotEmpty || nameController.text.isNotEmpty; - isNameFieldInvalid = nameController.text.isEmpty; } @override @@ -116,14 +119,20 @@ class CreateSpaceDialogState extends State { TextField( controller: nameController, onChanged: (value) { - enteredName = value; + enteredName = value.trim(); setState(() { - if (value.isNotEmpty) { - isOkButtonEnabled = true; - isNameFieldInvalid = false; - } else { - isNameFieldInvalid = true; - isOkButtonEnabled = false; + isNameFieldExist = false; + isOkButtonEnabled = false; + isNameFieldInvalid = value.isEmpty; + + if (!isNameFieldInvalid) { + if (widget.parentSpace?.children + .any((child) => child.name == value) ?? + false) { + isNameFieldExist = true; + } else { + isOkButtonEnabled = true; + } } }); }, @@ -140,8 +149,9 @@ class CreateSpaceDialogState extends State { enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: BorderSide( - color: - isNameFieldInvalid ? ColorsManager.red : ColorsManager.boxColor, + color: isNameFieldInvalid || isNameFieldExist + ? ColorsManager.red + : ColorsManager.boxColor, width: 1.5, ), ), @@ -165,6 +175,17 @@ class CreateSpaceDialogState extends State { ?.copyWith(color: ColorsManager.red), ), ), + if (isNameFieldExist) + Padding( + padding: const EdgeInsets.only(top: 8.0), + child: Text( + '*Name already exist', + style: Theme.of(context) + .textTheme + .bodySmall + ?.copyWith(color: ColorsManager.red), + ), + ), const SizedBox(height: 16), if (selectedProducts.isNotEmpty) _buildSelectedProductsButtons(widget.products ?? []) @@ -269,7 +290,7 @@ class CreateSpaceDialogState extends State { spaceIconList: spaceIconList, onIconSelected: (String selectedIcon) { setState(() { - this.selectedIcon = selectedIcon; + this.selectedIcon = selectedIcon; }); }, );