forked from asjqkkkk/flutter-todos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverlay_util.dart
70 lines (58 loc) · 1.67 KB
/
overlay_util.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
import 'package:flutter/material.dart';
class OverlayUtil{
static OverlayUtil _instance;
static OverlayUtil getInstance(){
if(_instance == null){
_instance = OverlayUtil._internal();
}
return _instance;
}
OverlayUtil._internal();
OverlayEntry _overlayEntry;
// Timer timeTask;
void show(BuildContext context, {Widget showWidget, String text = "默认显示内容",Duration duration }){
if(_overlayEntry == null){
_showEntry(showWidget, context,text,duration);
} else{
_overlayEntry.remove();
_overlayEntry = null;
_showEntry(showWidget, context,text,duration);
}
}
void hide(){
if(_overlayEntry != null){
_overlayEntry.remove();
_overlayEntry = null;
}
}
void _showEntry(Widget showWidget, BuildContext context, String text, Duration duration) {
_overlayEntry = OverlayEntry(builder: (ctx){
return showWidget??_defaultShow(text);
});
Overlay.of(context).insert(_overlayEntry);
// timeTask?.cancel();
// timeTask = Timer(duration??Duration(seconds: 3), (){
// if(_overlayEntry != null){
// _overlayEntry.remove();
// _overlayEntry = null;
// }
// });
}
Widget _defaultShow(String text){
return Container(
alignment: Alignment.bottomCenter,
margin: EdgeInsets.only(bottom: 50),
child: Material(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Colors.grey.withOpacity(0.5),
child: Container(
margin: EdgeInsets.fromLTRB(10, 5, 10, 5),
child: Text(
text,
style: TextStyle(fontSize: 16, color: Colors.white),
),
),
),
);
}
}