在 Flutter 中无法更改 ListView 中下拉菜单的状态

In Flutter Unable to change the state of DropDown Menu in ListView

我正在尝试使用 setState 更改 DropDown 的状态,值正在更改但它没有反映在 UI 上,它仅在我添加新的小部件时反映在新的小部件上。

应用功能

如何更改状态?

请帮我解决这个问题

class _MyPetNameState extends State<MyPetName> {
  var locationArray = [];
  var _myPets = List<Widget>();
  String sampleData = 'Hello';
  int _index = 1;
  var dataForm;
  String partnerName;

  List<_dropListItem> _weekItems = [
    _dropListItem(1, "Pet Type 1"),
    _dropListItem(2, "Pet Type 2"),
    _dropListItem(3, "Pet Type 3"),
    _dropListItem(3, "Pet Type 4"),
  ];

  List<DropdownMenuItem<_dropListItem>> _weekMenuItems;
  _dropListItem _selectedWeekItem;

  List<DropdownMenuItem<_dropListItem>> buildDropDownMenuItems(List listItems) {
    List<DropdownMenuItem<_dropListItem>> items = List();
    for (_dropListItem listItem in listItems) {
      items.add(
        DropdownMenuItem(
          child: Text(listItem.name),
          value: listItem,
        ),
      );
    }
    return items;
  }

  void _addLocation() {
    Map<String, String> _formData= {};
    int keyValue = _index;
    _myPets = List.from(_myPets)
      ..add(Column(
        key: Key("${keyValue}"),
        children: <Widget>[
          Container(
            padding: EdgeInsets.all(20.0),
            child: DropdownButton<_dropListItem>(
                value: _selectedWeekItem,
                items: _weekMenuItems,
                onChanged: (value) {
                  _formData['location'] = value.name;
                  setState(() {
                    _weekMenuItems = buildDropDownMenuItems(_weekItems);
                    _selectedWeekItem = value;
                  });
                }),
          ),
          Container(
            child: TextFormField(
              initialValue: '',
              onChanged: (val) {
                _formData['locationType'] = val;
                setState(() {
                  sampleData = val;
                });
              },
            ),
          ),
        ],
      ));
    setState(() => ++_index);
    locationArray.add(_formData);
  }


  void _sub(int _deleteIndex){
    setState(() {
      _myPets = List.of(_myPets)..removeAt(_deleteIndex - 1);
      --_index;
    });
  }


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _weekMenuItems = buildDropDownMenuItems(_weekItems);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          print('');
          print(locationArray);
          setState(() {
            dataForm = [];
          });

        },
        child: Text('Save'),
      ),
      appBar: AppBar(
        title: Text('Add your pets'),
        actions: <Widget>[
          FlatButton(
            child: Text('Add'),
            onPressed: (){
              _addLocation();

            },
          ),
        ],
      ),
      body: Column(
        children: [
          Expanded(
            child: ListView(
              children: _myPets,
            ),
          ),
        ],
      ),
    );
  }
}

class _dropListItem {
  int value;
  String name;

  _dropListItem(this.value, this.name);
}

我对你的代码做了一些修改

这可能对您有所帮助

class _MyPetNameState extends State<MyPetName> {
  List<dynamic> radioval = [];
    
  setSelectedRadio(int val, int idx) {
    setState(() {
      radioval[idx]["value"] = val;
    });
  }

  Widget _radioui(int keyValue) {
    return Column(
      children: <Widget>[
        ButtonBar(
          alignment: MainAxisAlignment.center,
          children: <Widget>[
            Radio(
              value: 0,
              groupValue: radioval[keyValue]["value"],
              activeColor: Colors.green,
              onChanged: (val) {
                print("Radio $val");
                setSelectedRadio(val, keyValue);
              },
            ),
            Radio(
              value: 1,
              groupValue: radioval[keyValue]["value"],
              activeColor: Colors.blue,
              onChanged: (val) {
                print("Radio $val");
                setSelectedRadio(val, keyValue);
              },
            ),
          ],
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            radioval.add({"name": "pet1", "value": 0});
          });
        },
        child: Icon(Icons.add),
      ),
      appBar: AppBar(
        title: Text('Add your pets'),
      ),
      body: Column(
        children: [
          Expanded(
            child: ListView(
              children: [for (int i = 0; i < radioval.length; i++) _radioui(i)],
            ),
          ),
        ],
      ),
    );
  }
}