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
74 stars 84 forks source link

selectedOptions do not update #78

Closed JaviBonilla closed 4 months ago

JaviBonilla commented 8 months ago

Changing selectedOptions in the onItemDelete does not update the widget.

Example:

import 'package:flutter/material.dart';
import 'package:multi_dropdown/multiselect_dropdown.dart';
import 'package:multi_dropdown/widgets/selection_chip.dart';

void main() {
  runApp(const MainApp());
}

const numOptions = 5;

class MainApp extends StatefulWidget {
  const MainApp({super.key});

  @override
  State<MainApp> createState() => _MainAppState();
}

class _MainAppState extends State<MainApp> {
  final List<int> selected = [];

  ValueItem<int> valueItem(int value) =>
      ValueItem(label: 'Item $value', value: value);

  @override
  Widget build(BuildContext context) {
    print('selected: $selected');
    return MaterialApp(
        home: Scaffold(
      body: Padding(
          padding: const EdgeInsets.all(50),
          child: MultiSelectDropDown<int>(
            selectedOptions: selected.map((value) => valueItem(value)).toList(),
            onOptionSelected: (List<ValueItem<int>> selectedList) {
              setState(() {
                selected.clear();
                selected.addAll(selectedList.map((e) => e.value ?? 0).toList());
              });
            },
            options: <ValueItem<int>>[
              for (int i = 1; i <= numOptions; i++) valueItem(i),
            ],
            selectedItemBuilder: (context, item) {
              return SelectionChip(
                chipConfig: const ChipConfig(
                  backgroundColor: Colors.purple,
                  labelColor: Colors.white,
                ),
                item: item,
                onItemDelete: (item) {
                  setState(() {
                    selected.remove(item.value);
                  });
                },
              );
            },
          )),
    ));
  }
}

https://github.com/oi-narendra/multiselect-dropdown/assets/20316179/47fa7d81-12a4-452e-813c-ca1d4846bb05

JaviBonilla commented 8 months ago

Ok, I've just realized that for that I should have used MultiSelectController 😅.