Skip to content

Instantly share code, notes, and snippets.

@iamEtornam
Last active October 31, 2020 18:16
Show Gist options
  • Save iamEtornam/46bc85f47021a3c4d38417c038c2fe1c to your computer and use it in GitHub Desktop.
Save iamEtornam/46bc85f47021a3c4d38417c038c2fe1c to your computer and use it in GitHub Desktop.
sample code to create Android-like dropdown menu in flutter
import 'package:flutter/material.dart';
class CustomPopupMenu {
CustomPopupMenu({this.title, this.icon});
String title;
IconData icon;
}
//initialise menus
List<CustomPopupMenu> choices = <CustomPopupMenu>[
CustomPopupMenu(title: 'Home', icon: Icons.home),
CustomPopupMenu(title: 'Profile', icon: Icons.person),
CustomPopupMenu(title: 'Notifications', icon: Icons.notification),
CustomPopupMenu(title: 'Settings', icon: Icons.settings),
];
//handle onclick on custom menu items
void _select(CustomPopupMenu choice) {
int choicePosition = choices.indexOf(choice);
switch (choicePosition) {
case 0:
print('Home');
//to home page
break;
case 1:
print('profile');
//to profile page
break;
case 2:
print('notification');
//to notification page
break;
case 3:
print('settings');
//to seetings page
break;
}
}
//widget for custom dropdown menu
PopupMenuButton<CustomPopupMenu>(
elevation: 2,
icon: Icon(Icons.menu),
onCanceled: () {
print('You have not chossed anything');
},
tooltip: 'popup menu',
onSelected: _select,
itemBuilder: (BuildContext context) {
return choices.map((CustomPopupMenu choice) {
return PopupMenuItem<CustomPopupMenu>(
value: choice,
child: Row(
children: <Widget>[
Icon(choice.icon),
SizedBox(
width: 8,
),
Text(choice.title),
],
),
);
}).toList();
},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment