show static method

void show(
  1. BuildContext context, {
  2. required String txt,
  3. required NGAMsgType type,
  4. void onTap()?,
  5. int s = 3,
  6. String tip = '',
})

Implementation

static void show(
  final BuildContext context, {
  required final String txt,
  required final NGAMsgType type,
  final void Function()? onTap,
  final int s = 3,
  final String tip = '',
}) {
  if (!context.mounted) return;
  final overlayState = Overlay.of(context);
  IconData icon;
  Color color;
  switch (type) {
    case NGAMsgType.err:
      icon = Icons.error_rounded;
      color = Colors.redAccent;
      break;
    case NGAMsgType.warn:
      icon = Icons.warning_rounded;
      color = Colors.orangeAccent;
      break;
    case NGAMsgType.ok:
      icon = Icons.check_circle_rounded;
      color = Colors.greenAccent;
      break;
    case NGAMsgType.info:
      icon = Icons.info_rounded;
      color = Colors.blueAccent;
      break;
  }
  OverlayEntry toastOverlayEntry(final Tween<Offset> tween) => OverlayEntry(
        builder: (final ctx) => Positioned(
          top: MediaQuery.of(ctx).size.height * 0.075,
          left: MediaQuery.of(ctx).size.width * 0.1,
          right: MediaQuery.of(ctx).size.width * 0.1,
          child: TweenAnimationBuilder(
            tween: tween,
            duration: const Duration(milliseconds: 300),
            builder: (final ctx, final Offset offset, final Widget? child) =>
                Transform.translate(offset: offset * MediaQuery.of(ctx).size.height, child: child),
            child: Center(
              child: ClipRRect(
                borderRadius: BorderRadius.circular(16),
                child: BackdropFilter(
                  filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
                  child: Material(
                    color: Colors.transparent,
                    borderRadius: BorderRadius.circular(16),
                    child: Tooltip(
                      message: tip,
                      child: InkWell(
                        onTap: onTap,
                        borderRadius: BorderRadius.circular(16),
                        child: Container(
                          padding: const EdgeInsets.all(16),
                          decoration: BoxDecoration(
                            color: Colors.white.withAlpha(128),
                            borderRadius: BorderRadius.circular(16),
                          ),
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Icon(icon, color: color),
                              const SizedBox(width: 12),
                              Text(txt, style: TextStyle(color: color)),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      );

  final goEntry = toastOverlayEntry(Tween(begin: const Offset(0, -1), end: Offset.zero));
  overlayState.insert(goEntry);
  Future<void>.delayed(Duration(seconds: s), () {
    final backEntry = toastOverlayEntry(Tween(begin: Offset.zero, end: const Offset(0, -1)));
    overlayState.insert(backEntry);
    goEntry.remove();
    Future<void>.delayed(const Duration(milliseconds: 300), backEntry.remove);
  });
}