@@ -17,6 +17,8 @@ class FocusedMenuHolder extends StatefulWidget {
17
17
final Color blurBackgroundColor;
18
18
final double bottomOffsetHeight;
19
19
final double menuOffset;
20
+ /// Open with tap insted of long press.
21
+ final bool openWithTap;
20
22
21
23
const FocusedMenuHolder (
22
24
{Key key,
@@ -31,7 +33,8 @@ class FocusedMenuHolder extends StatefulWidget {
31
33
this .blurBackgroundColor,
32
34
this .menuWidth,
33
35
this .bottomOffsetHeight,
34
- this .menuOffset})
36
+ this .menuOffset,
37
+ this .openWithTap = false })
35
38
: super (key: key);
36
39
37
40
@override
@@ -43,7 +46,7 @@ class _FocusedMenuHolderState extends State<FocusedMenuHolder> {
43
46
Offset childOffset = Offset (0 , 0 );
44
47
Size childSize;
45
48
46
- getOffset () {
49
+ getOffset (){
47
50
RenderBox renderBox = containerKey.currentContext.findRenderObject ();
48
51
Size size = renderBox.size;
49
52
Offset offset = renderBox.localToGlobal (Offset .zero);
@@ -57,38 +60,48 @@ class _FocusedMenuHolderState extends State<FocusedMenuHolder> {
57
60
Widget build (BuildContext context) {
58
61
return GestureDetector (
59
62
key: containerKey,
60
- onTap: widget.onPressed,
63
+ onTap: () async {
64
+ widget.onPressed ();
65
+ if (widget.openWithTap) {
66
+ await openMenu (context);
67
+ }
68
+ },
61
69
onLongPress: () async {
62
- getOffset ();
63
- await Navigator .push (
64
- context,
65
- PageRouteBuilder (
66
- transitionDuration:
67
- widget.duration ?? Duration (milliseconds: 100 ),
68
- pageBuilder: (context, animation, secondaryAnimation) {
69
- animation = Tween (begin: 0.0 , end: 1.0 ).animate (animation);
70
- return FadeTransition (
71
- opacity: animation,
72
- child: FocusedMenuDetails (
73
- itemExtent: widget.menuItemExtent,
74
- menuBoxDecoration: widget.menuBoxDecoration,
75
- child: widget.child,
76
- childOffset: childOffset,
77
- childSize: childSize,
78
- menuItems: widget.menuItems,
79
- blurSize: widget.blurSize,
80
- menuWidth: widget.menuWidth,
81
- blurBackgroundColor: widget.blurBackgroundColor,
82
- animateMenu: widget.animateMenuItems ?? true ,
83
- bottomOffsetHeight: widget.bottomOffsetHeight ?? 0 ,
84
- menuOffset: widget.menuOffset ?? 0 ,
85
- ));
86
- },
87
- fullscreenDialog: true ,
88
- opaque: false ));
70
+ if (! widget.openWithTap) {
71
+ await openMenu (context);
72
+ }
89
73
},
90
74
child: widget.child);
91
75
}
76
+
77
+ Future openMenu (BuildContext context) async {
78
+ getOffset ();
79
+ await Navigator .push (
80
+ context,
81
+ PageRouteBuilder (
82
+ transitionDuration: widget.duration ?? Duration (milliseconds: 100 ),
83
+ pageBuilder: (context, animation, secondaryAnimation) {
84
+ animation = Tween (begin: 0.0 , end: 1.0 ).animate (animation);
85
+ return FadeTransition (
86
+ opacity: animation,
87
+ child: FocusedMenuDetails (
88
+ itemExtent: widget.menuItemExtent,
89
+ menuBoxDecoration: widget.menuBoxDecoration,
90
+ child: widget.child,
91
+ childOffset: childOffset,
92
+ childSize: childSize,
93
+ menuItems: widget.menuItems,
94
+ blurSize: widget.blurSize,
95
+ menuWidth: widget.menuWidth,
96
+ blurBackgroundColor: widget.blurBackgroundColor,
97
+ animateMenu: widget.animateMenuItems ?? true ,
98
+ bottomOffsetHeight: widget.bottomOffsetHeight ?? 0 ,
99
+ menuOffset: widget.menuOffset ?? 0 ,
100
+ ));
101
+ },
102
+ fullscreenDialog: true ,
103
+ opaque: false ));
104
+ }
92
105
}
93
106
94
107
class FocusedMenuDetails extends StatelessWidget {
@@ -106,19 +119,7 @@ class FocusedMenuDetails extends StatelessWidget {
106
119
final double menuOffset;
107
120
108
121
const FocusedMenuDetails (
109
- {Key key,
110
- @required this .menuItems,
111
- @required this .child,
112
- @required this .childOffset,
113
- @required this .childSize,
114
- @required this .menuBoxDecoration,
115
- @required this .itemExtent,
116
- @required this .animateMenu,
117
- @required this .blurSize,
118
- @required this .blurBackgroundColor,
119
- @required this .menuWidth,
120
- this .bottomOffsetHeight,
121
- this .menuOffset})
122
+ {Key key, @required this .menuItems, @required this .child, @required this .childOffset, @required this .childSize,@required this .menuBoxDecoration, @required this .itemExtent,@required this .animateMenu,@required this .blurSize,@required this .blurBackgroundColor,@required this .menuWidth, this .bottomOffsetHeight, this .menuOffset})
122
123
: super (key: key);
123
124
124
125
@override
@@ -128,15 +129,10 @@ class FocusedMenuDetails extends StatelessWidget {
128
129
final maxMenuHeight = size.height * 0.45 ;
129
130
final listHeight = menuItems.length * (itemExtent ?? 50.0 );
130
131
131
- final maxMenuWidth = menuWidth ?? (size.width * 0.70 );
132
+ final maxMenuWidth = menuWidth?? (size.width * 0.70 );
132
133
final menuHeight = listHeight < maxMenuHeight ? listHeight : maxMenuHeight;
133
- final leftOffset = (childOffset.dx + maxMenuWidth) < size.width
134
- ? childOffset.dx
135
- : (childOffset.dx - maxMenuWidth + childSize.width);
136
- final topOffset = (childOffset.dy + menuHeight + childSize.height) <
137
- size.height - bottomOffsetHeight
138
- ? childOffset.dy + childSize.height + menuOffset
139
- : childOffset.dy - menuHeight - menuOffset;
134
+ final leftOffset = (childOffset.dx+ maxMenuWidth ) < size.width ? childOffset.dx: (childOffset.dx- maxMenuWidth+ childSize.width);
135
+ final topOffset = (childOffset.dy + menuHeight + childSize.height) < size.height - bottomOffsetHeight ? childOffset.dy + childSize.height + menuOffset : childOffset.dy - menuHeight - menuOffset;
140
136
return Scaffold (
141
137
backgroundColor: Colors .transparent,
142
138
body: Container (
@@ -148,11 +144,9 @@ class FocusedMenuDetails extends StatelessWidget {
148
144
Navigator .pop (context);
149
145
},
150
146
child: BackdropFilter (
151
- filter: ImageFilter .blur (
152
- sigmaX: blurSize ?? 4 , sigmaY: blurSize ?? 4 ),
147
+ filter: ImageFilter .blur (sigmaX: blurSize?? 4 , sigmaY: blurSize?? 4 ),
153
148
child: Container (
154
- color:
155
- (blurBackgroundColor ?? Colors .black).withOpacity (0.7 ),
149
+ color: (blurBackgroundColor?? Colors .black).withOpacity (0.7 ),
156
150
),
157
151
)),
158
152
Positioned (
@@ -174,14 +168,8 @@ class FocusedMenuDetails extends StatelessWidget {
174
168
decoration: menuBoxDecoration ??
175
169
BoxDecoration (
176
170
color: Colors .grey.shade200,
177
- borderRadius:
178
- const BorderRadius .all (Radius .circular (5.0 )),
179
- boxShadow: [
180
- const BoxShadow (
181
- color: Colors .black38,
182
- blurRadius: 10 ,
183
- spreadRadius: 1 )
184
- ]),
171
+ borderRadius: const BorderRadius .all (Radius .circular (5.0 )),
172
+ boxShadow: [const BoxShadow (color: Colors .black38, blurRadius: 10 , spreadRadius: 1 )]),
185
173
child: ClipRRect (
186
174
borderRadius: const BorderRadius .all (Radius .circular (5.0 )),
187
175
child: ListView .builder (
@@ -191,26 +179,24 @@ class FocusedMenuDetails extends StatelessWidget {
191
179
itemBuilder: (context, index) {
192
180
FocusedMenuItem item = menuItems[index];
193
181
Widget listItem = GestureDetector (
194
- onTap: () {
182
+ onTap:
183
+ () {
195
184
Navigator .pop (context);
196
185
item.onPressed ();
186
+
197
187
},
198
188
child: Container (
199
189
alignment: Alignment .center,
200
190
margin: const EdgeInsets .only (bottom: 1 ),
201
191
color: item.backgroundColor ?? Colors .white,
202
192
height: itemExtent ?? 50.0 ,
203
193
child: Padding (
204
- padding: const EdgeInsets .symmetric (
205
- vertical: 8.0 , horizontal: 14 ),
194
+ padding: const EdgeInsets .symmetric (vertical: 8.0 , horizontal: 14 ),
206
195
child: Row (
207
- mainAxisAlignment:
208
- MainAxisAlignment .spaceBetween,
196
+ mainAxisAlignment: MainAxisAlignment .spaceBetween,
209
197
children: < Widget > [
210
198
item.title,
211
- if (item.trailingIcon != null ) ...[
212
- item.trailingIcon
213
- ]
199
+ if (item.trailingIcon != null ) ...[item.trailingIcon]
214
200
],
215
201
),
216
202
)));
@@ -235,15 +221,10 @@ class FocusedMenuDetails extends StatelessWidget {
235
221
),
236
222
),
237
223
),
238
- Positioned (
239
- top: childOffset.dy,
240
- left: childOffset.dx,
241
- child: AbsorbPointer (
242
- absorbing: true ,
243
- child: Container (
244
- width: childSize.width,
245
- height: childSize.height,
246
- child: child))),
224
+ Positioned (top: childOffset.dy, left: childOffset.dx, child: AbsorbPointer (absorbing: true , child: Container (
225
+ width: childSize.width,
226
+ height: childSize.height,
227
+ child: child))),
247
228
],
248
229
),
249
230
),
0 commit comments