import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'dart:async'; import 'kr_simple_loading.dart'; /// 简单的Toast组件,替代flutter_easyloading class KRToast { static OverlayEntry? _overlayEntry; static Timer? _timer; /// 显示Toast消息 static void kr_showToast( String message, { Duration duration = const Duration(milliseconds: 1500), KRToastPosition position = KRToastPosition.center, }) { _hideToast(); // 隐藏之前的Toast _overlayEntry = OverlayEntry( builder: (context) => _ToastWidget( message: message, position: position, ), ); Overlay.of(Get.overlayContext!).insert(_overlayEntry!); _timer = Timer(duration, () { _hideToast(); }); } /// 显示加载动画 static void kr_showLoading({String? message}) { _hideToast(); // 隐藏之前的Toast _overlayEntry = OverlayEntry( builder: (context) => _LoadingWidget( message: message, ), ); Overlay.of(Get.overlayContext!).insert(_overlayEntry!); } /// 隐藏Toast或Loading static void kr_hideLoading() { _hideToast(); } /// 隐藏Toast static void _hideToast() { _timer?.cancel(); _timer = null; _overlayEntry?.remove(); _overlayEntry = null; } } /// Toast位置枚举 enum KRToastPosition { top, center, bottom, } /// Toast组件 class _ToastWidget extends StatelessWidget { final String message; final KRToastPosition position; const _ToastWidget({ required this.message, required this.position, }); @override Widget build(BuildContext context) { return Material( color: Colors.transparent, child: SafeArea( child: Align( alignment: _getAlignment(), child: Container( margin: const EdgeInsets.all(16.0), padding: const EdgeInsets.symmetric(horizontal: 20.0, vertical: 12.0), decoration: BoxDecoration( color: Colors.black.withOpacity(0.8), borderRadius: BorderRadius.circular(12.0), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.1), offset: const Offset(0, 2), blurRadius: 8, spreadRadius: 2, ) ], ), child: Text( message, style: const TextStyle( fontSize: 15.0, color: Colors.white, fontWeight: FontWeight.w500, letterSpacing: 0.3, ), ), ), ), ), ); } Alignment _getAlignment() { switch (position) { case KRToastPosition.top: return Alignment.topCenter; case KRToastPosition.center: return Alignment.center; case KRToastPosition.bottom: return Alignment.bottomCenter; } } } /// Loading组件 class _LoadingWidget extends StatelessWidget { final String? message; const _LoadingWidget({this.message}); @override Widget build(BuildContext context) { return Material( color: Colors.black.withOpacity(0.2), child: SafeArea( child: Center( child: Container( padding: const EdgeInsets.symmetric(horizontal: 25.0, vertical: 15.0), decoration: BoxDecoration( color: Colors.black.withOpacity(0.8), borderRadius: BorderRadius.circular(15.0), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.1), offset: const Offset(0, 2), blurRadius: 8, spreadRadius: 2, ) ], ), child: Column( mainAxisSize: MainAxisSize.min, children: [ KRSimpleLoading( color: Colors.white, size: 35.0, ), if (message != null) ...[ const SizedBox(height: 12.0), Text( message!, style: const TextStyle( fontSize: 14.0, color: Colors.white, ), ), ], ], ), ), ), ), ); } }