Alexandru Buruiana
Alexandru Buruiana

Reputation: 137

Flutter Add Items in ListView

How can you add items from a dropdown into a listview?

This is the code for the searchable dropdown. I created a button for adding, and I have a problem understanding how I could add a selected item from the dropdown, directly into a ListView. Curently I return just some text in the Listview. (the name Galati for each elements from the dropdown)

List<PlatformReach> _platformReach = PlatformReach.getPlatformReach();
  List<DropdownMenuItem<PlatformReach>> _dropdownPlatformReach;

  PlatformReach _selectedPlatformReach;

  void initState() {
    _dropdownMenuItems = buildDropDownMenuItems(_visibility);
    _selectedVisibility = _dropdownMenuItems[0].value;

    _dropdownPlatformReach =
        buildDropdownMenuItemsPlatformReach(_platformReach);
    _selectedPlatformReach = _dropdownPlatformReach[0].value;

    super.initState();
  }

  List<DropdownMenuItem<PlatformReach>> buildDropdownMenuItemsPlatformReach(
      List platforms) {
    List<DropdownMenuItem<PlatformReach>> items = List();
    for (PlatformReach platform in platforms) {
      items.add(
        DropdownMenuItem(
          value: platform,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text(
                platform.name,
                style: TextStyle(fontWeight: FontWeight.bold),
                textAlign: TextAlign.start,
              ),
              Text(
                platform.hint,
                style: TextStyle(
                    fontWeight: FontWeight.normal, color: Colors.grey),
                textAlign: TextAlign.end,
              )
            ],
          ),
        ),
      );
    }
    return items;
  }
Expanded(

                                  child: SearchableDropdown.single(
                                  isExpanded: true,
                                  value: _selectedPlatformReach,
                                  hint: " ",
                                  items: _dropdownPlatformReach,
                                  onChanged: (PlatformReach selectedPlatformReach) {
                                    setState(() {
                                      _selectedPlatformReach = selectedPlatformReach;
                                    });
                                  },
                                ),
                                flex: 2,
                              ),
class PlatformReach {
  String name;
  String hint;

  PlatformReach(this.name, this.hint);

  static List<PlatformReach> getPlatformReach() {
    return <PlatformReach>[
      PlatformReach('Jud Galati', '(RO, [Galati] County)'),
      PlatformReach('Jud Braila', '(RO, [Braila] County)'),
      PlatformReach('Jud Prahova', '(RO, [Ploiesti] County)'),
      PlatformReach('Jud Maramures', '(RO, [Baia Mare] County)'),
    ];
  }
}
ListView.builder(
                              padding: EdgeInsets.only(left: 10),
                              scrollDirection: Axis.vertical,
                              shrinkWrap: true,
                              itemCount: _platformReach.length,
                              itemBuilder: (BuildContext ctxt, int Index) {
                                return new Text('Galati');
                              }),

Upvotes: 0

Views: 247

Answers (1)

Mert Toptas
Mert Toptas

Reputation: 114

DropdownButton first you should use this. then you should wrap this up with a Listview. After that, you can add items by mapping it. Below is a simple example.

ListView.builder(
                itemCount: 20,
                itemExtent: 50.0,
                itemBuilder: (BuildContext context, int index) {
                    for (int i = 0; i < 20; i++) {
                    selectedItemValue.add("NONE");
                    }
                    return DropdownButton(
                    value: selectedItemValue[index].toString(),
                    items: _dropDownItem(),
                    onChanged: (value) {
                        selectedItemValue[index] = value;
                        setState(() {});
                    },
                    hint: Text('Select drowdown'),
                    );
                }),
            )
        ],
        ));



 List<DropdownMenuItem<String>> _dropDownItem() {
    List<String> ddl = ['Jud Galati', '(RO, [Galati] County)'];
    return ddl
        .map((value) => DropdownMenuItem(
                value: value,
                child: Text(value),
            ))
        .toList();
    }

Upvotes: 1

Related Questions