【问题标题】:Create a state, city drop down, based on selected state display the value of city drop down?创建一个州,城市下拉,根据选择的州显示城市下拉的值?
【发布时间】:2020-02-18 09:06:40
【问题描述】:

创建一个状态,城市下拉菜单,根据所选状态,API 请求将触发并将响应显示为城市下拉菜单的值,对此有什么想法吗?

【问题讨论】:

  • 发布一些代码,以便我们为您提供帮助
  • 我需要多个相互依赖的下拉列表,一旦我更改状态,Json 数据将显示在城市下拉列表中,这是我的想法,我现在没有工作代码。

标签: android ios flutter mobile


【解决方案1】:

您可以通过以下方式使用 Dropdown 来达到您的预期。

import 'package:flutter/material.dart';

class DropDown extends StatefulWidget {
  @override
  _DropDownState createState() => _DropDownState();
}

class _DropDownState extends State<DropDown> {


  Map<String, String> stateCity = {
    "Surat": "Gujarat",
    "Ahmadabad": "Gujarat",
    "Vadodara": "Gujarat",
    "Mp1": "Mp",
    "Mp2": "Mp",
    "Up1": "Up",
    "Up2": "Up",
    "Up3": "Up",
  };

  List<String> state = ['Gujarat','Mp','Up'];
  List<String>  city= [];

  String selectedState;
  String selectedcity;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              DropdownButton(
                onChanged: (value) {
                  setState(() {
                    selectedState = value;
                    selectedcity =null;
                    city.clear();
                    stateCity.forEach((k,v){
                      print(k);
                      if(selectedState==v){
                        city.add(k);
                      }
                     });
                  });
                },
                value: selectedState,
                items: state
                    .map((state) => DropdownMenuItem(
                          child: Text(state),
                          value: state,
                        ))
                    .toList(),
              ),
              DropdownButton(
                onChanged: (value) {
                  setState(() {
                    selectedcity = value;
                  });
                  print(value);
                },
                value: selectedcity,
                items: selectedState !=  null ? city.map((city) => DropdownMenuItem(child: Text(city),value: city,)).toList(): [],
              )
            ],
          ),
        ),
      ),
    );
  }
}

【讨论】:

    猜你喜欢
    • 2017-08-27
    • 2011-01-07
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 2013-06-28
    相关资源
    最近更新 更多