【问题标题】:In Flutter Unable to change the state of DropDown Menu in ListView在 Flutter 中无法改变 ListView 中 DropDown Menu 的状态
【发布时间】:2020-12-27 21:18:30
【问题描述】:

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

应用功能

  • 最初是一个空白屏幕
  • 当我点击添加时,它将添加下拉菜单和文本字段
  • 同样,我们可以添加很多。这些小部件将被添加到 _mypets 列表中
  • 当我点击保存时,我正在打印一个列表数组

如何更改状态?

  • 这是一个有状态的小部件

请帮我解决这个问题

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);
}

【问题讨论】:

    标签: flutter listview drop-down-menu


    【解决方案1】:

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

    这可能对你有帮助

    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)],
                ),
              ),
            ],
          ),
        );
      }
    }
    

    【讨论】:

    • 谢谢你工作得很好,但我正在关注这个链接'stackoverflow.com/questions/55596251/…'我想在点击保存按钮时有文本字段和下拉菜单,它应该得到数据列表。我不知道这样做。请指导我
    猜你喜欢
    • 2020-05-11
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多