55 lines
1.5 KiB
Dart
55 lines
1.5 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import '../../../core/theme/app_colors.dart';
|
|
|
|
/// Shows a single SnackBar, replacing any current one to avoid duplicates.
|
|
void showAppSnackBar(
|
|
BuildContext context, {
|
|
required String message,
|
|
Color? backgroundColor,
|
|
Duration duration = const Duration(seconds: 4),
|
|
}) {
|
|
final messenger = ScaffoldMessenger.of(context);
|
|
messenger.hideCurrentSnackBar();
|
|
messenger.showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
message,
|
|
style: const TextStyle(color: Colors.white, fontWeight: FontWeight.w600),
|
|
),
|
|
backgroundColor: backgroundColor,
|
|
behavior: SnackBarBehavior.floating,
|
|
duration: duration,
|
|
),
|
|
);
|
|
}
|
|
|
|
void showErrorSnackBar(BuildContext context, String message) {
|
|
showAppSnackBar(context, message: message, backgroundColor: AppColors.danger);
|
|
}
|
|
|
|
void showSuccessSnackBar(BuildContext context, String message) {
|
|
showAppSnackBar(context, message: message, backgroundColor: AppColors.success);
|
|
}
|
|
|
|
/// True when this route is the top-most visible route (avoids stacked screens
|
|
/// reacting to the same AuthBloc feedback).
|
|
bool isCurrentRoute(BuildContext context) {
|
|
final route = ModalRoute.of(context);
|
|
return route?.isCurrent ?? true;
|
|
}
|
|
|
|
/// Auto-clears a local message via [setState] after [duration].
|
|
Timer? scheduleMessageClear(
|
|
void Function(VoidCallback fn) setState,
|
|
void Function() clear, {
|
|
Duration duration = const Duration(seconds: 4),
|
|
Timer? previous,
|
|
}) {
|
|
previous?.cancel();
|
|
return Timer(duration, () {
|
|
setState(clear);
|
|
});
|
|
}
|