popup_window.dart
7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
///create by elileo on 2018/12/21
///https://github.com/elileo1/flutter_travel_friends/blob/master/lib/widget/PopupWindow.dart
///
/// weilu update:
/// 1.去除了IntrinsicWidth限制,添加了默认蒙版。
/// 2.简化position计算。
const Duration _kWindowDuration = Duration.zero;
const double _kWindowCloseIntervalEnd = 2.0 / 3.0;
const double _kWindowScreenPadding = 0.001;
///弹窗方法
Future<T?> showPopupWindow<T>({
required BuildContext context,
required RenderBox anchor,
required Widget child,
Offset? offset,
String? semanticLabel,
bool isShowBg = false,
}) {
switch (defaultTargetPlatform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
break;
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
semanticLabel ??= MaterialLocalizations.of(context).popupMenuLabel;
}
final RenderBox? overlay = Overlay.of(context)!.context.findRenderObject() as RenderBox?;
// 默认位置锚点下方
final Offset _offset = Offset(0, anchor.size.height);
if (offset == null) {
offset = _offset;
} else {
offset = offset + _offset;
}
// 获得控件左下方的坐标
final a = anchor.localToGlobal(offset, ancestor: overlay);
// 获得控件右下方的坐标
final b = anchor.localToGlobal(anchor.size.bottomLeft(offset), ancestor: overlay);
final RelativeRect position = RelativeRect.fromRect(
Rect.fromPoints(a, b),
Offset.zero & overlay!.size,
);
return Navigator.push(context,
_PopupWindowRoute(
position: position,
child: child,
semanticLabel: semanticLabel,
barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
isShowBg: isShowBg
));
}
///自定义弹窗路由:参照_PopupMenuRoute修改的
class _PopupWindowRoute<T> extends PopupRoute<T> {
_PopupWindowRoute({
RouteSettings? settings,
required this.child,
required this.position,
required this.barrierLabel,
required this.semanticLabel,
required this.isShowBg,
}) : super(settings: settings);
final Widget child;
final RelativeRect position;
final String? semanticLabel;
final bool isShowBg;
@override
Color? get barrierColor => null;
@override
bool get barrierDismissible => true;
@override
final String barrierLabel;
@override
Duration get transitionDuration => _kWindowDuration;
@override
Animation<double> createAnimation() {
return CurvedAnimation(
parent: super.createAnimation(),
curve: Curves.linear,
reverseCurve: const Interval(0.0, _kWindowCloseIntervalEnd));
}
@override
Widget buildPage(BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation) {
final Widget win = _PopupWindow<T>(
route: this,
semanticLabel: semanticLabel,
);
return MediaQuery.removePadding(
context: context,
removeTop: true,
removeBottom: true,
removeLeft: true,
removeRight: true,
child: Builder(
builder: (BuildContext context) {
return GestureDetector(
onTap: () => Navigator.pop(context),
child: Material(
type: MaterialType.transparency,
child: Container(
width: double.infinity,
height: double.infinity,
color: isShowBg ? const Color(0x99000000) : null,
child: CustomSingleChildLayout(
delegate: _PopupWindowLayoutDelegate(
position, Directionality.of(context)
),
child: win,
),
),
),
);
},
),
);
}
}
///自定义弹窗控件:对自定义的弹窗内容进行再包装,添加长宽、动画等约束条件
class _PopupWindow<T> extends StatelessWidget {
const _PopupWindow({
Key? key,
required this.route,
required this.semanticLabel,
}) : super(key: key);
final _PopupWindowRoute<T> route;
final String? semanticLabel;
@override
Widget build(BuildContext context) {
const double length = 10.0;
const double unit = 1.0 /
(length + 1.5); // 1.0 for the width and 0.5 for the last item's fade.
final CurveTween opacity = CurveTween(curve: const Interval(0.0, 1.0 / 3.0));
final CurveTween width = CurveTween(curve: const Interval(0.0, unit));
final CurveTween height = CurveTween(curve: const Interval(0.0, unit * length));
final Widget child = SingleChildScrollView(
child: route.child,
);
return AnimatedBuilder(
animation: route.animation!,
builder: (BuildContext context, Widget? child) {
return Opacity(
opacity: opacity.evaluate(route.animation!),
child: Align(
alignment: AlignmentDirectional.topEnd,
widthFactor: width.evaluate(route.animation!),
heightFactor: height.evaluate(route.animation!),
child: Semantics(
scopesRoute: true,
namesRoute: true,
explicitChildNodes: true,
label: semanticLabel,
child: child,
),
),
);
},
child: child,
);
}
}
///自定义委托内容:子控件大小及其位置计算
class _PopupWindowLayoutDelegate extends SingleChildLayoutDelegate {
_PopupWindowLayoutDelegate(
this.position, this.textDirection);
final RelativeRect position;
final TextDirection textDirection;
@override
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
// The menu can be at most the size of the overlay minus 8.0 pixels in each
// direction.
return BoxConstraints.loose(constraints.biggest -
const Offset(_kWindowScreenPadding * 2.0, _kWindowScreenPadding * 2.0) as Size);
}
@override
Offset getPositionForChild(Size size, Size childSize) {
// size: The size of the overlay.
// childSize: The size of the menu, when fully open, as determined by
// getConstraintsForChild.
// Find the ideal vertical position.
double y = position.top;
// Find the ideal horizontal position.
double x;
if (position.left > position.right) {
// Menu button is closer to the right edge, so grow to the left, aligned to the right edge.
x = size.width - position.right - childSize.width;
} else if (position.left < position.right) {
// Menu button is closer to the left edge, so grow to the right, aligned to the left edge.
x = position.left;
} else {
// Menu button is equidistant from both edges, so grow in reading direction.
switch (textDirection) {
case TextDirection.rtl:
x = size.width - position.right - childSize.width;
break;
case TextDirection.ltr:
x = position.left;
break;
}
}
// Avoid going outside an area defined as the rectangle 8.0 pixels from the
// edge of the screen in every direction.
if (x < _kWindowScreenPadding)
x = _kWindowScreenPadding;
else if (x + childSize.width > size.width - _kWindowScreenPadding)
x = size.width - childSize.width - _kWindowScreenPadding;
if (y < _kWindowScreenPadding)
y = _kWindowScreenPadding;
else if (y + childSize.height > size.height - _kWindowScreenPadding)
y = size.height - childSize.height - _kWindowScreenPadding;
return Offset(x, y);
}
@override
bool shouldRelayout(_PopupWindowLayoutDelegate oldDelegate) {
return position != oldDelegate.position;
}
}