device type select

This commit is contained in:
hannathkadher
2025-01-12 10:04:44 +04:00
parent 6591ef1664
commit 3c5e0a7778
8 changed files with 433 additions and 48 deletions

View File

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
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/add_device_type/views/add_device_type_model_widget.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/all_spaces/model/space_model.dart';
@ -28,6 +29,7 @@ class CreateSpaceDialog extends StatefulWidget {
final SpaceModel? editSpace;
final List<SpaceTemplateModel>? spaceModels;
final List<SubspaceModel>? subspaces;
final List<Tag>? tags;
const CreateSpaceDialog(
{super.key,
@ -40,7 +42,8 @@ class CreateSpaceDialog extends StatefulWidget {
this.editSpace,
this.selectedProducts = const [],
this.spaceModels,
this.subspaces});
this.subspaces,
this.tags});
@override
CreateSpaceDialogState createState() => CreateSpaceDialogState();
@ -56,6 +59,7 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
bool isNameFieldInvalid = false;
bool isNameFieldExist = false;
List<SubspaceModel>? subspaces;
List<Tag>? tags;
@override
void initState() {
@ -326,7 +330,7 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
subspaces == null
? DefaultButton(
onPressed: () {
_showSubSpaceModelDialog(context, enteredName, [],
_showSubSpaceDialog(context, enteredName, [],
false, widget.products, subspaces);
},
backgroundColor: ColorsManager.textFieldGreyColor,
@ -401,7 +405,7 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
),
GestureDetector(
onTap: () async {
_showSubSpaceModelDialog(
_showSubSpaceDialog(
context,
enteredName,
[],
@ -429,51 +433,50 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
),
),
const SizedBox(height: 10),
DefaultButton(
onPressed: () {
showDialog(
context: context,
builder: (context) => AddDeviceWidget(
products: widget.products,
onProductsSelected: (selectedProductsMap) {
setState(() {
selectedProducts = selectedProductsMap;
});
(tags?.isNotEmpty == true ||
subspaces?.any((subspace) =>
subspace.tags?.isNotEmpty == true) ==
true)
? Container()
: DefaultButton(
onPressed: () {
_showTagCreateDialog(context, enteredName, tags,
subspaces, widget.products);
},
),
);
},
backgroundColor: ColorsManager.textFieldGreyColor,
foregroundColor: Colors.black,
borderColor: ColorsManager.neutralGray,
borderRadius: 16.0,
padding: 10.0, // Reduced padding for smaller size
child: Align(
alignment: Alignment.centerLeft,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.only(left: 6.0),
child: SvgPicture.asset(
Assets.addIcon,
width: screenWidth * 0.015, // Adjust icon size
height: screenWidth * 0.015,
backgroundColor: ColorsManager.textFieldGreyColor,
foregroundColor: Colors.black,
borderColor: ColorsManager.neutralGray,
borderRadius: 16.0,
padding: 10.0, // Reduced padding for smaller size
child: Align(
alignment: Alignment.centerLeft,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.only(left: 6.0),
child: SvgPicture.asset(
Assets.addIcon,
width: screenWidth *
0.015, // Adjust icon size
height: screenWidth * 0.015,
),
),
const SizedBox(width: 3),
Flexible(
child: Text(
'Add devices',
overflow: TextOverflow
.ellipsis, // Prevent overflow
style: Theme.of(context)
.textTheme
.bodyMedium,
),
),
],
),
),
const SizedBox(width: 3),
Flexible(
child: Text(
'Add devices',
overflow:
TextOverflow.ellipsis, // Prevent overflow
style: Theme.of(context).textTheme.bodyMedium,
),
),
],
),
),
),
)
],
),
),
@ -560,6 +563,7 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
if (selectedModel != null) {
setState(() {
selectedSpaceModel = selectedModel;
subspaces = null;
});
}
},
@ -568,7 +572,7 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
);
}
void _showSubSpaceModelDialog(
void _showSubSpaceDialog(
BuildContext context,
String name,
final List<Tag>? spaceTags,
@ -589,10 +593,68 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
if (slectedSubspaces != null) {
setState(() {
subspaces = slectedSubspaces;
selectedSpaceModel = null;
});
}
});
},
);
}
void _showTagCreateDialog(
BuildContext context,
String name,
final List<Tag>? spaceTags,
List<SubspaceModel>? subspaces,
List<ProductModel>? products) {
showDialog(
context: context,
builder: (BuildContext context) {
return AddDeviceTypeWidget(
spaceName: name,
products: products,
subspaces: subspaces,
spaceTags: spaceTags,
allTags: [],
initialSelectedProducts:
createInitialSelectedProducts(tags, subspaces),
);
},
);
}
List<SelectedProduct> createInitialSelectedProducts(
List<Tag>? tags, List<SubspaceModel>? subspaces) {
final Map<ProductModel, int> productCounts = {};
if (tags != null) {
for (var tag in tags) {
if (tag.product != null) {
productCounts[tag.product!] = (productCounts[tag.product!] ?? 0) + 1;
}
}
}
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;
}
}
}
}
}
return productCounts.entries
.map((entry) => SelectedProduct(
productId: entry.key.uuid,
count: entry.value,
productName: entry.key.name ?? 'Unnamed',
product: entry.key,
))
.toList();
}
}