在颤振中选择项目下拉时添加输入文本字段

add inputt textfield when selected item dropdown in flutter

当我在 flutter 中选择项目 'other' 下拉列表时,是否可以添加小部件输入文本字段? 这是用于 flutter mobile android

我的代码

List <String> klasifikasi = [
'Fatality',
'Lainnya'];

DropdownButton<String>(
                  focusColor:Colors.white,
                  value: _chosenValue,
                  //elevation: 5,
                  style: TextStyle(color: Colors.white),
                  iconEnabledColor:Colors.blue,
                  items: klasifikasi.map<DropdownMenuItem<String>>((String value) {
                    return DropdownMenuItem<String>(
                      value: value,
                      child: Text(value,style:TextStyle(color:Colors.black),),
                    );
                  }).toList(),
                  hint:Text(
                    "Klasifikasi Insiden",
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: 14,
                        fontWeight: FontWeight.w400),
                  ),

                  onChanged: (String value) {
                    setState(() {

                      _chosenValue = value;
                      if (_chosenValue == klasifikasi){
                        return Scaffold(
                          appBar: AppBar(),
                          body: Center(
                            child: Container(
                              color: Colors.grey,
                               child: _buildTextField(
                                  labelText: 'Lainnya',
                                  controller: _lainCtrl,
                                ),
                            ),
                          ),

                        );

                      }
                    });
                  },
                ),

当我选择 'lainnya' 时显示文本字段以输入值

将下拉按钮放在一列中,

bool addtextfield = false;
if (_chosenValue == klasifikasi){
    
    setState((){

      addtextfield = true;
    });
}
addtextfield == true?
//Show ur input field
:Container(),

也许你可以试试这个,

bool _showTextField = false;
    Column(
      children: [
        DropdownButton<String>(
            focusColor: Colors.white,
            value: _chosenValue,
            //elevation: 5,
            style: TextStyle(color: Colors.white),
            iconEnabledColor: Colors.blue,
            items: klasifikasi.map<DropdownMenuItem<String>>((String value) {
              return DropdownMenuItem<String>(
                value: value,
                child: Text(
                  value,
                  style: TextStyle(color: Colors.black),
                ),
              );
            }).toList(),
            hint: Text(
              "Klasifikasi Insiden",
              style: TextStyle(
                  color: Colors.black,
                  fontSize: 14,
                  fontWeight: FontWeight.w400),
            ),
            onChanged: (String value) {
              setState(() {
                _chosenValue = value;
                if (_chosenValue == klasifikasi.last) {
                  _showTextField = true;
                } else {
                  _showTextField = false;
                }
              });
            }),
        Visibility(
          visible: _showTextField,
          child: //Your textfield here,
        ),
      ],
    );

希望对你有所帮助^_^