how to show list of dropdown from botton in flutter

110

Try below code hope its helpful to you . you must used dropdown_below from here

Create your list

List genderList = [
    {'no': 1, 'gender': 'Male'},
    {'no': 2, 'gender': 'Female'},
    {'no': 3, 'gender': 'Other'}
  ];

One varibale and list our value

List<DropdownMenuItem<Object?>> _dropdownTestItems = [];
  var selectedGender;

Create initState() and dispose() method:

  @override
  void initState() {
    _dropdownTestItems = buildDropdownTestItems(genderList);
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

Add your selected gender value in dropdown

  List<DropdownMenuItem<Object?>> buildDropdownTestItems(List genderList) {
    List<DropdownMenuItem<Object?>> items = [];
    for (var i in genderList) {
      items.add(
        DropdownMenuItem(
          value: i,
          child: Text(
            i['gender'],
            style: TextStyle(color: Colors.black),
          ),
        ),
      );
    }
    return items;
  }

Your Widget:

 Padding(
                padding: const EdgeInsets.all(8.0),
                child: DropdownBelow(
                  itemWidth: 100,
                  itemTextstyle: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.w400,
                      color: Colors.black),
                  boxTextstyle: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.w400,
                      color: Colors.white54),
                  boxPadding: EdgeInsets.fromLTRB(13, 12, 13, 12),
                  boxWidth: 100,
                  boxHeight: 45,
                  boxDecoration: BoxDecoration(
                    color: Colors.transparent,
                    border: Border.all(width: 1, color: Colors.black,),
                  ),
                  icon: Icon(
                    Icons.arrow_downward,
                    color: Colors.black,
                  ),
                  hint: Text(
                    'Gender',
                    style: TextStyle(
                      color: Colors.black,
                    ),
                  ),
                  value: selectedGender,
                  items: _dropdownTestItems,
                  onChanged: (selectedTest) {
                    setState(() {
                      selectedGender = selectedTest;
                    });
                  },
                ),
              ),

Your result screen-> enter image description here

Share:
110
TimeToCode
Author by

TimeToCode

I'm a motivated programmer, can do anything with passion. Currently working on flutter.

Updated on January 01, 2023

Comments

  • TimeToCode
    TimeToCode over 1 year

    I want to start the list of dropdown from its bottom, here my dropdown code

    Container(
                                  height: 55.0,
                                  width: MediaQuery.of(context).size.width *
                                      0.43,
                                  child: DropdownButtonFormField<String>(
                                    decoration: InputDecoration(
                                      errorText: _validatedrop
                                          ? 'This is the required field'
                                          : null,
                                          focusedBorder: OutlineInputBorder(borderSide: BorderSide(color: Color(int.parse(textfieldBorderColor)))),
                                      border: OutlineInputBorder(
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(10.0))),
                                      errorBorder: OutlineInputBorder(
                                          borderSide: BorderSide(
                                              color: Colors.red, width: 5)),
                                    ),
                                    items: genderlist
                                        .map<DropdownMenuItem<String>>(
                                            (String value) {
                                      return DropdownMenuItem<String>(
                                     
                                          value: value, child: Text(value));
                                    }).toList(),
                                    hint: Text(
                                      "Male",
                                      style: GoogleFonts.montserrat(
                                          color: Color(
                                              int.parse(hinttextColor))),
                                    ),
                                    onChanged: (value) async {
                                      setState(() {
                                        this.selectedGender = value!;
    
                                        if (this.selectedGender == "") {
                                          _validatedrop = true;
                                          
                                        } else {
                                          _validatedrop = false;
                                        }
                                      });
                                    },
                                  )),
    
    

    its output look like this

    enter image description here

    and i want its output like this, it should start from below

    enter image description here

    Please help if anyone know how to do this.

    • Wali Khan
      Wali Khan over 2 years
      You mean that dialog should flow upwards ?
    • TimeToCode
      TimeToCode over 2 years
      no, downwards! i post the required output image, please check
  • TimeToCode
    TimeToCode over 2 years
    hey bro, i am getting this error 'package:dropdown_below/dropdown_below.dart': Failed assertion: line 421 pos 16: 'value == null || items .where((DropdownMenuItem<T> item) => item.value == value) .length == 1': is not true.
  • Ravindra S. Patil
    Ravindra S. Patil over 2 years
    can you add your code in your question?
  • TimeToCode
    TimeToCode over 2 years
    i resolved it, thankyou!!
  • Ravindra S. Patil
    Ravindra S. Patil over 2 years
    Glad to help you.