Update: 2026-06-26 17:29:23
This commit is contained in:
@@ -2,12 +2,7 @@ import 'dart:ui';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:get/get.dart';
|
||||
import '../../constant/colors.dart';
|
||||
import '../../main.dart';
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Snackbar variant definition
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
enum _SnackVariant { success, error, info, warning }
|
||||
|
||||
extension _VariantProps on _SnackVariant {
|
||||
@@ -38,20 +33,8 @@ extension _VariantProps on _SnackVariant {
|
||||
_SnackVariant.info => 'Info',
|
||||
_SnackVariant.warning => 'Warning',
|
||||
};
|
||||
|
||||
HapticFeedbackType get haptic => switch (this) {
|
||||
_SnackVariant.error => HapticFeedbackType.medium,
|
||||
_SnackVariant.warning => HapticFeedbackType.medium,
|
||||
_SnackVariant.success => HapticFeedbackType.light,
|
||||
_SnackVariant.info => HapticFeedbackType.selection,
|
||||
};
|
||||
}
|
||||
|
||||
enum HapticFeedbackType { light, medium, selection }
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Core snackbar widget
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
class _SnackContent extends StatefulWidget {
|
||||
final String message;
|
||||
final _SnackVariant variant;
|
||||
@@ -65,6 +48,7 @@ class _SnackContent extends StatefulWidget {
|
||||
class _SnackContentState extends State<_SnackContent>
|
||||
with TickerProviderStateMixin {
|
||||
late final AnimationController _ctrl;
|
||||
late final AnimationController _scaleCtrl;
|
||||
late final Animation<double> _scaleAnim;
|
||||
late final Animation<double> _progressAnim;
|
||||
|
||||
@@ -74,25 +58,25 @@ class _SnackContentState extends State<_SnackContent>
|
||||
void initState() {
|
||||
super.initState();
|
||||
_ctrl = AnimationController(vsync: this, duration: _displayDuration);
|
||||
|
||||
_scaleCtrl = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(milliseconds: 500),
|
||||
);
|
||||
_scaleAnim = CurvedAnimation(
|
||||
parent: AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(milliseconds: 500),
|
||||
)..forward(),
|
||||
parent: _scaleCtrl,
|
||||
curve: Curves.elasticOut,
|
||||
);
|
||||
|
||||
_progressAnim = Tween<double>(begin: 1.0, end: 0.0).animate(
|
||||
CurvedAnimation(parent: _ctrl, curve: Curves.linear),
|
||||
);
|
||||
|
||||
_scaleCtrl.forward();
|
||||
_ctrl.forward();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_ctrl.dispose();
|
||||
_scaleCtrl.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -107,16 +91,16 @@ class _SnackContentState extends State<_SnackContent>
|
||||
decoration: BoxDecoration(
|
||||
color: surface,
|
||||
borderRadius: BorderRadius.circular(18),
|
||||
border: Border.all(color: accent.withOpacity(0.18), width: 1.2),
|
||||
border: Border.all(color: accent.withAlpha(46), width: 1.2),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: accent.withOpacity(0.12),
|
||||
color: accent.withAlpha(31),
|
||||
blurRadius: 20,
|
||||
spreadRadius: -2,
|
||||
offset: const Offset(0, 6),
|
||||
),
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.06),
|
||||
color: Colors.black.withAlpha(15),
|
||||
blurRadius: 10,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
@@ -127,28 +111,24 @@ class _SnackContentState extends State<_SnackContent>
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// ── Main row ──────────────────────────────────────────────────
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(14, 14, 10, 12),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
// Animated icon badge
|
||||
ScaleTransition(
|
||||
scale: _scaleAnim,
|
||||
child: Container(
|
||||
width: 40,
|
||||
height: 40,
|
||||
decoration: BoxDecoration(
|
||||
color: accent.withOpacity(0.12),
|
||||
color: accent.withAlpha(31),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: Icon(v.icon, color: accent, size: 22),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
|
||||
// Text content
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
@@ -166,8 +146,8 @@ class _SnackContentState extends State<_SnackContent>
|
||||
const SizedBox(height: 3),
|
||||
Text(
|
||||
widget.message,
|
||||
style: TextStyle(
|
||||
color: Colors.grey[800],
|
||||
style: const TextStyle(
|
||||
color: Color(0xFF424242),
|
||||
fontSize: 13.5,
|
||||
height: 1.4,
|
||||
fontWeight: FontWeight.w400,
|
||||
@@ -178,19 +158,17 @@ class _SnackContentState extends State<_SnackContent>
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// Close button
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
HapticFeedback.lightImpact();
|
||||
Get.closeCurrentSnackbar();
|
||||
_closeSnackbar(context);
|
||||
},
|
||||
child: Container(
|
||||
width: 30,
|
||||
height: 30,
|
||||
margin: const EdgeInsets.only(left: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.grey.withOpacity(0.1),
|
||||
color: Colors.grey.withAlpha(25),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: Icon(
|
||||
@@ -203,24 +181,17 @@ class _SnackContentState extends State<_SnackContent>
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// ── Thin progress strip ───────────────────────────────────────
|
||||
AnimatedBuilder(
|
||||
animation: _progressAnim,
|
||||
builder: (_, __) => Stack(
|
||||
children: [
|
||||
// Track
|
||||
Container(
|
||||
height: 3,
|
||||
color: accent.withOpacity(0.08),
|
||||
),
|
||||
// Fill
|
||||
Container(height: 3, color: accent.withAlpha(20)),
|
||||
FractionallySizedBox(
|
||||
widthFactor: _progressAnim.value,
|
||||
child: Container(
|
||||
height: 3,
|
||||
decoration: BoxDecoration(
|
||||
color: accent.withOpacity(0.45),
|
||||
color: accent.withAlpha(115),
|
||||
borderRadius: const BorderRadius.only(
|
||||
topRight: Radius.circular(4),
|
||||
bottomRight: Radius.circular(4),
|
||||
@@ -236,74 +207,57 @@ class _SnackContentState extends State<_SnackContent>
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _closeSnackbar(BuildContext context) {
|
||||
ScaffoldMessenger.maybeOf(context)?.hideCurrentSnackBar();
|
||||
}
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Internal dispatcher — single source of truth
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
SnackbarController _show(_SnackVariant variant, String message) {
|
||||
// Dismiss any existing snackbar first (no stacking)
|
||||
if (Get.isSnackbarOpen) Get.closeCurrentSnackbar();
|
||||
int _retryCount = 0;
|
||||
|
||||
switch (variant.haptic) {
|
||||
case HapticFeedbackType.light:
|
||||
HapticFeedback.lightImpact();
|
||||
case HapticFeedbackType.medium:
|
||||
void _show(_SnackVariant variant, String message) {
|
||||
if (Get.context == null) {
|
||||
if (_retryCount < 3) {
|
||||
_retryCount++;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => _show(variant, message));
|
||||
}
|
||||
return;
|
||||
}
|
||||
_retryCount = 0;
|
||||
|
||||
final context = Get.context;
|
||||
if (context == null) return;
|
||||
|
||||
final messenger = ScaffoldMessenger.maybeOf(context);
|
||||
if (messenger == null) return;
|
||||
|
||||
messenger.clearSnackBars();
|
||||
|
||||
switch (variant) {
|
||||
case _SnackVariant.error:
|
||||
case _SnackVariant.warning:
|
||||
HapticFeedback.mediumImpact();
|
||||
case HapticFeedbackType.selection:
|
||||
case _SnackVariant.success:
|
||||
HapticFeedback.lightImpact();
|
||||
case _SnackVariant.info:
|
||||
HapticFeedback.selectionClick();
|
||||
}
|
||||
|
||||
final BuildContext? context = Get.context ?? navigatorKey.currentContext;
|
||||
if (context != null) {
|
||||
final overlay = Overlay.maybeOf(context);
|
||||
if (overlay == null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
backgroundColor: Colors.transparent,
|
||||
elevation: 0,
|
||||
padding: EdgeInsets.zero,
|
||||
content: _SnackContent(message: message, variant: variant),
|
||||
duration: const Duration(seconds: 4),
|
||||
),
|
||||
);
|
||||
return SnackbarController(GetSnackBar(title: '', message: ''));
|
||||
}
|
||||
}
|
||||
|
||||
return Get.snackbar(
|
||||
'',
|
||||
'',
|
||||
snackPosition: SnackPosition.TOP,
|
||||
backgroundColor: Colors.transparent,
|
||||
margin: EdgeInsets.zero,
|
||||
padding: EdgeInsets.zero,
|
||||
duration: const Duration(seconds: 4),
|
||||
barBlur: 0,
|
||||
overlayBlur: 0,
|
||||
overlayColor: Colors.transparent,
|
||||
isDismissible: true,
|
||||
dismissDirection: DismissDirection.up,
|
||||
forwardAnimationCurve: Curves.easeOutCubic,
|
||||
reverseAnimationCurve: Curves.easeInCubic,
|
||||
snackStyle: SnackStyle.FLOATING,
|
||||
userInputForm: Form(
|
||||
child: _SnackContent(message: message, variant: variant),
|
||||
messenger.showSnackBar(
|
||||
SnackBar(
|
||||
content: _SnackContent(message: message, variant: variant),
|
||||
backgroundColor: Colors.transparent,
|
||||
elevation: 0,
|
||||
margin: EdgeInsets.zero,
|
||||
padding: EdgeInsets.zero,
|
||||
behavior: SnackBarBehavior.floating,
|
||||
duration: const Duration(seconds: 4),
|
||||
dismissDirection: DismissDirection.up,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Public API — drop-in replacements for the old functions
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
SnackbarController mySnackbarSuccess(String message) =>
|
||||
_show(_SnackVariant.success, message);
|
||||
|
||||
SnackbarController mySnackeBarError(String message) =>
|
||||
_show(_SnackVariant.error, message);
|
||||
|
||||
SnackbarController mySnackbarInfo(String message) =>
|
||||
_show(_SnackVariant.info, message);
|
||||
|
||||
SnackbarController mySnackbarWarning(String message) =>
|
||||
_show(_SnackVariant.warning, message);
|
||||
void mySnackbarSuccess(String message) => _show(_SnackVariant.success, message);
|
||||
void mySnackbarError(String message) => _show(_SnackVariant.error, message);
|
||||
void mySnackbarInfo(String message) => _show(_SnackVariant.info, message);
|
||||
void mySnackbarWarning(String message) => _show(_SnackVariant.warning, message);
|
||||
|
||||
Reference in New Issue
Block a user