107 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Dart
		
	
	
			
		
		
	
	
			107 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Dart
		
	
	
| import 'package:flutter/material.dart';
 | |
| import 'package:fluttertoast/fluttertoast.dart';
 | |
| 
 | |
| class ToastUtil {
 | |
|   /// 普通灰色背景提示(仅文字)
 | |
|   static void showNormal(BuildContext context, String message, {
 | |
|     ToastGravity gravity = ToastGravity.BOTTOM,
 | |
|     int duration = 2,
 | |
|   }) {
 | |
|     _showToast(
 | |
|       context: context,
 | |
|       message: message,
 | |
|       gravity: gravity,
 | |
|       duration: duration,
 | |
|     );
 | |
|   }
 | |
| 
 | |
|   /// 成功提示(带图标)
 | |
|   static void showSuccess(BuildContext context, String message, {
 | |
|     ToastGravity gravity = ToastGravity.CENTER,
 | |
|     int duration = 3,
 | |
|   }) {
 | |
|     _showToast(
 | |
|       context: context,
 | |
|       message: message,
 | |
|       gravity: gravity,
 | |
|       duration: duration,
 | |
|       isSuccess: true,
 | |
|     );
 | |
|   }
 | |
| 
 | |
|   /// 失败提示(带图标)
 | |
|   static void showError(BuildContext context, String message, {
 | |
|     ToastGravity gravity = ToastGravity.CENTER,
 | |
|     int duration = 4,
 | |
|   }) {
 | |
|     _showToast(
 | |
|       context: context,
 | |
|       message: message,
 | |
|       gravity: gravity,
 | |
|       duration: duration,
 | |
|       isError: true,
 | |
|     );
 | |
|   }
 | |
| 
 | |
|   /// 内部通用方法
 | |
|   static void _showToast({
 | |
|     required BuildContext context,
 | |
|     required String message,
 | |
|     required ToastGravity gravity,
 | |
|     required int duration,
 | |
|     bool isSuccess = false,
 | |
|     bool isError = false,
 | |
|   }) {
 | |
|     // 如果有图标(成功或失败),则使用自定义 Widget
 | |
|     if (isSuccess || isError) {
 | |
|       final fToast = FToast();
 | |
|       fToast.init(context);
 | |
|       fToast.showToast(
 | |
|         child: _buildIconToast(message, isSuccess: isSuccess),
 | |
|         gravity: gravity,
 | |
|         toastDuration: Duration(seconds: duration),
 | |
|       );
 | |
|     }
 | |
|     // 普通文字提示
 | |
|     else {
 | |
|       Fluttertoast.showToast(
 | |
|         msg: message,
 | |
|         toastLength: duration > 2 ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT,
 | |
|         gravity: gravity,
 | |
|         backgroundColor: Colors.grey[500],
 | |
|         textColor: Colors.white,
 | |
|         fontSize: 16.0,
 | |
|       );
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   /// 构建带图标的 Toast Widget
 | |
|   static Widget _buildIconToast(String message, {bool isSuccess = true}) {
 | |
|     return Container(
 | |
|       padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 16.0),
 | |
|       decoration: BoxDecoration(
 | |
|         borderRadius: BorderRadius.circular(25.0),
 | |
|         color: Colors.grey[850]?.withOpacity(0.9),
 | |
|       ),
 | |
|       child: Column(
 | |
|         mainAxisSize: MainAxisSize.min,
 | |
|         children: [
 | |
|           Icon(
 | |
|             isSuccess ? Icons.check : Icons.error_outline,
 | |
|             color: isSuccess ? Colors.greenAccent[400] : Colors.redAccent[400],
 | |
|             size: 36.0,
 | |
|           ),
 | |
|           const SizedBox(height: 8.0),
 | |
|           Text(
 | |
|             message,
 | |
|             style: const TextStyle(
 | |
|               color: Colors.white,
 | |
|               fontSize: 16.0,
 | |
|             ),
 | |
|             textAlign: TextAlign.center,
 | |
|           ),
 | |
|         ],
 | |
|       ),
 | |
|     );
 | |
|   }
 | |
| } |