From 42c7577b35d3bb77595319d2e76b60a3b63c8551 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Fri, 29 Nov 2024 19:03:27 +0400 Subject: [PATCH] validity for space name --- .../widgets/community_structure_widget.dart | 1 + .../dialogs/create_community_dialog.dart | 38 +++++++++---- .../widgets/dialogs/create_space_dialog.dart | 57 +++++++++++++------ .../widgets/sidebar_widget.dart | 2 - 4 files changed, 68 insertions(+), 30 deletions(-) diff --git a/lib/pages/spaces_management/widgets/community_structure_widget.dart b/lib/pages/spaces_management/widgets/community_structure_widget.dart index c8d7c8d7..22f0d218 100644 --- a/lib/pages/spaces_management/widgets/community_structure_widget.dart +++ b/lib/pages/spaces_management/widgets/community_structure_widget.dart @@ -331,6 +331,7 @@ class _CommunityStructureAreaState extends State { products: widget.products, name: space.name, icon: space.icon, + editSpace: space, isEdit: true, selectedProducts: space.selectedProducts, onCreateSpace: (String name, String icon, diff --git a/lib/pages/spaces_management/widgets/dialogs/create_community_dialog.dart b/lib/pages/spaces_management/widgets/dialogs/create_community_dialog.dart index 0db7c461..19ca8a99 100644 --- a/lib/pages/spaces_management/widgets/dialogs/create_community_dialog.dart +++ b/lib/pages/spaces_management/widgets/dialogs/create_community_dialog.dart @@ -18,6 +18,7 @@ class CreateCommunityDialog extends StatefulWidget { class CreateCommunityDialogState extends State { String enteredName = ''; bool isNameFieldExist = false; + bool isNameEmpty = false; @override Widget build(BuildContext context) { @@ -63,10 +64,15 @@ class CreateCommunityDialogState extends State { 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 { ), border: OutlineInputBorder( borderSide: BorderSide( - color: isNameFieldExist + color: isNameFieldExist || isNameEmpty ? ColorsManager.red : ColorsManager.boxColor, width: 1), @@ -91,16 +97,15 @@ class CreateCommunityDialogState extends State { ), 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 { ?.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 { Navigator.of(context).pop(); } }, - backgroundColor: isNameFieldExist || enteredName.isEmpty + backgroundColor: isNameFieldExist || isNameEmpty ? ColorsManager.lightGrayColor : ColorsManager.secondaryColor, borderRadius: 10, 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 eba5096c..028729b8 100644 --- a/lib/pages/spaces_management/widgets/dialogs/create_space_dialog.dart +++ b/lib/pages/spaces_management/widgets/dialogs/create_space_dialog.dart @@ -13,13 +13,15 @@ import 'package:syncrow_web/utils/constants/assets.dart'; import 'package:syncrow_web/utils/constants/space_icon_const.dart'; class CreateSpaceDialog extends StatefulWidget { - final Function(String, String, List selectedProducts) onCreateSpace; + final Function(String, String, List selectedProducts) + onCreateSpace; final List? products; final String? name; final String? icon; final bool isEdit; final List selectedProducts; final SpaceModel? parentSpace; + final SpaceModel? editSpace; const CreateSpaceDialog( {super.key, @@ -29,6 +31,7 @@ class CreateSpaceDialog extends StatefulWidget { this.name, this.icon, this.isEdit = false, + this.editSpace, this.selectedProducts = const []}); @override @@ -49,8 +52,10 @@ class CreateSpaceDialogState extends State { super.initState(); selectedIcon = widget.icon ?? Assets.location; nameController = TextEditingController(text: widget.name ?? ''); - selectedProducts = widget.selectedProducts.isNotEmpty ? widget.selectedProducts : []; - isOkButtonEnabled = enteredName.isNotEmpty || nameController.text.isNotEmpty; + selectedProducts = + widget.selectedProducts.isNotEmpty ? widget.selectedProducts : []; + isOkButtonEnabled = + enteredName.isNotEmpty || nameController.text.isNotEmpty; } @override @@ -59,7 +64,9 @@ class CreateSpaceDialogState extends State { final screenWidth = MediaQuery.of(context).size.width; return AlertDialog( - title: widget.isEdit ? const Text('Edit Space') : const Text('Create New Space'), + title: widget.isEdit + ? const Text('Edit Space') + : const Text('Create New Space'), backgroundColor: ColorsManager.whiteColors, content: SizedBox( width: screenWidth * 0.5, // Limit dialog width @@ -126,11 +133,17 @@ class CreateSpaceDialogState extends State { isNameFieldInvalid = value.isEmpty; if (!isNameFieldInvalid) { - if (widget.parentSpace?.children - .any((child) => child.name == value) ?? - false) { + if ((widget.parentSpace?.children.any( + (child) => child.name == value) ?? + false) || + (widget.parentSpace?.name == value) || + (widget.editSpace?.children.any( + (child) => child.name == value) ?? + false)) { isNameFieldExist = true; + isOkButtonEnabled = false; } else { + isNameFieldExist = false; isOkButtonEnabled = true; } } @@ -218,7 +231,8 @@ class CreateSpaceDialogState extends State { padding: const EdgeInsets.only(left: 6.0), child: SvgPicture.asset( Assets.addIcon, - width: screenWidth * 0.015, // Adjust icon size + width: screenWidth * + 0.015, // Adjust icon size height: screenWidth * 0.015, ), ), @@ -226,8 +240,11 @@ class CreateSpaceDialogState extends State { Flexible( child: Text( 'Add devices / Assign a space model', - overflow: TextOverflow.ellipsis, // Prevent overflow - style: Theme.of(context).textTheme.bodyMedium, + overflow: TextOverflow + .ellipsis, // Prevent overflow + style: Theme.of(context) + .textTheme + .bodyMedium, ), ), ], @@ -262,16 +279,20 @@ class CreateSpaceDialogState extends State { }); return; } else { - String newName = enteredName.isNotEmpty ? enteredName : (widget.name ?? ''); + String newName = enteredName.isNotEmpty + ? enteredName + : (widget.name ?? ''); if (newName.isNotEmpty) { - widget.onCreateSpace(newName, selectedIcon, selectedProducts); + widget.onCreateSpace( + newName, selectedIcon, selectedProducts); Navigator.of(context).pop(); } } }, borderRadius: 10, - backgroundColor: - isOkButtonEnabled ? ColorsManager.secondaryColor : ColorsManager.grayColor, + backgroundColor: isOkButtonEnabled + ? ColorsManager.secondaryColor + : ColorsManager.grayColor, foregroundColor: ColorsManager.whiteColors, child: const Text('OK'), ), @@ -318,7 +339,8 @@ class CreateSpaceDialogState extends State { children: [ for (var i = 0; i < selectedProducts.length; i++) ...[ HoverableButton( - iconPath: _mapIconToProduct(selectedProducts[i].productId, products), + iconPath: + _mapIconToProduct(selectedProducts[i].productId, products), text: 'x${selectedProducts[i].count}', onTap: () { setState(() { @@ -328,7 +350,8 @@ class CreateSpaceDialogState extends State { }, ), if (i < selectedProducts.length - 1) - const SizedBox(width: 2), // Add space except after the last button + const SizedBox( + width: 2), // Add space except after the last button ], const SizedBox(width: 2), GestureDetector( @@ -364,6 +387,8 @@ class CreateSpaceDialogState extends State { ); } + + String _mapIconToProduct(String uuid, List products) { // Find the product with the matching UUID final product = products.firstWhere( diff --git a/lib/pages/spaces_management/widgets/sidebar_widget.dart b/lib/pages/spaces_management/widgets/sidebar_widget.dart index 9dd61501..e11be2c2 100644 --- a/lib/pages/spaces_management/widgets/sidebar_widget.dart +++ b/lib/pages/spaces_management/widgets/sidebar_widget.dart @@ -7,7 +7,6 @@ import 'package:syncrow_web/pages/spaces_management/bloc/space_management_event. import 'package:syncrow_web/pages/spaces_management/model/community_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/space_model.dart'; import 'package:syncrow_web/pages/spaces_management/widgets/community_tile.dart'; -import 'package:syncrow_web/pages/spaces_management/widgets/dialogs/create_community_dialog.dart'; import 'package:syncrow_web/pages/spaces_management/widgets/space_tile_widget.dart'; import 'package:syncrow_web/utils/color_manager.dart'; import 'package:syncrow_web/utils/constants/assets.dart'; @@ -204,7 +203,6 @@ class _SidebarWidgetState extends State { Widget _buildSpaceTile(SpaceModel space, CommunityModel community) { bool isExpandedSpace = _isSpaceOrChildSelected(space); -// Check if space should be expanded return SpaceTile( title: space.name, key: ValueKey(space.uuid),