0

is it possible to add widget input text field when I selected item 'other' dropdown in flutter? this is for flutter mobile android

enter image description here

my code

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,
                                ),
                            ),
                          ),

                        );

                      }
                    });
                  },
                ),

when I selected 'lainnya' showing textfield to input value

1
  • I am confused why you are returning a scaffold inside a set state method. Where is that even being returned to? This is very strange way of coding it. It would be much easier if you Just layout the whole screen the use an inline if statement to hide /show the text input field Commented May 20, 2021 at 6:33

2 Answers 2

2

Maybe you can try this,

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

i hope that will be help you ^_^

Sign up to request clarification or add additional context in comments.

Comments

1

Put you dropdown button in a column,

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

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

2 Comments

is this can use for i selected string 'Lainnya" ? or this method for all string in dropdown items?
i try if (_chosenValue == klasifikasi[9]){ setState((){ addtextfield = true; }); } addtextfield == true ? _buildTextField( labelText: 'Lainnya', controller: _lainCtrl,) : Container(); its not showing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.