Как добавить выпадающий список разных изображений с текстом

#flutter #dart #drop-down-menu #flutter-layout

#flutter #dart #выпадающее меню #flutter-макет

Вопрос:

внутри объекта column как мы можем обрабатывать добавление изображений, связанных с другим языком текста, в выпадающее меню?

 child: new DropdownButtonFormField(
                    decoration: InputDecoration.collapsed(hintText: ''),
                    items: <String>['Japan', 'China'].map((String value) {
                      return new DropdownMenuItem<String>(
                        value: value,
                        child: Align(
                            alignment: Alignment.centerRight,
                            child: Row(
                              children: [
                                new Image.asset('assets/images/'  value   '.jpg',
                                    width: 75.0, height: 75.0),
                                SizedBox(
                                  width: 25,
                                ),
                                new Text(value),
                              ],
                            )),
                      );
                    }).toList(),
                  ),
  

Комментарии:

1. Решил проблему, добавив класс модели вместо строк в тип карты нашел этот урок очень полезным coderzheaven.com/2019/04/16/dropdown-list-in-flutter

Ответ №1:

Вы должны использовать модель в выпадающем списке.

Например

 class Demo extends StatefulWidget {
  @override
  _DemoState createState() => _DemoState();
}

class _DemoState extends State<Demo> {
  List<CountryData> _countries = CountryData.getCounties();
  List<DropdownMenuItem<CountryData>> _dropdownMenuItems;
  CountryData _selectedCompany;

  @override
  void initState() {
    _dropdownMenuItems = buildDropdownMenuItems(_countries);
    _selectedCompany = _dropdownMenuItems[0].value;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Center(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text("Country"),
              SizedBox(
                height: 20.0,
              ),
              DropdownButton(
                value: _selectedCompany,
                items: _dropdownMenuItems,
                onChanged: onChangeDropdownItem,
              ),
              SizedBox(
                height: 20.0,
              ),
              Text('Selected: ${_selectedCompany.name}'),
            ],
          ),
        ),
      ),
    );
  }

  onChangeDropdownItem(CountryData selectedCompany) {
    setState(() {
      _selectedCompany = selectedCompany;
    });
  }

  List<DropdownMenuItem<CountryData>> buildDropdownMenuItems(List counties) {
    List<DropdownMenuItem<CountryData>> items = List();
    for (CountryData country in counties) {
      items.add(
        DropdownMenuItem(
          value: country,
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Row(
              children: [
                Image.asset(
                  country.images,
                  width: 10,
                  height: 10,
                ),
                SizedBox(
                  width: 10,
                ),
                Text(country.name)
              ],
            ),
          ),
        ),
      );
    }
    return items;
  }

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

class CountryData {
  int id;
  String name;
  String images;

  CountryData(this.id, this.name, this.images);

  static List<CountryData> getCounties() {
    return <CountryData>[
      CountryData(1, 'india', 'assets/images/india.png'),
      CountryData(2, 'australia', 'assets/images/australia.png'),
      CountryData(3, 'chine', 'assets/images/china.png'),
      CountryData(4, 'SU', 'assets/images/south-africa.png'),
      CountryData(5, 'UK', 'assets/images/united-kingdom.png'),
    ];
  }
}
  

Вывод

вывод