From 89d3b79617e4921ec937fbfdb2de0fd0781eb25b Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 7 May 2020 19:04:58 +0200 Subject: [PATCH] Simplify code --- lib/ui/screens/notifications_screen.dart | 10 +-------- lib/ui/widgets/custom_list_tile.dart | 27 +++++++++++++++++++----- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/ui/screens/notifications_screen.dart b/lib/ui/screens/notifications_screen.dart index 7c0a4bc..935962d 100644 --- a/lib/ui/screens/notifications_screen.dart +++ b/lib/ui/screens/notifications_screen.dart @@ -292,15 +292,7 @@ class _NotificationTile extends StatelessWidget { onTap: () => _onTap(context), title: Text(message), subtitle: Text(diffTimeFromNowToStr(notification.timeCreate)), - onLongPressWithInfo: (size, offset) { - final position = RelativeRect.fromLTRB( - offset.dx - size.width, - offset.dy, - offset.dx, - offset.dy + size.height, - ); //fromSize(Rect.fromPoints(offset, Offset.zero), size);*/ - - print(position); + onLongPressOpenMenu: (position) { showMenu(context: context, position: position, items: [ PopupMenuItem( child: Text(tr("Delete")), diff --git a/lib/ui/widgets/custom_list_tile.dart b/lib/ui/widgets/custom_list_tile.dart index 84ee0b3..f37e72e 100644 --- a/lib/ui/widgets/custom_list_tile.dart +++ b/lib/ui/widgets/custom_list_tile.dart @@ -21,6 +21,9 @@ class CustomListTile extends StatelessWidget { /// Custom onLongPress function final Function(Size, Offset) onLongPressWithInfo; + /// Show menu onLongPress + final Function(RelativeRect) onLongPressOpenMenu; + const CustomListTile({ Key key, this.leading, @@ -35,6 +38,7 @@ class CustomListTile extends StatelessWidget { this.onLongPress, this.selected = false, this.onLongPressWithInfo, + this.onLongPressOpenMenu, }) : assert(isThreeLine != null), assert(enabled != null), assert(selected != null), @@ -59,12 +63,25 @@ class CustomListTile extends StatelessWidget { } void _longPress(BuildContext context) { - RenderBox renderBox = context.findRenderObject(); - final size = renderBox.size; - final offset = renderBox.localToGlobal(Offset(size.width, size.height)); - if (onLongPress != null) onLongPress(); - if (onLongPressWithInfo != null) onLongPressWithInfo(size, offset); + if (onLongPressWithInfo != null || onLongPressOpenMenu != null) { + RenderBox renderBox = context.findRenderObject(); + final size = renderBox.size; + final offset = renderBox.localToGlobal(Offset(size.width, size.height)); + + if (onLongPressWithInfo != null) onLongPressWithInfo(size, offset); + + if (onLongPressOpenMenu != null) { + final position = RelativeRect.fromLTRB( + offset.dx - size.width, + offset.dy, + offset.dx, + offset.dy + size.height, + ); + + onLongPressOpenMenu(position); + } + } } }