oi-narendra / multiselect-dropdown

Streamlined Flutter widget for versatile multi-selection with extensive customization.
https://pub.dev/packages/multi_dropdown
GNU General Public License v3.0
73 stars 84 forks source link

Updating Items, not updating the dropdown. #156

Open singgihmardianto opened 2 months ago

singgihmardianto commented 2 months ago

Hi there, great plugin here! Basically, so thankful with this plugin. But I am facing an issue when I updating the items parameters.

First, I have a component called InputMultiselect that wrap this widget. Something like this

class InputMultiSelect<T extends Object> extends StatelessWidget {
  const InputMultiSelect({
    super.key,
    this.items,
    this.selectedItem,
    this.onChange,
    this.name,
  });

  final List<T>? items;
  final List<T>? selectedItem;
  final String? name;
  final Function(List<T>? selected)? onChange;

  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.fromLTRB(0, 0, 12, 12),
      child: SizedBox.fromSize(
        size: Size.fromHeight(39),
        child: MultiDropdown<T>(
          items: items!
              .map((item) => DropdownItem(
                  label: item.toString(),
                  value: item,
                  selected: selectedItem!.contains(item)))
              .toList(),
          onSelectionChange: (selected) => onChange!(selected),
          searchEnabled: true,
        ),
      ),
    );
  }
}

Then I will use in it my form simply just like this:

InputMultiSelect(
  name: "Some Input",
  selectedItem: _categories,
  items: _categoryOptions,
  onChange: onCategoryChange,
),

The problem is whenever I update _categoryOptions using setState() the dropdown will not update the items. I already do a trace to look how the plugin works, so may be I can get a hot fix for my issue.

I debug widget.items on line 383 the items is updated. image

But here on line 433, it's uses _dropdownController.items instead of widget.items image

There I assume may be the reason why the dropdown it's not updating.

Any idea how to do a hot fix here? Or Am I missed something? Thanks!

papakay commented 2 weeks ago

@singgihmardianto please were you able to solve this issue?

oi-narendra commented 2 weeks ago

class InputMultiSelect extends StatefulWidget { const InputMultiSelect({ super.key, this.items, this.selectedItem, this.onChange, this.name, });

final List? items; final List? selectedItem; final String? name; final Function(List? selected)? onChange;

@override State createState() => _InputMultiSelectState(); }

class _InputMultiSelectState extends State<InputMultiSelect> { final MultiSelectController controller = MultiSelectController();

@override void initState() { super.initState(); }

@override void didUpdateWidget(InputMultiSelect oldWidget) { super.didUpdateWidget(oldWidget);

if (!listEquals(oldWidget.items, widget.items)) { controller.setItems(widget.items! .map((item) => DropdownItem( label: item.toString(), value: item, selected: widget.selectedItem!.contains(item))) .toList()); } }

@override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.fromLTRB(0, 0, 12, 12), child: SizedBox.fromSize( size: const Size.fromHeight(39), child: MultiDropdown( controller: controller, items: widget.items! .map((item) => DropdownItem( label: item.toString(), value: item, selected: widget.selectedItem!.contains(item))) .toList(), onSelectionChange: (selected) => widget.onChange!(selected), searchEnabled: true, ), ), ); } }

On Tue, Nov 19, 2024 at 3:24 PM Kehinde Alabi @.***> wrote:

@singgihmardianto https://github.com/singgihmardianto please were you able to solve this issue?

— Reply to this email directly, view it on GitHub https://github.com/oi-narendra/multiselect-dropdown/issues/156#issuecomment-2484679541, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEF7OWXGNJTJ2OR465S2JN32BK4P3AVCNFSM6AAAAABOGPADMKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDIOBUGY3TSNJUGE . You are receiving this because you are subscribed to this thread.Message ID: @.***>