引用了fluttertoast插件
static void showToast(
BuildContext context, ToastType toastType, String content) {
FlutterToast flutterToast = FlutterToast(context);
Widget toast = Container(
constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width/2),
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
color: Color.fromRGBO(50, 50, 50, 0.9),
),
child: Column(mainAxisSize: MainAxisSize.min, children: [
toastType == ToastType.OK
? Icon(Icons.check, color: Colors.blue)
: (toastType == ToastType.Error
? Icon(Icons.error, color: Colors.red)
: Icon(Icons.warning, color: Colors.yellow)),
Padding(
padding: EdgeInsets.only(top: 10),
child: Text(content,
style: TextStyle(
fontSize: 13,
color: Colors.white,
)),
),
]),
);
flutterToast.showToast(
child: toast,
gravity: ToastGravity.CENTER,
toastDuration: Duration(seconds: 2),
);
}
enum ToastType { Warning, Error, OK }
可以在页面里直接调用,非常美观