Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Last active January 2, 2024 05:53
Show Gist options
  • Save slightfoot/f0b753606c97d8a2c06659803c12d858 to your computer and use it in GitHub Desktop.
Save slightfoot/f0b753606c97d8a2c06659803c12d858 to your computer and use it in GitHub Desktop.
Flutter Drop List Example with TextField
import 'package:flutter/material.dart';
void main()
{
final TextEditingController _controller = new TextEditingController();
var items = ['Working a lot harder', 'Being a lot smarter', 'Being a self-starter', 'Placed in charge of trading charter'];
runApp(
new MaterialApp(
title: 'Drop List Example',
home: new Scaffold(
appBar: new AppBar(title: const Text('Drop List Example')),
body: new Center(
child: new Container(
child: new Column(
children: [
new Padding(
padding: const EdgeInsets.all(24.0),
child: new Row(
children: <Widget>[
new Expanded(
child: new TextField(controller: _controller)
),
new PopupMenuButton<String>(
icon: const Icon(Icons.arrow_drop_down),
onSelected: (String value) {
_controller.text = value;
},
itemBuilder: (BuildContext context) {
return items.map<PopupMenuItem<String>>((String value) {
return new PopupMenuItem(child: new Text(value), value: value);
}).toList();
},
),
],
),
),
],
),
),
),
),
),
);
}
@Jules369-ZM
Copy link

Thanks, Have managed to display data.
Now how do i increase the width of the PopUpMenuItem

@NiranjanShah
Copy link

can we make focus action detector in Popumenu item, basically what we want is, a user types in the text field and the list in the popupmenuitem filters the list, also in case , user wants to scroll/select using arrow keys, he/she can do it.

Quick assistance is appreciated....

@prektrons
Copy link

Thanks for sharing the code. how to move dropdown icon inside TextField?

@vickyparanjothi use the decoration property of TextField, and the suffixIcon property of InputDecoration:

TextField(
  controller: _controller,
  decoration: InputDecoration(
    suffixIcon: PopupMenuButton<String>(
      icon: const Icon(Icons.arrow_drop_down),
      onSelected: (String value) {
        _controller.text = value;
      },
      itemBuilder: (BuildContext context) {
        return items
            .map<PopupMenuItem<String>>((String value) {
          return new PopupMenuItem(
              child: new Text(value), value: value);
        }).toList();
      },
    ),
  ),
)

I have created a Textfield in similar way, But the data is not getting stored in firestore(firebase)

@augustmuir
Copy link

augustmuir commented Jun 16, 2021

You should put this on pub.dev. The packages which do the same thing there are trash and their sizes are all fixed. Thanks

@NiranjanShah
Copy link

can we make focus action detector in Popumenu item, basically what we want is, a user types in the text field and the list in the popupmenuitem filters the list, also in case , user wants to scroll/select using arrow keys, he/she can do it.

Quick assistance is appreciated....

can any one assit

@officialismailshah
Copy link

Thanks for sharing the code. how to move dropdown icon inside TextField?

@vickyparanjothi use the decoration property of TextField, and the suffixIcon property of InputDecoration:

TextField(
  controller: _controller,
  decoration: InputDecoration(
    suffixIcon: PopupMenuButton<String>(
      icon: const Icon(Icons.arrow_drop_down),
      onSelected: (String value) {
        _controller.text = value;
      },
      itemBuilder: (BuildContext context) {
        return items
            .map<PopupMenuItem<String>>((String value) {
          return new PopupMenuItem(
              child: new Text(value), value: value);
        }).toList();
      },
    ),
  ),
)

thanks bro it worked for me too...😊😊😊

@rahi0
Copy link

rahi0 commented Sep 24, 2021

I want to show a popup list right below a textfield. How can I trigger popup items using textfield "onchange" property? Can anyone assist?

@officialismailshah
Copy link

officialismailshah commented Sep 25, 2021 via email

@Jules369-ZM
Copy link

Jules369-ZM commented Oct 1, 2021 via email

@Asadbek-IP
Copy link

thank you

@alexlozdev
Copy link

How can make the width of PopupMenuItem full like the dropdownlist?

@CoffeeAddicts
Copy link

How can make the width of PopupMenuItem full like the dropdownlist?

Don't know if you still need this, but I'm putting it here because maybe someone will need it:
Inside the PopupMenuButton Widget you can add BoxConstraints which can have the minHeight or minWeight set to whatever double value you need.

So Pop Up Menu with a set width of 200:

new PopupMenuButton<String>(
          icon: const Icon(Icons.arrow_drop_down),
          onSelected: (String value) {
            _controller.text = value;
         },
         constraints: BoxConstraints(minWidth: 200),
         itemBuilder: (BuildContext context) {
             return items.map<PopupMenuItem<String>>((String value) {
                return new PopupMenuItem(child: new Text(value), value: value);
         }).toList();
    },
),

Or if you want the PopUpMenu to take up all available space just use double.inifity for example:

new PopupMenuButton<String>(
          icon: const Icon(Icons.arrow_drop_down),
          onSelected: (String value) {
            _controller.text = value;
         },
         constraints: BoxConstraints(minWidth: double.inifity ),
         itemBuilder: (BuildContext context) {
             return items.map<PopupMenuItem<String>>((String value) {
                return new PopupMenuItem(child: new Text(value), value: value);
         }).toList();
    },
),

@MarioCarlosChita
Copy link

MarioCarlosChita commented Oct 16, 2023

@CoffeeAddicts I also put readOnly to true , because without it , you may replace the value by rewriting the value

TextField(
controller: _controller,
decoration: InputDecoration(
suffixIcon: PopupMenuButton(
icon: const Icon(Icons.arrow_drop_down),
onSelected: (String value) {
_controller.text = value;
},
itemBuilder: (BuildContext context) {
return items
.map<PopupMenuItem>((String value) {
return new PopupMenuItem(
child: new Text(value), value: value);
}).toList();
},
),
),
)

@ahmeedev
Copy link

ahmeedev commented Jan 2, 2024

\

Thanks for sharing the code. how to move dropdown icon inside TextField?

@vickyparanjothi use the decoration property of TextField, and the suffixIcon property of InputDecoration:

TextField(
  controller: _controller,
  decoration: InputDecoration(
    suffixIcon: PopupMenuButton<String>(
      icon: const Icon(Icons.arrow_drop_down),
      onSelected: (String value) {
        _controller.text = value;
      },
      itemBuilder: (BuildContext context) {
        return items
            .map<PopupMenuItem<String>>((String value) {
          return new PopupMenuItem(
              child: new Text(value), value: value);
        }).toList();
      },
    ),
  ),
)

works like a charm bro!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment