mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-11-26 22:04:55 +00:00
create space model
This commit is contained in:
@ -7,10 +7,10 @@ import 'package:syncrow_web/pages/spaces_management/space_model/bloc/create_spac
|
||||
import 'package:syncrow_web/pages/spaces_management/space_model/bloc/create_space_model_event.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/space_model/bloc/create_space_model_state.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/space_model/models/space_template_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/space_model/widgets/tag_chips_display_widget.dart';
|
||||
|
||||
import 'package:syncrow_web/pages/spaces_management/space_model/widgets/subspace_model_create_widget.dart';
|
||||
import 'package:syncrow_web/services/space_model_mang_api.dart';
|
||||
import 'package:syncrow_web/utils/color_manager.dart';
|
||||
|
||||
import '../../models/subspace_template_model.dart';
|
||||
@ -26,31 +26,45 @@ class CreateSpaceModelDialog extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final SpaceModelManagementApi _spaceModelApi = SpaceModelManagementApi();
|
||||
|
||||
final screenWidth = MediaQuery.of(context).size.width;
|
||||
List<SubspaceTemplateModel>? subspaces = spaceModel?.subspaceModels ?? [];
|
||||
final TextEditingController spaceNameController = TextEditingController(
|
||||
text: spaceModel?.modelName ?? '',
|
||||
);
|
||||
|
||||
return AlertDialog(
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
|
||||
backgroundColor: ColorsManager.whiteColors,
|
||||
content: SizedBox(
|
||||
width: screenWidth * 0.3,
|
||||
child: BlocProvider(
|
||||
create: (_) {
|
||||
final bloc = CreateSpaceModelBloc();
|
||||
if (spaceModel != null) {
|
||||
bloc.add(UpdateSpaceTemplate(spaceModel!));
|
||||
} else {
|
||||
bloc.add(UpdateSpaceTemplate(SpaceTemplateModel(
|
||||
modelName: '',
|
||||
subspaceModels: [],
|
||||
)));
|
||||
}
|
||||
return bloc;
|
||||
},
|
||||
child: BlocBuilder<CreateSpaceModelBloc, CreateSpaceModelState>(
|
||||
builder: (context, state) {
|
||||
width: screenWidth * 0.3,
|
||||
child: BlocProvider(
|
||||
create: (_) {
|
||||
final bloc = CreateSpaceModelBloc(_spaceModelApi);
|
||||
if (spaceModel != null) {
|
||||
bloc.add(UpdateSpaceTemplate(spaceModel!));
|
||||
} else {
|
||||
bloc.add(UpdateSpaceTemplate(SpaceTemplateModel(
|
||||
modelName: '',
|
||||
subspaceModels: [],
|
||||
)));
|
||||
}
|
||||
|
||||
spaceNameController.addListener(() {
|
||||
bloc.add(UpdateSpaceTemplateName(name: spaceNameController.text));
|
||||
});
|
||||
|
||||
return bloc;
|
||||
},
|
||||
child: BlocBuilder<CreateSpaceModelBloc, CreateSpaceModelState>(
|
||||
builder: (context, state) {
|
||||
if (state is CreateSpaceModelLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
} else if (state is CreateSpaceModelLoaded) {
|
||||
final updatedSpaceModel = state.space;
|
||||
final subspaces = updatedSpaceModel.subspaceModels ?? [];
|
||||
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
@ -66,6 +80,11 @@ class CreateSpaceModelDialog extends StatelessWidget {
|
||||
width: screenWidth * 0.25,
|
||||
child: TextField(
|
||||
controller: spaceNameController,
|
||||
onChanged: (value) {
|
||||
context
|
||||
.read<CreateSpaceModelBloc>()
|
||||
.add(UpdateSpaceTemplateName(name: value));
|
||||
},
|
||||
style: const TextStyle(color: ColorsManager.blackColor),
|
||||
decoration: InputDecoration(
|
||||
filled: true,
|
||||
@ -85,11 +104,12 @@ class CreateSpaceModelDialog extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
SubspaceModelCreate(context, subspaces: subspaces),
|
||||
SubspaceModelCreate(context,
|
||||
subspaces: state.space.subspaceModels ?? []),
|
||||
const SizedBox(height: 10),
|
||||
TagChipDisplay(context,
|
||||
screenWidth: screenWidth,
|
||||
spaceModel: spaceModel,
|
||||
spaceModel: updatedSpaceModel,
|
||||
products: products,
|
||||
subspaces: subspaces,
|
||||
allTags: allTags,
|
||||
@ -109,8 +129,14 @@ class CreateSpaceModelDialog extends StatelessWidget {
|
||||
Expanded(
|
||||
child: DefaultButton(
|
||||
onPressed: () {
|
||||
// Return data when OK is pressed
|
||||
Navigator.of(context).pop(subspaces);
|
||||
final updatedSpaceTemplate =
|
||||
updatedSpaceModel.copyWith(
|
||||
modelName: spaceNameController.text.trim(),
|
||||
);
|
||||
context.read<CreateSpaceModelBloc>().add(
|
||||
CreateSpaceTemplate(
|
||||
spaceTemplate: updatedSpaceTemplate),
|
||||
);
|
||||
},
|
||||
backgroundColor: ColorsManager.secondaryColor,
|
||||
borderRadius: 10,
|
||||
@ -123,9 +149,19 @@ class CreateSpaceModelDialog extends StatelessWidget {
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
)),
|
||||
} else if (state is CreateSpaceModelError) {
|
||||
return Text(
|
||||
'Error: ${state.message}',
|
||||
style: const TextStyle(color: Colors.red),
|
||||
);
|
||||
}
|
||||
|
||||
// Default case (e.g., CreateSpaceModelInitial)
|
||||
return const Center(child: Text('Initializing...'));
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,7 +10,8 @@ import 'package:syncrow_web/pages/spaces_management/space_model/bloc/create_spac
|
||||
class SubspaceModelCreate extends StatelessWidget {
|
||||
final List<SubspaceTemplateModel> subspaces;
|
||||
|
||||
const SubspaceModelCreate(BuildContext context, {
|
||||
const SubspaceModelCreate(
|
||||
BuildContext context, {
|
||||
Key? key,
|
||||
required this.subspaces,
|
||||
}) : super(key: key);
|
||||
@ -26,6 +27,8 @@ class SubspaceModelCreate extends StatelessWidget {
|
||||
overlayColor: ColorsManager.transparentColor,
|
||||
),
|
||||
onPressed: () async {
|
||||
Navigator.of(context).pop();
|
||||
|
||||
final result = await showDialog<List<SubspaceTemplateModel>>(
|
||||
barrierDismissible: false,
|
||||
context: context,
|
||||
@ -58,16 +61,16 @@ class SubspaceModelCreate extends StatelessWidget {
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
decoration: BoxDecoration(
|
||||
color: ColorsManager.textFieldGreyColor,
|
||||
borderRadius: BorderRadius.circular(15),
|
||||
color: ColorsManager.textFieldGreyColor,
|
||||
borderRadius: BorderRadius.circular(15),
|
||||
border: Border.all(
|
||||
color: ColorsManager.textFieldGreyColor,
|
||||
color: ColorsManager.textFieldGreyColor,
|
||||
width: 3.0, // Border width
|
||||
),
|
||||
),
|
||||
child: Wrap(
|
||||
spacing: 8.0,
|
||||
runSpacing: 8.0,
|
||||
spacing: 8.0,
|
||||
runSpacing: 8.0,
|
||||
children: [
|
||||
...subspaces.map(
|
||||
(subspace) => Chip(
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_svg/svg.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/product_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/selected_product_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/space_model/models/space_template_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/space_model/models/subspace_template_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_model.dart';
|
||||
@ -29,7 +30,8 @@ class TagChipDisplay extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return (spaceModel?.subspaceModels?.isNotEmpty == true || spaceModel?.tags?.isNotEmpty == true ||
|
||||
return (spaceModel?.subspaceModels?.isNotEmpty == true ||
|
||||
spaceModel?.tags?.isNotEmpty == true ||
|
||||
spaceModel?.subspaceModels
|
||||
?.any((subspace) => subspace.tags?.isNotEmpty == true) ==
|
||||
true)
|
||||
@ -60,8 +62,7 @@ class TagChipDisplay extends StatelessWidget {
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: SvgPicture.asset(
|
||||
entry.key.icon ??
|
||||
'assets/default_icon.svg', // Provide a default asset path if null
|
||||
entry.key.icon ?? 'assets/icons/gateway.svg',
|
||||
fit: BoxFit.contain,
|
||||
),
|
||||
),
|
||||
@ -71,19 +72,19 @@ class TagChipDisplay extends StatelessWidget {
|
||||
color: ColorsManager.spaceColor,
|
||||
),
|
||||
),
|
||||
backgroundColor:
|
||||
Colors.white, // Chip background color
|
||||
backgroundColor: Colors.white,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius:
|
||||
BorderRadius.circular(16), // Rounded chip
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
side: const BorderSide(
|
||||
color: ColorsManager.spaceColor, // Border color
|
||||
color: ColorsManager.spaceColor,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
GestureDetector(
|
||||
onTap: () async {
|
||||
Navigator.of(context).pop();
|
||||
|
||||
await showDialog<bool>(
|
||||
barrierDismissible: false,
|
||||
context: context,
|
||||
@ -92,6 +93,10 @@ class TagChipDisplay extends StatelessWidget {
|
||||
subspaces: subspaces,
|
||||
allTags: allTags,
|
||||
spaceName: spaceNameController.text,
|
||||
spaceTagModels: spaceModel?.tags,
|
||||
initialSelectedProducts:
|
||||
_createInitialSelectedProducts(
|
||||
spaceModel?.tags, spaceModel?.subspaceModels),
|
||||
),
|
||||
);
|
||||
// Edit action
|
||||
@ -117,6 +122,8 @@ class TagChipDisplay extends StatelessWidget {
|
||||
)
|
||||
: TextButton(
|
||||
onPressed: () async {
|
||||
Navigator.of(context).pop();
|
||||
|
||||
final result = await showDialog<bool>(
|
||||
barrierDismissible: false,
|
||||
context: context,
|
||||
@ -148,4 +155,42 @@ class TagChipDisplay extends StatelessWidget {
|
||||
}
|
||||
return groupedTags;
|
||||
}
|
||||
|
||||
List<SelectedProduct> _createInitialSelectedProducts(
|
||||
List<TagModel>? tags, List<SubspaceTemplateModel>? subspaces) {
|
||||
final Map<ProductModel, int> productCounts = {};
|
||||
|
||||
// Count products in spaceModel tags
|
||||
if (tags != null) {
|
||||
for (var tag in tags) {
|
||||
if (tag.product != null) {
|
||||
productCounts[tag.product!] = (productCounts[tag.product!] ?? 0) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Count products in subspaces
|
||||
if (subspaces != null) {
|
||||
for (var subspace in subspaces) {
|
||||
if (subspace.tags != null) {
|
||||
for (var tag in subspace.tags!) {
|
||||
if (tag.product != null) {
|
||||
productCounts[tag.product!] =
|
||||
(productCounts[tag.product!] ?? 0) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create SelectedProduct instances
|
||||
return productCounts.entries
|
||||
.map((entry) => SelectedProduct(
|
||||
productId: entry.key.uuid,
|
||||
count: entry.value,
|
||||
productName: entry.key.name ?? 'Unnamed',
|
||||
product: entry.key,
|
||||
))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user