multiple dropdown selection fixed.

main_production_RSG
Sikander Saleem 2 months ago
parent 40ec8947af
commit 05f3d89930

@ -45,25 +45,17 @@ class _ExportFormViewState<T> extends State<ExportFormView<T>> {
Lookup? selectedFormat;
void _updateSelectedColumns(List<AssetColumnMetadata>? value) {
if (value != null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
setState(() {
selectedColumns = value;
});
}
if (value != null && mounted) {
setState(() {
selectedColumns = value;
});
}
}
void _updateSelectedFormat(Lookup? value) {
if (value != null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
setState(() {
selectedFormat = value;
});
}
if (value != null && mounted) {
setState(() {
selectedFormat = value;
});
}
}
@ -87,6 +79,7 @@ class _ExportFormViewState<T> extends State<ExportFormView<T>> {
showAsBottomSheet: false,
showAsFullScreenDialog: true,
requestById: 1,
initialValue: selectedFormat,
onSelect: _updateSelectedFormat,
),
18.height,
@ -98,6 +91,7 @@ class _ExportFormViewState<T> extends State<ExportFormView<T>> {
showShadow: false,
height: 56.toScreenHeight,
showCancel: true,
initialValue: selectedColumns,
requestById: widget.moduleType.id,
onSelect: _updateSelectedColumns,
),

@ -78,21 +78,11 @@ class _MultipleItemDropDownMenuState<T extends Base, X extends LoadingListNotifi
..clear()
..addAll(results.cast<T>());
// Call onSelect if items changed
if (widget.onSelect != null) {
widget.onSelect!(_selectedItem);
}
// Don't call onSelect in initState - parent already has the initialValue
}
}
}
// if (widget.initialValue != null) {
// final result = (X == NullableLoadingProvider ? widget.staticData : provider?.items)?.where((element) => element.identifier == widget.initialValue?.identifier);
// if (result?.isNotEmpty ?? false) _selectedItem.add(result!.first as T);
// if (widget.onSelect != null && (widget.initialValue?.identifier ?? "") != (_selectedItem?.identifier ?? "")) {
// widget.onSelect!(_selectedItem); // Non-null assertion after null check
// }
// }
super.initState();
}
@ -103,43 +93,47 @@ class _MultipleItemDropDownMenuState<T extends Base, X extends LoadingListNotifi
@override
void didUpdateWidget(covariant MultipleItemDropDownMenu<T, X> oldWidget) {
if (widget.initialValue.isNotEmpty) {
// Get the source list
final sourceList = (X == NullableLoadingProvider ? widget.staticData : provider?.items);
if (sourceList != null && sourceList.isNotEmpty) {
// Collect matches based on identifier
final results = sourceList.where((element) => widget.initialValue!.any((init) => init.name == element.name)).toList();
if (results.isNotEmpty) {
_selectedItem
..clear()
..addAll(results.cast<T>());
// Call onSelect if items changed
if (widget.onSelect != null) {
widget.onSelect!(_selectedItem);
// Only update if initialValue actually changed from the old widget
if (_hasInitialValueChanged(oldWidget)) {
if (widget.initialValue.isNotEmpty) {
// Get the source list
final sourceList = (X == NullableLoadingProvider ? widget.staticData : provider?.items);
if (sourceList != null && sourceList.isNotEmpty) {
// Collect matches based on identifier
final results = sourceList.where((element) => widget.initialValue!.any((init) => init.name == element.name)).toList();
if (results.isNotEmpty) {
_selectedItem
..clear()
..addAll(results.cast<T>());
// Don't call onSelect here - it will create infinite loop
// The parent already has the correct value
}
}
} else {
// initialValue is empty, clear selection
_selectedItem.clear();
}
}
// if (widget.initialValue != null) {
// final result = (X == NullableLoadingProvider ? widget.staticData : provider?.items)?.where((element) => element.identifier == widget.initialValue?.identifier);
//
// if (result?.isNotEmpty ?? false) {
// _selectedItem.add(result!.first as T);
// } else {
// _selectedItem = [];
// }
// // if (widget.onSelect != null && (widget.initialValue?.identifier ?? "") != (_selectedItem?.identifier ?? "")) {
// // widget.onSelect!(_selectedItem); // Non-null assertion after null check
// // }
// } else {
// _selectedItem = [];
// }
super.didUpdateWidget(oldWidget);
}
bool _hasInitialValueChanged(MultipleItemDropDownMenu<T, X> oldWidget) {
if (oldWidget.initialValue.length != widget.initialValue.length) {
return true;
}
if (widget.initialValue.isEmpty && oldWidget.initialValue.isEmpty) {
return false;
}
// Compare items by name
for (int i = 0; i < widget.initialValue.length; i++) {
if (!oldWidget.initialValue.any((old) => old.name == widget.initialValue[i].name)) {
return true;
}
}
return false;
}
@override
Widget build(BuildContext context) {
final isEmpty = (X == NullableLoadingProvider ? widget.staticData : provider?.items)?.isEmpty ?? true;

@ -33,7 +33,9 @@ class _MultipleSelectionBottomSheetState<T> extends State<MultipleSelectionBotto
@override
void initState() {
_selectedValue = widget.selectedItem;
// Create a defensive copy to prevent modifying the original list
// This ensures temporary selections don't affect parent until confirmed
_selectedValue = List<T>.from(widget.selectedItem);
super.initState();
}
@ -78,8 +80,10 @@ class _MultipleSelectionBottomSheetState<T> extends State<MultipleSelectionBotto
),
).onPress(_selectedValue.isNotEmpty
? () {
_selectedValue = [];
setState(() {});
widget.onSelect(_selectedValue);
Navigator.pop(context);
widget.onSelect([]);
}
: null),
),
@ -157,8 +161,8 @@ class _MultipleSelectionBottomSheetState<T> extends State<MultipleSelectionBotto
label: context.translation.select,
maxWidth: true,
onPressed: () {
Navigator.pop(context);
widget.onSelect(_selectedValue);
Navigator.pop(context);
},
)),
],

@ -36,7 +36,9 @@ class _SelectionBottomSheetState<T extends Base> extends State<MultipleSelection
@override
void initState() {
_selectedValue = widget.selectedItem;
// Create a defensive copy to prevent modifying the original list
// This ensures temporary selections don't affect parent until confirmed
_selectedValue = List<T>.from(widget.selectedItem);
super.initState();
}
@ -75,8 +77,8 @@ class _SelectionBottomSheetState<T extends Base> extends State<MultipleSelection
],
).onPress(_selectedValue.isNotEmpty
? () {
Navigator.pop(context);
widget.onSelect([]);
Navigator.pop(context);
}
: null),
),
@ -178,8 +180,8 @@ class _SelectionBottomSheetState<T extends Base> extends State<MultipleSelection
label: context.translation.select,
maxWidth: true,
onPressed: () {
Navigator.pop(context);
widget.onSelect(_selectedValue);
Navigator.pop(context);
},
)),
],

Loading…
Cancel
Save