added validation

This commit is contained in:
hannathkadher
2025-01-06 16:38:44 +04:00
parent a31eb27c92
commit 6ee650e9f8
6 changed files with 278 additions and 131 deletions

View File

@ -11,15 +11,17 @@ class AssignTagModelsDialog extends StatefulWidget {
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,
}) : super(key: key);
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();
@ -29,12 +31,15 @@ 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();
print(widget.addedProducts);
print(widget.subspaces);
// Initialize tags from widget.initialTags or create new ones if it's empty
tags = widget.initialTags?.isNotEmpty == true
@ -58,6 +63,39 @@ class AssignTagModelsDialogState extends State<AssignTagModelsDialog> {
? 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
@ -66,123 +104,160 @@ class AssignTagModelsDialogState extends State<AssignTagModelsDialog> {
title: const Text('Assign Tags'),
backgroundColor: ColorsManager.whiteColors,
content: SingleChildScrollView(
child: Container(
width: MediaQuery.of(context).size.width * 0.4,
decoration: BoxDecoration(
border: Border.all(color: ColorsManager.dataHeaderGrey, width: 1),
borderRadius: BorderRadius.circular(20),
),
child: Theme(
data: Theme.of(context).copyWith(
dataTableTheme: DataTableThemeData(
headingRowColor:
MaterialStateProperty.all(ColorsManager.dataHeaderGrey),
headingTextStyle: const TextStyle(
color: ColorsManager.blackColor,
fontWeight: FontWeight.bold,
),
),
),
child: DataTable(
border: TableBorder.all(
color: ColorsManager.dataHeaderGrey,
width: 1,
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),
),
columns: const [
DataColumn(label: Text('#')),
DataColumn(label: Text('Device')),
DataColumn(label: Text('Tag')),
DataColumn(label: Text('Location')),
],
rows: tags.asMap().entries.map((entry) {
final index = entry.key + 1;
final tag = entry.value;
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?.catName ?? 'Unknown'),
),
),
DataCell(
Center(
child: DropdownButton<String>(
value: tag.tag!.isNotEmpty ? tag.tag : null,
onChanged: (value) {
setState(() {
tag.tag = value ?? ''; // Update tag value
});
},
items: [
const DropdownMenuItem(
value: null,
child: Text('None'),
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();
},
),
...List.generate(10, (index) {
final tagName = 'Tag ${index + 1}';
return DropdownMenuItem(
value: tagName,
child: Text(tagName),
);
}),
],
),
),
),
DataCell(
Center(
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
alignment: AlignmentDirectional.centerEnd,
value: locations.contains(tag.location)
? tag.location
: null, // Validate value
onChanged: (value) {
setState(() {
tag.location =
value ?? 'None'; // Update location
});
},
dropdownColor: Colors.white,
icon: const Icon(
Icons.arrow_drop_down,
color: Colors.black,
),
style: const TextStyle(
fontSize: 16,
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: Colors.grey,
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(),
))
.toList(),
),
),
),
),
),
],
);
}).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: [
@ -198,15 +273,19 @@ class AssignTagModelsDialogState extends State<AssignTagModelsDialog> {
),
),
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
if (widget.onTagsAssigned != null) {
widget.onTagsAssigned!(tags);
}
},
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: ColorsManager.secondaryColor,
backgroundColor: isSaveEnabled
? ColorsManager.secondaryColor
: Colors.grey, // Change button color when disabled
foregroundColor: ColorsManager.whiteColors,
),
),