Home > Mobile >  How to add required validation for drop down in flutter?
How to add required validation for drop down in flutter?

Time:10-01

Please refer below code. I have to add required validation here.
Container( height: MediaQuery.of(context).size.height * 0.07, width: MediaQuery.of(context).size.width * 0.4, child: InputDecorator( decoration: InputDecoration( errorStyle: const TextStyle(fontSize: 0.05),

                    //  fillColor: Colors.grey[10],
                    border: OutlineInputBorder(
                        borderSide: BorderSide(
                      color: Colors.black,
                      // width: 10
                    )),
                  ),
                  // child: Padding(
                  //   padding: EdgeInsets.only(top: 4),
                  child: DropdownButtonHideUnderline(
                    child: DropdownButton2(
                      hint: Text(
                        'Select Document Status',
                        style: TextStyle(
                          fontSize: 8,
                          //color: Theme.of(context).hintColor,
                        ),
                      ),
                      items: items1
                          .map((item) => DropdownMenuItem<String>(
                                value: item,
                                child: Text(
                                  item,
                                  style: const TextStyle(
                                    fontSize: 10,
                                  ),
                                ),
                              ))
                          .toList(),
                      value: selectedItem,
                      onChanged: (value) {
                        setState(() {
                          selectedItem = value as String;
                        });
                      },
                      //isExpanded: true,
                      buttonHeight: 20,
                      buttonWidth: 150,
                      itemHeight: 40,
                      dropdownWidth: 200,
                    ),
                  ),
                ),
              ),

CodePudding user response:

You use third party dependency that's why you can't put validation on it. You can use in-built widget of Flutter named DropdownButtonFormField. In this widget you will get property of validator.

Official doc: Flutter Doc

Example: Sample example

CodePudding user response:

you need to use "DropdownButtonFormField2" which have a validator.

DropdownButtonFormField2(
  validator: (value){
   ///---check
  },
)
  • Related