Flutter开发08-自定义组件

提示框

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
import 'package:flutter/material.dart';
import 'dart:async';

class ToastHelper {
static void showToast(BuildContext context, String text) {
const style = TextStyle(color: Colors.white, fontSize: 16.0);

Widget widget = Center(
child: Material(
child: Container(
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 10.0),
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.5),
borderRadius: BorderRadius.all(Radius.circular(6))),
child: Text(
text,
textAlign: TextAlign.center,
style: style,
),
),
),
);
var entry = OverlayEntry(
builder: (_) => widget,
);

Overlay.of(context).insert(entry);

Timer(const Duration(seconds: 2), () {
entry?.remove();
});
}
}

调用

1
ToastHelper.showToast(context, "双击两次退出!");

加载中

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
import 'package:flutter/material.dart';

class LoadingHelper {
static bool isShow = false;

static void showLoading(BuildContext context, {String text}) {
showDialog(
context: context,
barrierDismissible: false,
builder: (_) {
if (text != null) {
return new ZLoadingDialog(
outsideDismiss: false,
loadingText: text,
);
} else {
return new ZLoadingDialog(
outsideDismiss: false,
);
}
});
isShow = true;
}

static void hideLoading(BuildContext context) {
if (isShow) {
Navigator.of(context).pop();
isShow = false;
}
}
}

class ZLoadingDialog extends StatefulWidget {
String loadingText;
bool outsideDismiss;
Function dismissCallback;

ZLoadingDialog(
{Key key,
this.loadingText = "加载中...",
this.outsideDismiss = true,
this.dismissCallback})
: super(key: key);

@override
State<ZLoadingDialog> createState() => _LoadingDialog();
}

class _LoadingDialog extends State<ZLoadingDialog> {
_dismissDialog() {
if (widget.dismissCallback != null) {
widget.dismissCallback();
}
Navigator.of(context).pop();
}

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return new GestureDetector(
onTap: widget.outsideDismiss ? _dismissDialog : null,
child: Material(
type: MaterialType.transparency,
child: new Center(
child: new SizedBox(
width: 120.0,
height: 120.0,
child: new Container(
decoration: ShapeDecoration(
color: Color(0xffffffff),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(8.0),
),
),
),
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new CircularProgressIndicator(),
new Padding(
padding: const EdgeInsets.only(
top: 20.0,
),
child: new Text(
widget.loadingText,
style: new TextStyle(fontSize: 12.0),
),
),
],
),
),
),
),
),
);
}
}

调用方式

1
2
3
4
5
6
7
8
LoadingHelper.showLoading(context);
client
.teacher_login(this.username, this.userpwd)
.then((value) => {login(value)})
.catchError((err) =>
{ToastHelper.showToast(context, "请求失败!")})
.whenComplete(
() => LoadingHelper.hideLoading(context));