从DropdownButton显示正确类别的文本字段

问题描述 投票:0回答:1

仅当在我的下拉按钮中选择一个选项时,如何显示我的窗口小部件?

我尝试这个:

  showWidgetOnCategory() async {
    if(_selectedCategory == 'FREE'){
       _buildCountPerson();
    }
  }

  Widget _dropdownCategory(){
    return Padding(
      padding: EdgeInsets.only(left:0), 
      child: DropdownButton(
        isExpanded: true,
        hint: Text('Type', style: TextStyle(color: Colors.grey.shade300),),
        value: _selectedCategory,

        onChanged: (newValue){
          setState(() async {
            _selectedCategory = newValue;
          });
        },
        items: _categories.map((category){
          return DropdownMenuItem(
            child: new Text(category, style: TextStyle(color: Colors.black),),
            value: category,
          );
        }).toList(),
      )
    );
  }


  _buildCountPerson(){
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    return Container(
      child: Row(
        children: <Widget>[
              Padding(
                padding: EdgeInsets.only(right: 15),
                child: Text('Liczba miejsc:', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              )),
              SizedBox(
                width: 120,
                height: 50,
                child: TextFormField(
                  keyboardType: TextInputType.number,
                  controller: _countPersonOnEventController,
                  decoration: InputDecoration(
                    hintText: '0',
                        border: new OutlineInputBorder(
                        borderSide: new BorderSide(color: Colors.teal)
                      ),
                  ),
                  validator: (String value) {
                      if(value.isEmpty){
                        return 'Pole wymagane';
                      }else return null;
                    },
                    onSaved: (String value) {
                      _count = int.parse(value);
                      print(_count);
                    },
                  )
              )

        ],
      ));
  }

但不起作用,有谁知道如何帮助我吗?

///////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ////////////////////////////////////////

flutter dart
1个回答
1
投票

最初可以使用boolean droDownSelected = false,然后在DropDown的onChanged方法中将其设置为true,以便可以显示其他小部件。您可以通过以下方式按条件显示小部件。

bool droDownSelected = false;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        DropdownButton(), // in onChanged method setState droDownSelected to true so below widget is shown
        droDownSelected ? WidgetYouWantToShow() : SizedBox(),
      ],
    );
  }
© www.soinside.com 2019 - 2024. All rights reserved.