Skip to content

Instantly share code, notes, and snippets.

@reduxdj
Created November 16, 2024 06:33
Show Gist options
  • Save reduxdj/872979e154139b7477ddbac65c065c6e to your computer and use it in GitHub Desktop.
Save reduxdj/872979e154139b7477ddbac65c065c6e to your computer and use it in GitHub Desktop.
class TopAppMessage {
static RxBool toastShowingNow = false.obs;
static OverlayEntry? _overlayEntry;
static void showProgressBarToast({
required BuildContext context,
required String text,
String? btnTitle,
void Function()? onTap,
Duration duration = const Duration(seconds: 4),
String semanticsLabelButton = "",
String semanticsLabelText = "",
bool showProgressIndicator = true,
}) {
toastShowingNow.value = true;
DateTime? dialClickTime;
bool isRedundantClick(DateTime currentTime) {
if (dialClickTime == null) {
dialClickTime = currentTime;
return false;
}
if (currentTime.difference(dialClickTime!).inMilliseconds < 150) {
return true;
}
dialClickTime = currentTime;
return false;
}
_overlayEntry = OverlayEntry(
builder: (context) => Positioned(
top: 0,
left: 0,
right: 0,
child: Material(
color: Colors.transparent, // Ensure transparency
child: MaterialBanner(
backgroundColor: CColors.dark2,
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TimingProgressBar(
duration: duration,
showProgressIndicator: showProgressIndicator,
),
Padding(
padding: const EdgeInsets.only(
top: 10, left: 20, right: 10, bottom: 14),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Semantics(
explicitChildNodes: true,
label: semanticsLabelText,
child: Text(
text,
style: const InterReg(size: 16, color: CColors.white2),
),
),
),
const SizedBox(width: 20),
if (btnTitle != null)
AppButton.primary(
title: btnTitle,
semanticsLabel: semanticsLabelButton,
size: ButtonSize.small,
onTap: onTap ??
() {
if (isRedundantClick(DateTime.now())) return;
hideCurrentToast();
},
)
],
),
),
],
),
actions: const [SizedBox.shrink()], // Placeholder for actions
),
),
),
);
Overlay.of(context)?.insert(_overlayEntry!);
// Hide the banner after the specified duration
Future.delayed(duration, () {
hideCurrentToast();
toastShowingNow.value = false;
});
}
static void hideCurrentToast() {
_overlayEntry?.remove();
_overlayEntry = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment