assign tag dialog

This commit is contained in:
hannathkadher
2025-01-07 16:30:36 +04:00
parent 6ee650e9f8
commit e7e0149b3a
7 changed files with 531 additions and 298 deletions

View File

@ -1,297 +0,0 @@
import 'package:flutter/material.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/subspace_template_model.dart';
import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_model.dart';
import 'package:syncrow_web/utils/color_manager.dart';
class AssignTagModelsDialog extends StatefulWidget {
final List<ProductModel>? products;
final List<SubspaceTemplateModel>? subspaces;
final List<TagModel>? initialTags;
final ValueChanged<List<TagModel>>? onTagsAssigned;
final List<SelectedProduct> addedProducts;
final List<String>? allTags;
const AssignTagModelsDialog(
{Key? key,
required this.products,
required this.subspaces,
required this.addedProducts,
this.initialTags,
this.onTagsAssigned,
this.allTags})
: super(key: key);
@override
AssignTagModelsDialogState createState() => AssignTagModelsDialogState();
}
class AssignTagModelsDialogState extends State<AssignTagModelsDialog> {
late List<TagModel> tags;
late List<SelectedProduct> selectedProducts;
late List<String> locations;
late List<String> otherTags;
late List<TextEditingController> controllers;
Set<String> usedTags = {};
String? errorMessage;
bool isSaveEnabled = true;
@override
void initState() {
super.initState();
// Initialize tags from widget.initialTags or create new ones if it's empty
tags = widget.initialTags?.isNotEmpty == true
? widget.initialTags!
: widget.addedProducts
.expand((selectedProduct) => List.generate(
selectedProduct.count, // Generate `count` number of tags
(index) => TagModel(
tag: '', // Initialize each tag with a default value
product: selectedProduct.product,
location: 'None', // Default location
),
))
.toList();
// Initialize selected products
selectedProducts = widget.addedProducts;
// Initialize locations from subspaces or empty list if null
locations = widget.subspaces != null
? widget.subspaces!.map((subspace) => subspace.subspaceName).toList()
: [];
locations.add("None");
otherTags = widget.allTags != null ? widget.allTags! : [];
controllers = List.generate(
tags.length,
(index) => TextEditingController(text: tags[index].tag),
);
for (final tag in tags) {
if (tag.tag != null && tag.tag!.isNotEmpty) {
usedTags.add(tag.tag!);
}
}
_validateTags();
}
void _validateTags() {
// Disable save if any tag is empty
final hasEmptyTag =
tags.any((tag) => tag.tag == null || tag.tag!.trim().isEmpty);
setState(() {
isSaveEnabled = !hasEmptyTag;
});
}
@override
void dispose() {
// Dispose of controllers
for (final controller in controllers) {
controller.dispose();
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Assign Tags'),
backgroundColor: ColorsManager.whiteColors,
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: MediaQuery.of(context).size.width * 0.4,
decoration: BoxDecoration(
border:
Border.all(color: ColorsManager.dataHeaderGrey, width: 1),
borderRadius: BorderRadius.circular(20),
),
child: DataTable(
border: TableBorder.all(
color: ColorsManager.dataHeaderGrey,
width: 1,
borderRadius: BorderRadius.circular(20),
),
columns: const [
DataColumn(label: Text('#')),
DataColumn(label: Text('Device')),
DataColumn(label: Text('Tag')),
DataColumn(label: Text('Location')),
],
rows: List.generate(tags.length, (index) {
final tag = tags[index];
final controller = controllers[index];
return DataRow(
cells: [
DataCell(Center(child: Text(index.toString()))),
DataCell(
Center(child: Text(tag.product?.name ?? 'Unknown'))),
DataCell(
Row(
children: [
Expanded(
child: TextFormField(
controller: controller,
decoration: const InputDecoration(
hintText: 'Enter Tag',
border: InputBorder.none,
hintStyle: TextStyle(
color: Colors.grey,
fontSize: 14,
),
),
style: const TextStyle(
color: Colors.black,
fontSize: 14,
),
onChanged: (value) {
setState(() {
tag.tag = value.trim();
_validateTags();
});
},
),
),
PopupMenuButton<String>(
icon: const Icon(Icons.arrow_drop_down,
color: Colors.black),
onSelected: (value) {
setState(() {
if (tag.tag != null && tag.tag!.isNotEmpty) {
usedTags.remove(tag.tag!);
}
controller.text = value;
tag.tag = value;
if (tag.tag != null && tag.tag!.isNotEmpty) {
usedTags.add(tag.tag!);
}
_validateTags(); // Validate after selection
});
},
color: Colors.white,
itemBuilder: (context) {
return widget.allTags!
.where((tagValue) =>
!usedTags.contains(tagValue))
.map((tagValue) {
return PopupMenuItem<String>(
value: tagValue,
child: Text(
tagValue,
style: const TextStyle(
color: Color(0xFF5D5D5D),
fontSize: 14,
),
),
);
}).toList();
},
),
],
),
),
DataCell(
Center(
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
alignment: AlignmentDirectional.center,
value: locations.contains(tag.location)
? tag.location
: null,
onChanged: (value) {
setState(() {
tag.location = value ?? 'None';
});
},
dropdownColor: ColorsManager.whiteColors,
icon: const Icon(Icons.arrow_drop_down,
color: Colors.black),
style: const TextStyle(
fontSize: 10,
fontWeight: FontWeight.w600,
color: Colors.black,
),
isExpanded: true,
items: locations
.map((location) => DropdownMenuItem(
value: location,
child: Text(
location,
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Color(0xFF5D5D5D),
),
),
))
.toList(),
),
),
),
),
],
);
}),
),
),
if (errorMessage != null)
Container(
width: MediaQuery.of(context).size.width * 0.4,
padding: const EdgeInsets.only(top: 8.0, left: 12.0),
child: Text(
errorMessage!,
style: const TextStyle(
color: Colors.red,
fontSize: 14,
fontWeight: FontWeight.bold,
),
),
),
],
),
),
actions: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Back'),
style: ElevatedButton.styleFrom(
backgroundColor: ColorsManager.boxColor,
foregroundColor: ColorsManager.blackColor,
),
),
ElevatedButton(
onPressed: isSaveEnabled
? () {
Navigator.of(context).pop();
if (widget.onTagsAssigned != null) {
widget.onTagsAssigned!(tags);
}
}
: null, // Disable the button if validation fails
child: const Text('Save'),
style: ElevatedButton.styleFrom(
backgroundColor: isSaveEnabled
? ColorsManager.secondaryColor
: Colors.grey, // Change button color when disabled
foregroundColor: ColorsManager.whiteColors,
),
),
],
),
],
);
}
}