From 1d35377137ce61689e9c5856db93fb80b7e3bb9b Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Sun, 5 Jan 2025 12:07:48 +0400 Subject: [PATCH] added white background color to container --- .../space_model/bloc/subspace_model_bloc.dart | 44 +++- .../bloc/subspace_model_state.dart | 14 + .../dialog/create_space_model_dialog.dart | 2 +- .../dialog/create_subspace_model_dialog.dart | 246 +++++++++--------- 4 files changed, 182 insertions(+), 124 deletions(-) diff --git a/lib/pages/spaces_management/space_model/bloc/subspace_model_bloc.dart b/lib/pages/spaces_management/space_model/bloc/subspace_model_bloc.dart index 41c1d76f..7455d8a5 100644 --- a/lib/pages/spaces_management/space_model/bloc/subspace_model_bloc.dart +++ b/lib/pages/spaces_management/space_model/bloc/subspace_model_bloc.dart @@ -4,15 +4,36 @@ import 'package:syncrow_web/pages/spaces_management/space_model/bloc/subspace_mo import 'package:syncrow_web/pages/spaces_management/space_model/models/space_template_model.dart'; import 'package:syncrow_web/utils/constants/action_enum.dart'; -// BLoC class SubSpaceModelBloc extends Bloc { - SubSpaceModelBloc() : super(SubSpaceModelState([], [])) { + SubSpaceModelBloc() : super(SubSpaceModelState([], [], '')) { + // Handle AddSubSpaceModel Event on((event, emit) { - final updatedSubSpaces = List.from(state.subSpaces) - ..add(event.subSpace); - emit(SubSpaceModelState(updatedSubSpaces, state.updatedSubSpaceModels)); + // Check for duplicate names (case-insensitive) + final existingNames = + state.subSpaces.map((e) => e.subspaceName.toLowerCase()).toSet(); + + if (existingNames.contains(event.subSpace.subspaceName.toLowerCase())) { + // Emit state with an error message if duplicate name exists + emit(SubSpaceModelState( + state.subSpaces, + state.updatedSubSpaceModels, + 'Subspace name already exists.', + )); + } else { + // Add subspace if no duplicate exists + final updatedSubSpaces = + List.from(state.subSpaces) + ..add(event.subSpace); + + emit(SubSpaceModelState( + updatedSubSpaces, + state.updatedSubSpaceModels, + '', // Clear error message + )); + } }); + // Handle RemoveSubSpaceModel Event on((event, emit) { final updatedSubSpaces = List.from(state.subSpaces) ..remove(event.subSpace); @@ -28,9 +49,14 @@ class SubSpaceModelBloc extends Bloc { )); } - emit(SubSpaceModelState(updatedSubSpaces, updatedSubspaceModels)); + emit(SubSpaceModelState( + updatedSubSpaces, + updatedSubspaceModels, + '', // Clear error message + )); }); + // Handle UpdateSubSpaceModel Event on((event, emit) { final updatedSubSpaces = state.subSpaces.map((subSpace) { if (subSpace.uuid == event.updatedSubSpace.uuid) { @@ -48,7 +74,11 @@ class SubSpaceModelBloc extends Bloc { uuid: event.updatedSubSpace.uuid!, )); - emit(SubSpaceModelState(updatedSubSpaces, updatedSubspaceModels)); + emit(SubSpaceModelState( + updatedSubSpaces, + updatedSubspaceModels, + '', // Clear error message + )); }); } } diff --git a/lib/pages/spaces_management/space_model/bloc/subspace_model_state.dart b/lib/pages/spaces_management/space_model/bloc/subspace_model_state.dart index 1579f32f..9026cb06 100644 --- a/lib/pages/spaces_management/space_model/bloc/subspace_model_state.dart +++ b/lib/pages/spaces_management/space_model/bloc/subspace_model_state.dart @@ -3,9 +3,23 @@ import 'package:syncrow_web/pages/spaces_management/space_model/models/space_tem class SubSpaceModelState { final List subSpaces; final List updatedSubSpaceModels; + final String errorMessage; SubSpaceModelState( this.subSpaces, this.updatedSubSpaceModels, + this.errorMessage, ); + + SubSpaceModelState copyWith({ + List? subSpaces, + List? updatedSubSpaceModels, + String? errorMessage, + }) { + return SubSpaceModelState( + subSpaces ?? this.subSpaces, + updatedSubSpaceModels ?? this.updatedSubSpaceModels, + errorMessage ?? this.errorMessage, + ); + } } diff --git a/lib/pages/spaces_management/space_model/widgets/dialog/create_space_model_dialog.dart b/lib/pages/spaces_management/space_model/widgets/dialog/create_space_model_dialog.dart index 77ca4e6d..cdb4ab60 100644 --- a/lib/pages/spaces_management/space_model/widgets/dialog/create_space_model_dialog.dart +++ b/lib/pages/spaces_management/space_model/widgets/dialog/create_space_model_dialog.dart @@ -102,7 +102,7 @@ class CreateSpaceModelDialog extends StatelessWidget { style: TextButton.styleFrom( padding: EdgeInsets.zero, ), - child: ButtonContentWidget( + child: const ButtonContentWidget( icon: Icons.add, label: 'Add Devices', ), diff --git a/lib/pages/spaces_management/space_model/widgets/dialog/create_subspace_model_dialog.dart b/lib/pages/spaces_management/space_model/widgets/dialog/create_subspace_model_dialog.dart index 31a1f0d0..c603b025 100644 --- a/lib/pages/spaces_management/space_model/widgets/dialog/create_subspace_model_dialog.dart +++ b/lib/pages/spaces_management/space_model/widgets/dialog/create_subspace_model_dialog.dart @@ -41,131 +41,145 @@ class CreateSubSpaceModelDialog extends StatelessWidget { }, child: BlocBuilder( builder: (context, state) { - return SizedBox( - width: screenWidth * 0.35, - child: Padding( - padding: const EdgeInsets.all(16.0), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - mainAxisSize: MainAxisSize.min, - children: [ - Text( - dialogTitle, - style: Theme.of(context) - .textTheme - .headlineLarge - ?.copyWith(color: ColorsManager.blackColor), - ), - const SizedBox(height: 16), - Container( - width: screenWidth * 0.35, - padding: const EdgeInsets.symmetric( - vertical: 10.0, horizontal: 16.0), - decoration: BoxDecoration( - color: ColorsManager.boxColor, - borderRadius: BorderRadius.circular(10), - ), - child: Wrap( - spacing: 8.0, - runSpacing: 8.0, - children: [ - ...state.subSpaces.map( - (subSpace) => Chip( - label: Text( - subSpace.subspaceName, - style: const TextStyle( - color: ColorsManager.spaceColor), - ), - backgroundColor: ColorsManager.whiteColors, - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(8), - side: const BorderSide( - color: ColorsManager.transparentColor, - width: 0, + return Container( + color: ColorsManager.whiteColors, + child: SizedBox( + width: screenWidth * 0.35, + child: Padding( + padding: const EdgeInsets.all(16.0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [ + Text( + dialogTitle, + style: Theme.of(context) + .textTheme + .headlineLarge + ?.copyWith(color: ColorsManager.blackColor), + ), + const SizedBox(height: 16), + Container( + width: screenWidth * 0.35, + padding: const EdgeInsets.symmetric( + vertical: 10.0, horizontal: 16.0), + decoration: BoxDecoration( + color: ColorsManager.boxColor, + borderRadius: BorderRadius.circular(10), + ), + child: Wrap( + spacing: 8.0, + runSpacing: 8.0, + children: [ + ...state.subSpaces.map( + (subSpace) => Chip( + label: Text( + subSpace.subspaceName, + style: const TextStyle( + color: ColorsManager.spaceColor), + ), + backgroundColor: ColorsManager.whiteColors, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(8), + side: const BorderSide( + color: ColorsManager.transparentColor, + width: 0, + ), + ), + deleteIcon: Container( + width: 24, + height: 24, + decoration: BoxDecoration( + shape: BoxShape.circle, + border: Border.all( + color: ColorsManager.lightGrayColor, + width: 1.5, + ), + ), + child: const Icon( + Icons.close, + size: 16, + color: ColorsManager.lightGrayColor, + ), + ), + onDeleted: () => context + .read() + .add(RemoveSubSpaceModel(subSpace)), ), ), - deleteIcon: Container( - width: 24, - height: 24, - decoration: BoxDecoration( - shape: BoxShape.circle, - border: Border.all( - color: ColorsManager.lightGrayColor, - width: 1.5, + SizedBox( + width: 200, + child: TextField( + controller: textController, + decoration: InputDecoration( + border: InputBorder.none, + hintText: state.subSpaces.isEmpty + ? 'Please enter the name' + : null, + hintStyle: const TextStyle( + color: ColorsManager.lightGrayColor), + ), + onSubmitted: (value) { + if (value.trim().isNotEmpty) { + context.read().add( + AddSubSpaceModel( + SubspaceTemplateModel( + subspaceName: value.trim(), + disabled: false))); + textController.clear(); + } + }, + style: const TextStyle( + color: ColorsManager.blackColor), + ), + ), + if (state.errorMessage.isNotEmpty) + Padding( + padding: const EdgeInsets.only(top: 8.0), + child: Text( + state.errorMessage, + style: const TextStyle( + color: ColorsManager.warningRed, + fontSize: 12, + ), ), ), - child: const Icon( - Icons.close, - size: 16, - color: ColorsManager.lightGrayColor, - ), - ), - onDeleted: () => context - .read() - .add(RemoveSubSpaceModel(subSpace)), - ), - ), - SizedBox( - width: 200, - child: TextField( - controller: textController, - decoration: InputDecoration( - border: InputBorder.none, - hintText: state.subSpaces.isEmpty - ? 'Please enter the name' - : null, - hintStyle: const TextStyle( - color: ColorsManager.lightGrayColor), - ), - onSubmitted: (value) { - if (value.trim().isNotEmpty) { - context.read().add( - AddSubSpaceModel(SubspaceTemplateModel( - subspaceName: value.trim(), - disabled: false))); - textController.clear(); - } - }, - style: const TextStyle( - color: ColorsManager.blackColor), - ), - ), - ], - ), - ), - const SizedBox(height: 16), - Row( - children: [ - Expanded( - child: CancelButton( - label: 'Cancel', - onPressed: () { - Navigator.of(context).pop(); - }, + ], ), ), - const SizedBox(width: 10), - Expanded( - child: DefaultButton( - onPressed: () { - final subSpaces = context - .read() - .state - .subSpaces; - Navigator.of(context).pop(subSpaces); - }, - backgroundColor: ColorsManager.secondaryColor, - borderRadius: 10, - foregroundColor: ColorsManager.whiteColors, - child: const Text('OK'), - ), + const SizedBox(height: 16), + Row( + children: [ + Expanded( + child: CancelButton( + label: 'Cancel', + onPressed: () { + Navigator.of(context).pop(); + }, + ), + ), + const SizedBox(width: 10), + Expanded( + child: DefaultButton( + onPressed: () { + final subSpaces = context + .read() + .state + .subSpaces; + Navigator.of(context).pop(subSpaces); + }, + backgroundColor: ColorsManager.secondaryColor, + borderRadius: 10, + foregroundColor: ColorsManager.whiteColors, + child: const Text('OK'), + ), + ), + ], ), ], ), - ], - ), - ), - ); + ), + )); }, ), ),