Search code examples
flutterdart-null-safety

How can I add label text for dropdown button


I want to add label text for Dropdown Button. How can I do it.....And label text should be resize when we select value from Dropdown button.

Container(
                              width: MediaQuery.of(context).size.width * .71,
                              child: DropdownButton(
                                isExpanded: true,
                                iconSize: 15,
                                underline: Container(
                                    child: Column(
                                  children: [
                                    Divider(
                                        thickness: 1,
                                        color: const Color(0xFFa5a5a5))
                                  ],
                                )),
                                value: dropdownvalue,
                                icon: Icon(Icons.keyboard_arrow_down),
                                items: items.map((String items) {
                                  return DropdownMenuItem(
                                      value: items,
                                      child: Text(
                                        items,
                                        style: TextStyle(
                                          fontSize: 10,
                                          fontWeight: FontWeight.w500,
                                          color: const Color(0xFFa5a5a5),
                                        ),
                                      ));
                                }).toList(),
                                onChanged: (newValue) {
                                  setState(() {
                                    dropdownvalue = newValue.toString();
                                  });
                                },
                              ),
                            ),

enter image description here


Solution

  • please try with Stack

    Container(
            padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
            width: MediaQuery.of(context).size.width * .71,
            child: Stack(
              children: [ 
                Text("Select society and location", style: TextStyle(
                  fontSize: 15,
                  fontWeight: FontWeight.w500,
                  color: Colors.orange,
                ), // adjust your title as you required
               ),
                DropdownButton(
                  isExpanded: true,
                  iconSize: 15,
                    hint: Text(
                      'Select ',
                      style: TextStyle(
                        fontSize: 12, color: const Color(0xFFa5a5a5)
                      ),
                      textAlign: TextAlign.center,
                    ),
                  underline: Container(
                      child: Column(
                        children: [
                          Divider(
                              thickness: 1,
                              color: const Color(0xFFa5a5a5))
                        ],
                      )),
                 // value: dropdownvalue,
                  icon: Icon(Icons.keyboard_arrow_down),
                  items: item.map((String items) {
                    return DropdownMenuItem(
                        value: items,
                        child: Text(
                          items,
                          style: TextStyle(
                            fontSize: 10,
                            fontWeight: FontWeight.w500,
                            color: const Color(0xFFa5a5a5),
                          ),
                        ));
                  }).toList(),
                  onChanged: (newValue) {
                    setState(() {
                        dropdownvalue = newValue.toString();
                    });
                  },
                )
              ],
            ),
          )
    

    Output:

    enter image description here