From 05f3d89930ec7a3cfa2954b5faff40fc883e3cf6 Mon Sep 17 00:00:00 2001 From: Sikander Saleem Date: Sun, 10 May 2026 11:36:49 +0300 Subject: [PATCH] multiple dropdown selection fixed. --- .../export_module/pages/export_form_view.dart | 22 ++---- .../multiple_item_drop_down_menu.dart | 76 +++++++++---------- .../muliple_selection_bottom_sheet.dart | 10 ++- .../multiple_selection_fullscreen_dialog.dart | 8 +- 4 files changed, 55 insertions(+), 61 deletions(-) diff --git a/lib/modules/export_module/pages/export_form_view.dart b/lib/modules/export_module/pages/export_form_view.dart index 446524dd..feef4583 100644 --- a/lib/modules/export_module/pages/export_form_view.dart +++ b/lib/modules/export_module/pages/export_form_view.dart @@ -45,25 +45,17 @@ class _ExportFormViewState extends State> { Lookup? selectedFormat; void _updateSelectedColumns(List? 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 extends State> { showAsBottomSheet: false, showAsFullScreenDialog: true, requestById: 1, + initialValue: selectedFormat, onSelect: _updateSelectedFormat, ), 18.height, @@ -98,6 +91,7 @@ class _ExportFormViewState extends State> { showShadow: false, height: 56.toScreenHeight, showCancel: true, + initialValue: selectedColumns, requestById: widget.moduleType.id, onSelect: _updateSelectedColumns, ), diff --git a/lib/new_views/common_widgets/multiple_item_drop_down_menu.dart b/lib/new_views/common_widgets/multiple_item_drop_down_menu.dart index 645b9514..7b07ffce 100644 --- a/lib/new_views/common_widgets/multiple_item_drop_down_menu.dart +++ b/lib/new_views/common_widgets/multiple_item_drop_down_menu.dart @@ -78,21 +78,11 @@ class _MultipleItemDropDownMenuState()); - // 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 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()); - - // 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()); + + // 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 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; diff --git a/lib/views/widgets/bottom_sheets/muliple_selection_bottom_sheet.dart b/lib/views/widgets/bottom_sheets/muliple_selection_bottom_sheet.dart index a9abe36e..6d6130c4 100644 --- a/lib/views/widgets/bottom_sheets/muliple_selection_bottom_sheet.dart +++ b/lib/views/widgets/bottom_sheets/muliple_selection_bottom_sheet.dart @@ -33,7 +33,9 @@ class _MultipleSelectionBottomSheetState extends State.from(widget.selectedItem); super.initState(); } @@ -78,8 +80,10 @@ class _MultipleSelectionBottomSheetState extends State extends State extends State.from(widget.selectedItem); super.initState(); } @@ -75,8 +77,8 @@ class _SelectionBottomSheetState extends State extends State