skip to Main Content

I am trying to create a dropdown list in flutter with ability to type in it and also display elements matching the input.

I tried dropdown menu widget in flutter which had a enableSearch property but I could not style the textfield .
I tried the menuStyle option but only the dropdown menu could be styled not the dropdown box.

2

Answers


  1. Try With this package dropdown_textfield:

    class DropDownButton extends StatefulWidget {
      const DropDownButton({Key? key}) : super(key: key);
    
      @override
      State<DropDownButton> createState() => _DropDownButtonState();
    }
    
    class _DropDownButtonState extends State<DropDownButton> {
      final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
      FocusNode searchFocusNode = FocusNode();
      FocusNode textFieldFocusNode = FocusNode();
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: SingleChildScrollView(
            child: Form(
              key: _formKey,
              child: Center(
                child: Padding(
                  padding: const EdgeInsets.all(20),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      const SizedBox(
                        height: 70,
                      ),
                      const Text(
                        "Dropdown with search Textfield",
                        style: TextStyle(fontWeight: FontWeight.bold),
                      ),
                      const SizedBox(
                        height: 20,
                      ),
                      DropDownTextField(
                        clearOption: false,
                        textFieldFocusNode: textFieldFocusNode,
                        searchFocusNode: searchFocusNode,
                        searchAutofocus: true,
                        dropDownItemCount: 8,
                        searchShowCursor: false,
                        enableSearch: true,
                        searchKeyboardType: TextInputType.number,
                        dropDownList: const [
                          DropDownValueModel(name: 'Flutter', value: "flutter"),
                          DropDownValueModel(name: 'Java', value: "java",),
                          DropDownValueModel(name: 'HTML', value: "html"),
                          DropDownValueModel(name: 'CSS', value: "css",),
                          DropDownValueModel(name: 'PHP', value: "php"),
                        ],
                        onChanged: (val) {},
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        );
      }
    }
    

    Output:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search