| 
									
										
										
										
											2025-07-18 17:13:38 +08:00
										 |  |  | import 'package:flutter/material.dart'; | 
					
						
							|  |  |  | import 'package:fluttertoast/fluttertoast.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ToastUtil { | 
					
						
							| 
									
										
										
										
											2025-09-02 16:22:17 +08:00
										 |  |  |   /// 普通提示(仅文字,屏幕中间)
 | 
					
						
							|  |  |  |   static void showNormal( | 
					
						
							|  |  |  |       BuildContext context, | 
					
						
							|  |  |  |       String message, { | 
					
						
							|  |  |  |         ToastGravity gravity = ToastGravity.CENTER, // 修改为 CENTER
 | 
					
						
							|  |  |  |         int duration = 2, | 
					
						
							|  |  |  |       }) { | 
					
						
							| 
									
										
										
										
											2025-07-18 17:13:38 +08:00
										 |  |  |     _showToast( | 
					
						
							|  |  |  |       context: context, | 
					
						
							|  |  |  |       message: message, | 
					
						
							|  |  |  |       gravity: gravity, | 
					
						
							|  |  |  |       duration: duration, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-02 16:22:17 +08:00
										 |  |  |   /// 成功提示(带图标,屏幕中间)
 | 
					
						
							|  |  |  |   static void showSuccess( | 
					
						
							|  |  |  |       BuildContext context, | 
					
						
							|  |  |  |       String message, { | 
					
						
							|  |  |  |         ToastGravity gravity = ToastGravity.CENTER, // 修改为 CENTER
 | 
					
						
							|  |  |  |         int duration = 3, | 
					
						
							|  |  |  |       }) { | 
					
						
							| 
									
										
										
										
											2025-07-18 17:13:38 +08:00
										 |  |  |     _showToast( | 
					
						
							|  |  |  |       context: context, | 
					
						
							|  |  |  |       message: message, | 
					
						
							|  |  |  |       gravity: gravity, | 
					
						
							|  |  |  |       duration: duration, | 
					
						
							|  |  |  |       isSuccess: true, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-02 16:22:17 +08:00
										 |  |  |   /// 失败提示(带图标,屏幕中间)
 | 
					
						
							|  |  |  |   static void showError( | 
					
						
							|  |  |  |       BuildContext context, | 
					
						
							|  |  |  |       String message, { | 
					
						
							|  |  |  |         ToastGravity gravity = ToastGravity.CENTER, // 修改为 CENTER
 | 
					
						
							|  |  |  |         int duration = 4, | 
					
						
							|  |  |  |       }) { | 
					
						
							| 
									
										
										
										
											2025-07-18 17:13:38 +08:00
										 |  |  |     _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, | 
					
						
							| 
									
										
										
										
											2025-09-02 16:22:17 +08:00
										 |  |  |         gravity: gravity, // 始终 CENTER
 | 
					
						
							| 
									
										
										
										
											2025-07-18 17:13:38 +08:00
										 |  |  |         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, | 
					
						
							|  |  |  |           ), | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2025-09-02 16:22:17 +08:00
										 |  |  | } |