-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtoast_utils.dart
More file actions
141 lines (129 loc) · 3.55 KB
/
toast_utils.dart
File metadata and controls
141 lines (129 loc) · 3.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
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
/// Name: 吐司工具类
/// 基于 [fluttertoast](https://pub.dev/packages/fluttertoast)
/// Created by Fitem on 2023/6/1
class ToastUtils {
/// 背景色
static Color toastBgColor = const Color(0x80000000);
/// Text颜色
static Color toastTextColor = Colors.white;
/// 字体大小
static double toastFontSize = 16.0;
/// 初始化 Toast
/// [bgColor] 背景色
/// [textColor] Text颜色
/// [fontSize] 字体大小
static void init({Color? bgColor, Color? textColor, double? fontSize}) {
toastBgColor = bgColor ?? toastBgColor;
toastTextColor = textColor ?? toastTextColor;
toastFontSize = fontSize ?? toastFontSize;
}
/// 显示中间的Toast
/// [msg] 显示的内容
static void showCenter(String msg) {
Fluttertoast.showToast(
msg: msg,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
backgroundColor: toastBgColor,
textColor: toastTextColor,
fontSize: 16.0,
);
}
/// 显示底部的Toast
/// [msg] 显示的内容
static void show(String msg) {
Fluttertoast.showToast(
msg: msg,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
backgroundColor: toastBgColor,
textColor: toastTextColor,
fontSize: 16.0,
);
}
/// 显示的Toast
/// [msg] 显示的内容
/// [toast] 显示的时间
/// [gravity] 显示的位置
/// [bgColor] 背景色
/// [textColor] Text颜色
/// [fontSize] 字体大小
static void showToast(
String msg, {
FToast toast = FToast.lengthShort,
FToastGravity gravity = FToastGravity.bottom,
Color? bgColor,
Color? textColor,
double? fontSize,
}) {
Fluttertoast.showToast(
msg: msg,
toastLength: _getToastLength(toast),
gravity: _getToastGravity(gravity),
backgroundColor: bgColor ?? toastBgColor,
textColor: textColor ?? toastTextColor,
fontSize: fontSize ?? toastFontSize,
);
}
/// 获取Toast显示时间
static _getToastLength(FToast toast) {
switch (toast) {
case FToast.lengthShort:
return Toast.LENGTH_SHORT;
case FToast.lengthLong:
return Toast.LENGTH_LONG;
}
}
/// 获取Toast显示位置
static _getToastGravity(FToastGravity gravity) {
switch (gravity) {
case FToastGravity.top:
return ToastGravity.TOP;
case FToastGravity.bottom:
return ToastGravity.BOTTOM;
case FToastGravity.center:
return ToastGravity.CENTER;
case FToastGravity.topLeft:
return ToastGravity.TOP_LEFT;
case FToastGravity.topRight:
return ToastGravity.TOP_RIGHT;
case FToastGravity.bottomLeft:
return ToastGravity.BOTTOM_LEFT;
case FToastGravity.bottomRight:
return ToastGravity.BOTTOM_RIGHT;
case FToastGravity.centerLeft:
return ToastGravity.CENTER_LEFT;
case FToastGravity.centerRight:
return ToastGravity.CENTER_RIGHT;
case FToastGravity.snackBar:
return ToastGravity.SNACKBAR;
case FToastGravity.none:
return ToastGravity.NONE;
}
}
}
/// Toast显示位置
enum FToastGravity {
top,
bottom,
center,
topLeft,
topRight,
bottomLeft,
bottomRight,
centerLeft,
centerRight,
snackBar,
none
}
/// Toast显示时间
/// 两种模式:[lengthShort] 显示时长 1秒钟、[lengthLong] 显示时长 5秒钟
/// Only for Android Platform
enum FToast {
/// Show Short toast for 1 sec
lengthShort,
/// Show Long toast for 5 sec
lengthLong
}