Want to open a PopupMenuButton in Flutter? You can provide a child to PopupMenuButton which can be any Widget including a ListTile.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class _MyHomePageState extends State<MyHomePage> { final GlobalKey _menuKey = new GlobalKey(); @override Widget build(BuildContext context) { final button = new PopupMenuButton( key: _menuKey, itemBuilder: (_) => <PopupMenuItem<String>>[ new PopupMenuItem<String>( child: const Text('Doge'), value: 'Doge'), new PopupMenuItem<String>( child: const Text('Lion'), value: 'Lion'), ], onSelected: (_) {}); final tile = new ListTile(title: new Text('Doge or lion?'), trailing: button, onTap: () { // This is a hack because _PopupMenuButtonState is private. dynamic state = _menuKey.currentState; state.showButtonMenu(); }); return new Scaffold( body: new Center( child: tile, ), ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.