|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:popup_menu/popup_menu.dart'; |
| 3 | + |
| 4 | +/// |
| 5 | +/// MainPopupPage |
| 6 | +class MainPopupPage extends StatefulWidget { |
| 7 | + @override |
| 8 | + State<StatefulWidget> createState() { |
| 9 | + return _MainPopupState(); |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +/// |
| 14 | +/// _MainPopupState |
| 15 | +class _MainPopupState extends State<MainPopupPage> { |
| 16 | + GlobalKey btnKey = GlobalKey(); |
| 17 | + GlobalKey btnKey2 = GlobalKey(); |
| 18 | + GlobalKey btnKey3 = GlobalKey(); |
| 19 | + |
| 20 | + GlobalKey btnKey1Right = GlobalKey(); |
| 21 | + GlobalKey btnKey2Right = GlobalKey(); |
| 22 | + GlobalKey btnKey3Right = GlobalKey(); |
| 23 | + |
| 24 | + @override |
| 25 | + Widget build(BuildContext context) { |
| 26 | + PopupMenu.context = context; |
| 27 | + |
| 28 | + return Scaffold( |
| 29 | + appBar: AppBar( |
| 30 | + title: Text("Popup"), |
| 31 | + ), |
| 32 | + body: Row( |
| 33 | + children: <Widget>[ |
| 34 | + Spacer(), |
| 35 | + Column(children: <Widget>[ |
| 36 | + Spacer(), |
| 37 | + RaisedButton.icon( |
| 38 | + key: btnKey, |
| 39 | + onPressed: () { |
| 40 | + _createMenu(btnKey); |
| 41 | + }, |
| 42 | + icon: Icon(Icons.folder_open), |
| 43 | + label: Text("left top"), |
| 44 | + ), |
| 45 | + Spacer(), |
| 46 | + RaisedButton.icon( |
| 47 | + key: btnKey2, |
| 48 | + onPressed: () { |
| 49 | + _createMenu(btnKey2); |
| 50 | + }, |
| 51 | + icon: Icon(Icons.folder_open), |
| 52 | + label: Text("left center"), |
| 53 | + ), |
| 54 | + Spacer(), |
| 55 | + RaisedButton.icon( |
| 56 | + key: btnKey3, |
| 57 | + onPressed: () { |
| 58 | + _createMenu(btnKey3); |
| 59 | + }, |
| 60 | + icon: Icon(Icons.folder_open), |
| 61 | + label: Text("left bottom"), |
| 62 | + ), |
| 63 | + Spacer(), |
| 64 | + ]), |
| 65 | + Spacer(), |
| 66 | + Spacer(), |
| 67 | + Column(children: <Widget>[ |
| 68 | + Spacer(), |
| 69 | + RaisedButton.icon( |
| 70 | + key: btnKey1Right, |
| 71 | + onPressed: () { |
| 72 | + _createMenu(btnKey1Right); |
| 73 | + }, |
| 74 | + icon: Icon(Icons.folder_open), |
| 75 | + label: Text("right top"), |
| 76 | + ), |
| 77 | + Spacer(), |
| 78 | + RaisedButton.icon( |
| 79 | + key: btnKey2Right, |
| 80 | + onPressed: () { |
| 81 | + _createMenu(btnKey2Right); |
| 82 | + }, |
| 83 | + icon: Icon(Icons.folder_open), |
| 84 | + label: Text("right center"), |
| 85 | + ), |
| 86 | + Spacer(), |
| 87 | + RaisedButton.icon( |
| 88 | + key: btnKey3Right, |
| 89 | + onPressed: () { |
| 90 | + _createMenu(btnKey3Right); |
| 91 | + }, |
| 92 | + icon: Icon(Icons.folder_open), |
| 93 | + label: Text("right bottom"), |
| 94 | + ), |
| 95 | + Spacer(), |
| 96 | + ]), |
| 97 | + Spacer(), |
| 98 | + ], |
| 99 | + ), |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + _createMenu(GlobalKey globalKey) { |
| 104 | + PopupMenu menu = PopupMenu( |
| 105 | + backgroundColor: Colors.teal, |
| 106 | + lineColor: Colors.tealAccent, |
| 107 | + maxColumn: 3, |
| 108 | + items: [ |
| 109 | + MenuItem(title: 'Copy', image: Image.asset('assets/avatar.jpg')), |
| 110 | + MenuItem( |
| 111 | + title: 'Home', |
| 112 | + textStyle: TextStyle(fontSize: 10.0, color: Colors.tealAccent), |
| 113 | + image: Icon( |
| 114 | + Icons.home, |
| 115 | + color: Colors.white, |
| 116 | + )), |
| 117 | + MenuItem( |
| 118 | + title: 'Mail', |
| 119 | + image: Icon( |
| 120 | + Icons.mail, |
| 121 | + color: Colors.white, |
| 122 | + )), |
| 123 | + MenuItem( |
| 124 | + title: 'Power', |
| 125 | + image: Icon( |
| 126 | + Icons.power, |
| 127 | + color: Colors.white, |
| 128 | + )), |
| 129 | + MenuItem( |
| 130 | + title: 'Setting', |
| 131 | + image: Icon( |
| 132 | + Icons.settings, |
| 133 | + color: Colors.white, |
| 134 | + )), |
| 135 | + MenuItem( |
| 136 | + title: 'PopupMenu', |
| 137 | + image: Icon( |
| 138 | + Icons.menu, |
| 139 | + color: Colors.white, |
| 140 | + )) |
| 141 | + ], |
| 142 | + onClickMenu: onClickMenu, |
| 143 | + onDismiss: onDismiss); |
| 144 | + menu.show(widgetKey: globalKey); |
| 145 | + } |
| 146 | + |
| 147 | + void onClickMenu(MenuItemProvider item) { |
| 148 | + print('Click menu -> ${item.menuTitle}'); |
| 149 | + } |
| 150 | + |
| 151 | + void onDismiss() { |
| 152 | + print('Menu is dismiss'); |
| 153 | + } |
| 154 | + |
| 155 | + void stateChanged(bool isShow) { |
| 156 | + print('menu is ${isShow ? 'showing' : 'closed'}'); |
| 157 | + } |
| 158 | +} |
0 commit comments