-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateLayer.dart
87 lines (75 loc) · 2.02 KB
/
CreateLayer.dart
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
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import './config/ShortSetting.dart';
import 'dart:async' show Completer;
///创建一个全屏的遮罩
///内容可自定义
///提供 show => 显示
///提供 close => 关闭
class CreateLayer {
OverlayEntry overlayEntry;
OverlayState overlayState;
bool active = false;
bool ignoring = false; // 是否事件穿透
bool _mounted = false; // 是否一个实例化挂载成功
CreateLayer(this.ignoring);
Future show ({
@required BuildContext context,
@required LayoutWidgetBuilder builder
}) async {
Completer completer = new Completer();
overlayState = Overlay.of(context);
if(this.active && !this._mounted) {
// 防止多个实例同时调用,导致只能关闭最后一个
// 在没有mounted之前,频繁触发,就关闭之前的
this.close();
}
this.active = true;
overlayEntry = new OverlayEntry(
builder: (context) {
Widget child = LayoutBuilder(
builder: (a, b) {
if(!completer.isCompleted) {
this._mounted = true;
completer.complete();
}
return builder(a, b);
}
);
return _Wrapper(ignoring, Container(
decoration: BoxDecoration(
color: ShortSetting.transparentColor
),
child: child,
),
);
}
);
overlayState.insert(overlayEntry);
return completer.future;
}
close () {
if (this.active) {
this._mounted = false;
this.overlayEntry?.remove();
this.active = false;
this.overlayEntry = null;
}
}
}
class _Wrapper extends StatelessWidget{
final bool ignoring;
final Widget child;
_Wrapper(this.ignoring, this.child);
@override
Widget build (BuildContext context) {
return !this.ignoring
? Container(
child: child,
)
: IgnorePointer(
ignoring: true,
child: child
);
}
}