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 98251382..70dde231 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 @@ -145,13 +145,11 @@ class CreateSpaceModelDialog extends StatelessWidget { ), const SizedBox(height: 10), TagChipDisplay( - context, - screenWidth: screenWidth, spaceModel: updatedSpaceModel, products: products, subspaces: subspaces, allTags: allTags, - spaceNameController: spaceNameController, + spaceName: spaceNameController.text, pageContext: pageContext, otherSpaceModels: otherSpaceModels, allSpaceModels: allSpaceModels, diff --git a/lib/pages/spaces_management/space_model/widgets/tag_chips_display_widget.dart b/lib/pages/spaces_management/space_model/widgets/tag_chips_display_widget.dart index fc6a8c88..86f99e02 100644 --- a/lib/pages/spaces_management/space_model/widgets/tag_chips_display_widget.dart +++ b/lib/pages/spaces_management/space_model/widgets/tag_chips_display_widget.dart @@ -10,139 +10,152 @@ import 'package:syncrow_web/pages/spaces_management/space_model/models/subspace_ import 'package:syncrow_web/pages/spaces_management/space_model/widgets/button_content_widget.dart'; import 'package:syncrow_web/pages/spaces_management/tag_model/views/add_device_type_model_widget.dart'; import 'package:syncrow_web/utils/color_manager.dart'; +import 'package:syncrow_web/utils/constants/assets.dart'; +import 'package:syncrow_web/utils/extension/build_context_x.dart'; class TagChipDisplay extends StatelessWidget { - final double screenWidth; + const TagChipDisplay({ + required this.spaceModel, + required this.products, + required this.subspaces, + required this.allTags, + required this.spaceName, + required this.projectTags, + this.pageContext, + this.otherSpaceModels, + this.allSpaceModels, + super.key, + }); + final SpaceTemplateModel? spaceModel; final List? products; final List? subspaces; final List? allTags; - final TextEditingController spaceNameController; + final String spaceName; final BuildContext? pageContext; final List? otherSpaceModels; final List? allSpaceModels; final List projectTags; - const TagChipDisplay(BuildContext context, - {Key? key, - required this.screenWidth, - required this.spaceModel, - required this.products, - required this.subspaces, - required this.allTags, - required this.spaceNameController, - this.pageContext, - this.otherSpaceModels, - this.allSpaceModels, - required this.projectTags}) - : super(key: key); + Map get _groupedTags { + final spaceTags = spaceModel?.tags ?? []; + + final subspaces = spaceModel?.subspaceModels ?? []; + final subspaceTags = subspaces.expand((e) => e.tags ?? []).toList(); + + return TagHelper.groupTags([...spaceTags, ...subspaceTags]); + } @override Widget build(BuildContext context) { - return (spaceModel?.tags?.isNotEmpty == true || - spaceModel?.subspaceModels?.any((subspace) => subspace.tags?.isNotEmpty == true) == - true) - ? SizedBox( - width: screenWidth * 0.25, - child: Container( - padding: const EdgeInsets.all(8.0), - decoration: BoxDecoration( - color: ColorsManager.textFieldGreyColor, - borderRadius: BorderRadius.circular(15), - border: Border.all( - color: ColorsManager.textFieldGreyColor, - width: 3.0, // Border width - ), - ), - child: Wrap( - spacing: 8.0, - runSpacing: 8.0, - children: [ - // Combine tags from spaceModel and subspaces - ...TagHelper.groupTags([ - ...?spaceModel?.tags, - ...?spaceModel?.subspaceModels?.expand((subspace) => subspace.tags ?? []) - ]).entries.map( - (entry) => Chip( - avatar: SizedBox( - width: 24, - height: 24, - child: SvgPicture.asset( - entry.key.icon ?? 'assets/icons/gateway.svg', - fit: BoxFit.contain, - ), - ), - label: Text( - 'x${entry.value}', // Show count - style: Theme.of(context) - .textTheme - .bodySmall! - .copyWith(color: ColorsManager.spaceColor), - ), - backgroundColor: ColorsManager.whiteColors, - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(16), - side: const BorderSide( - color: ColorsManager.spaceColor, - ), - ), - ), - ), - EditChip(onTap: () async { - // Use the Navigator's context for showDialog - Navigator.of(context).pop(); + final hasTags = spaceModel?.tags?.isNotEmpty ?? false; + final hasSubspaceTags = + spaceModel?.subspaceModels?.any((e) => e.tags?.isNotEmpty ?? false) ?? false; - await showDialog( - barrierDismissible: false, - context: context, - builder: (context) => AssignTagModelsDialog( - products: products, - allSpaceModels: allSpaceModels, - subspaces: subspaces, - pageContext: pageContext, - allTags: allTags, - spaceModel: spaceModel, - otherSpaceModels: otherSpaceModels, - initialTags: TagHelper.generateInitialTags( - subspaces: subspaces, spaceTagModels: spaceModel?.tags ?? []), - title: 'Edit Device', - addedProducts: TagHelper.createInitialSelectedProducts( - spaceModel?.tags ?? [], subspaces), - spaceName: spaceModel?.modelName ?? '', - projectTags: projectTags, - )); - }) - ], - ), - ), - ) - : TextButton( - onPressed: () async { - Navigator.of(context).pop(); + if (hasTags || hasSubspaceTags) { + return Container( + width: context.screenWidth * 0.25, + padding: const EdgeInsets.all(8), + decoration: BoxDecoration( + color: ColorsManager.textFieldGreyColor, + borderRadius: BorderRadius.circular(15), + border: Border.all( + color: ColorsManager.textFieldGreyColor, + width: 3, + ), + ), + child: Wrap( + spacing: 8, + runSpacing: 8, + children: [ + ..._groupedTags.entries.map((entry) => _buildChip(context, entry)), + _buildEditChip(context), + ], + ), + ); + } - await showDialog( - barrierDismissible: false, - context: context, - builder: (context) => AddDeviceTypeModelWidget( - products: products, - subspaces: subspaces, - allTags: allTags, - spaceName: spaceNameController.text, - pageContext: pageContext, - isCreate: true, - spaceModel: spaceModel, - otherSpaceModels: otherSpaceModels, - projectTags: projectTags, - ), - ); - }, - style: TextButton.styleFrom( - padding: EdgeInsets.zero, - ), - child: const ButtonContentWidget( - icon: Icons.add, - label: 'Add Devices', - ), - ); + return _buildAddDevicesButton(context); + } + + Widget _buildEditChip(BuildContext context) { + return EditChip( + onTap: () => showDialog( + context: context, + builder: (context) => AssignTagModelsDialog( + products: products, + allSpaceModels: allSpaceModels, + subspaces: subspaces, + pageContext: pageContext, + allTags: allTags, + spaceModel: spaceModel, + otherSpaceModels: otherSpaceModels, + initialTags: TagHelper.generateInitialTags( + subspaces: subspaces, + spaceTagModels: spaceModel?.tags ?? [], + ), + title: 'Edit Device', + addedProducts: TagHelper.createInitialSelectedProducts( + spaceModel?.tags ?? [], + subspaces, + ), + spaceName: spaceModel?.modelName ?? '', + projectTags: projectTags, + ), + ), + ); + } + + Widget _buildChip( + BuildContext context, + MapEntry entry, + ) { + return Chip( + backgroundColor: ColorsManager.whiteColors, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(16), + side: const BorderSide( + color: ColorsManager.spaceColor, + ), + ), + avatar: SvgPicture.asset( + entry.key.icon ?? Assets.gateway, + fit: BoxFit.contain, + height: 24, + width: 24, + ), + label: Text( + '${entry.value}', + style: context.textTheme.bodySmall!.copyWith( + color: ColorsManager.spaceColor, + ), + ), + ); + } + + Widget _buildAddDevicesButton(BuildContext context) { + return TextButton( + onPressed: () => showDialog( + context: context, + builder: (context) => AddDeviceTypeModelWidget( + products: products, + subspaces: subspaces, + allTags: allTags, + spaceName: spaceName, + pageContext: pageContext, + isCreate: true, + spaceModel: spaceModel, + otherSpaceModels: otherSpaceModels, + projectTags: projectTags, + ), + ), + style: TextButton.styleFrom( + padding: EdgeInsets.zero, + ), + child: const ButtonContentWidget( + icon: Icons.add, + label: 'Add Devices', + ), + ); } }