showGlassBanner function

GlassBannerController showGlassBanner({
  1. required BuildContext context,
  2. required Widget title,
  3. Widget? message,
  4. Widget? leading,
  5. Widget? trailing,
  6. Duration duration = const Duration(seconds: 4),
  7. VoidCallback? onTap,
})

Shows a card of glass that drops in from the top of the screen, as iOS's notifications do: a leading icon beside a title over a message.

It goes again after duration, when swiped up, or when tapped, which also calls onTap. A new banner replaces one already showing.

Implementation

GlassBannerController showGlassBanner({
  required BuildContext context,
  required Widget title,
  Widget? message,
  Widget? leading,
  Widget? trailing,
  Duration duration = const Duration(seconds: 4),
  VoidCallback? onTap,
}) {
  _current?.dismiss();
  final controller = GlassBannerController._();
  _current = controller;
  final carried = GlassOverlayContext.of(context);
  final overlay = Overlay.of(context, rootOverlay: true);
  late final OverlayEntry entry;
  var removed = false;
  void gone() {
    if (removed) return;
    removed = true;
    if (entry.mounted) entry.remove();
    entry.dispose();
    if (identical(_current, controller)) _current = null;
    controller._finish();
  }

  entry = OverlayEntry(
    builder:
        (context) => carried.wrap(
          _GlassBanner(
            controller: controller,
            title: title,
            message: message,
            leading: leading,
            trailing: trailing,
            duration: duration,
            onTap: onTap,
            onGone: gone,
          ),
        ),
  );
  overlay.insert(entry);
  return controller;
}