Skip to content

Commit

Permalink
fix #5
Browse files Browse the repository at this point in the history
  • Loading branch information
chinabrant committed Oct 17, 2019
1 parent 31ff87b commit dc08c07
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 19 deletions.
20 changes: 18 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class _MyHomePageState extends State<MyHomePage> {
PopupMenu menu;
GlobalKey btnKey = GlobalKey();
GlobalKey btnKey2 = GlobalKey();
GlobalKey btnKey3 = GlobalKey();

@override
void initState() {
Expand Down Expand Up @@ -60,7 +61,7 @@ class _MyHomePageState extends State<MyHomePage> {
Icons.menu,
color: Colors.white,
))
], onClickMenu: onClickMenu, onDismiss: onDismiss, maxColumn: 1);
], onClickMenu: onClickMenu, onDismiss: onDismiss, maxColumn: 4);
}

void stateChanged(bool isShow) {
Expand All @@ -85,9 +86,10 @@ class _MyHomePageState extends State<MyHomePage> {
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
color: Colors.blue,
child: MaterialButton(
height: 45.0,
key: btnKey,
Expand All @@ -103,6 +105,14 @@ class _MyHomePageState extends State<MyHomePage> {
child: Text('Show Menu'),
),
),
Container(
child: MaterialButton(
key: btnKey3,
height: 45.0,
onPressed: onDismissOnlyBeCalledOnce,
child: Text('Show Menu'),
),
),
Container(
child: MaterialButton(
height: 30.0,
Expand All @@ -116,7 +126,13 @@ class _MyHomePageState extends State<MyHomePage> {
);
}

void onDismissOnlyBeCalledOnce() {
menu.show(widgetKey: btnKey3);
}

void onGesturesDemo() {
menu.dismiss();
return;
Navigator.push(
context,
MaterialPageRoute(builder: (context) => GestureDemo()),
Expand Down
53 changes: 51 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
archive:
dependency: transitive
description:
name: archive
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.10"
args:
dependency: transitive
description:
name: args
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.5.2"
async:
dependency: transitive
description:
Expand Down Expand Up @@ -29,6 +43,20 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.14.11"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.1"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.3"
cupertino_icons:
dependency: "direct main"
description:
Expand All @@ -46,6 +74,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
image:
dependency: transitive
description:
name: image
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.4"
matcher:
dependency: transitive
description:
Expand Down Expand Up @@ -74,6 +109,13 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.8.0+1"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.4.0"
popup_menu:
dependency: "direct dev"
description:
Expand All @@ -87,7 +129,7 @@ packages:
name: quiver
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.4"
version: "2.0.5"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -149,5 +191,12 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.8"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.5.0"
sdks:
dart: ">=2.2.2 <3.0.0"
dart: ">=2.4.0 <3.0.0"
52 changes: 37 additions & 15 deletions lib/popup_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,42 @@ class PopupMenu {
static var itemHeight = 65.0;
static var arrowHeight = 10.0;
OverlayEntry _entry;
List<MenuItem> items;
int _row; // row count
int _col; // col count
// The left top point of this menu.
List<MenuItemProvider> items;

/// row count
int _row;

/// col count
int _col;

/// The left top point of this menu.
Offset _offset;

/// Menu will show at above or under this rect
Rect _showRect;

/// if false menu is show above of the widget, otherwise menu is show under the widget
bool _isDown = true;

/// The max column count, default is 4.
int _maxColumn;

/// callback
VoidCallback dismissCallback;
MenuClickCallback onClickMenu;
PopupMenuStateChanged stateChanged;
Rect _showRect; // 显示在哪个view的rect
bool _isDown = true; // 是显示在下方还是上方,通过计算得到

/// Cannot be null
static BuildContext context;
// The max column count, default is 4.
int _maxColumn;

/// style
Color _backgroundColor;
Color _highlightColor;
Color _lineColor;

/// It's showing or not.
bool _isShow = false;
bool get isShow => _isShow; // It's showing or not.
bool get isShow => _isShow;

PopupMenu(
{MenuClickCallback onClickMenu,
Expand All @@ -71,7 +88,7 @@ class PopupMenu {
Color highlightColor,
Color lineColor,
PopupMenuStateChanged stateChanged,
List<MenuItem> items}) {
List<MenuItemProvider> items}) {
this.onClickMenu = onClickMenu;
this.dismissCallback = onDismiss;
this.stateChanged = stateChanged;
Expand All @@ -85,7 +102,7 @@ class PopupMenu {
}
}

void show({Rect rect, GlobalKey widgetKey, List<MenuItem> items}) {
void show({Rect rect, GlobalKey widgetKey, List<MenuItemProvider> items}) {
if (rect == null && widgetKey == null) {
print("'rect' and 'key' can't be both null");
return;
Expand Down Expand Up @@ -235,7 +252,7 @@ class PopupMenu {

// 创建一行的item, row 从0开始算
List<Widget> _createRowItems(int row) {
List<MenuItem> subItems =
List<MenuItemProvider> subItems =
items.sublist(row * _col, min(row * _col + _col, items.length));
List<Widget> itemWidgets = [];
int i = 0;
Expand Down Expand Up @@ -306,7 +323,7 @@ class PopupMenu {
return width / ratio;
}

Widget _createMenuItem(MenuItem item, bool showLine) {
Widget _createMenuItem(MenuItemProvider item, bool showLine) {
return _MenuItemWidget(
item: item,
showLine: showLine,
Expand All @@ -326,6 +343,11 @@ class PopupMenu {
}

void dismiss() {
if (!_isShow) {
// Remove method should only be called once
return;
}

_entry.remove();
_isShow = false;
if (dismissCallback != null) {
Expand All @@ -339,7 +361,7 @@ class PopupMenu {
}

class _MenuItemWidget extends StatefulWidget {
final MenuItem item;
final MenuItemProvider item;
// 是否要显示右边的分隔线
final bool showLine;
final Color lineColor;
Expand Down Expand Up @@ -437,7 +459,7 @@ class _MenuItemWidgetState extends State<_MenuItemWidget> {
child: Material(
color: Colors.transparent,
child: Text(
widget.item.title,
widget.item.menuTitle,
style: widget.item.menuTextStyle,
),
),
Expand Down

0 comments on commit dc08c07

Please sign in to comment.