elysia/lib/plugin/Toast.dart
2025-11-04 09:53:47 +08:00

61 lines
1.7 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
class Toast{
static void show(
String message, {
ToastType? type,
ToastPosition? position,
}) {
// type 和 position 有默认值
ToastType toastType = type ?? ToastType.None;
ToastPosition toastPosition = position ?? ToastPosition.bottom;
showToastWidget(
_getContent(toastType, message),
duration: const Duration(seconds: 2),
position: toastPosition, // top / center / bottom
);
}
static Widget _getContent(ToastType type,String message){
Widget icon;
switch(type){
case ToastType.Normal: icon = Icon(Icons.check_circle,color: Colors.green);
case ToastType.Error: icon = Icon(Icons.cancel,color: Colors.redAccent);
case ToastType.Warn: icon = Icon(Icons.error,color: Colors.deepOrangeAccent);
case ToastType.None: icon = SizedBox();
}
Widget result = Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
decoration: BoxDecoration(
color: Colors.black87,
borderRadius: BorderRadius.circular(8),
boxShadow: [
BoxShadow(
color: Colors.black26,
blurRadius: 6,
offset: Offset(2, 2),
),
],
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
icon,
if(type!=ToastType.None)SizedBox(width: 5,),
Text(
message,
style: const TextStyle(color: Colors.white, fontSize: 16),
),
],
),
);
return result;
}
}
enum ToastType{
None,Error,Warn,Normal
}