Files
CityCards_Customer_Flutter/lib/common_packages/custom_snackbar.dart

210 lines
5.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class CustomSnackbar {
static void show(
BuildContext context, {
required String message,
Color? backgroundColor,
Color? textColor,
IconData? icon,
Duration duration = const Duration(seconds: 3),
bool useOverlay = false,
}) {
if (useOverlay) {
_showOverlaySnackbar(
context,
message: message,
backgroundColor: backgroundColor ?? Colors.black87,
textColor: textColor ?? Colors.white,
icon: icon,
duration: duration,
);
} else {
_showRegularSnackbar(
context,
message: message,
backgroundColor: backgroundColor ?? Colors.black87,
textColor: textColor ?? Colors.white,
icon: icon,
);
}
}
static void _showRegularSnackbar(
BuildContext context, {
required String message,
required Color backgroundColor,
required Color textColor,
IconData? icon,
}) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Row(
children: [
if (icon != null) ...[
Icon(
icon,
color: textColor,
size: 20.sp,
),
SizedBox(width: 12.w),
],
Expanded(
child: Text(
message,
style: TextStyle(
color: textColor,
fontSize: 14.sp,
fontWeight: FontWeight.w500,
),
),
),
],
),
backgroundColor: backgroundColor,
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.r),
),
margin: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
),
);
}
static void _showOverlaySnackbar(
BuildContext context, {
required String message,
required Color backgroundColor,
required Color textColor,
IconData? icon,
required Duration duration,
}) {
final overlay = Overlay.of(context);
final overlayEntry = OverlayEntry(
builder: (context) => Positioned(
top: MediaQuery.of(context).padding.top + 10,
left: 20.w,
right: 20.w,
child: Material(
color: Colors.transparent,
child: TweenAnimationBuilder<double>(
tween: Tween(begin: 0.0, end: 1.0),
duration: const Duration(milliseconds: 300),
builder: (context, value, child) {
return Transform.translate(
offset: Offset(0, -20 * (1 - value)),
child: Opacity(
opacity: value,
child: child,
),
);
},
child: Container(
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
decoration: BoxDecoration(
color: backgroundColor,
borderRadius: BorderRadius.circular(8.r),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.3),
blurRadius: 10,
offset: const Offset(0, 4),
),
],
),
child: Row(
children: [
if (icon != null) ...[
Icon(
icon,
color: textColor,
size: 20.sp,
),
SizedBox(width: 12.w),
],
Expanded(
child: Text(
message,
style: TextStyle(
color: textColor,
fontSize: 14.sp,
fontWeight: FontWeight.w500,
),
),
),
],
),
),
),
),
),
);
overlay.insert(overlayEntry);
Future.delayed(duration, () {
overlayEntry.remove();
});
}
// Helper methods for common use cases
static void showSuccess(
BuildContext context, {
required String message,
bool useOverlay = false,
}) {
show(
context,
message: message,
backgroundColor: Colors.green,
textColor: Colors.white,
icon: Icons.check_circle,
useOverlay: useOverlay,
);
}
static void showError(
BuildContext context, {
required String message,
bool useOverlay = false,
}) {
show(
context,
message: message,
backgroundColor: Colors.red,
textColor: Colors.white,
icon: Icons.error,
useOverlay: useOverlay,
);
}
static void showWarning(
BuildContext context, {
required String message,
bool useOverlay = false,
}) {
show(
context,
message: message,
backgroundColor: Colors.orange,
textColor: Colors.white,
icon: Icons.warning,
useOverlay: useOverlay,
);
}
static void showInfo(
BuildContext context, {
required String message,
bool useOverlay = false,
}) {
show(
context,
message: message,
backgroundColor: Colors.blue,
textColor: Colors.white,
icon: Icons.info,
useOverlay: useOverlay,
);
}
}