displayInfoBar function

Future<void> displayInfoBar(
  1. BuildContext context, {
  2. required InfoBarPopupBuilder builder,
  3. Alignment alignment = Alignment.bottomCenter,
  4. Duration duration = const Duration(seconds: 3),
})

Displays an InfoBar as a popup

duration is the duration that the popup will be in the screen

displayInfoBar(
  context,
  builder: (context, close) {
    return InfoBar(
      title: Text('Title'),
      action: IconButton(
        icon: const WindowsIcon(WindowsIcons.chrome_close),
        onPressed: close,
      ),
    );
  }
)

Implementation

Future<void> displayInfoBar(
  BuildContext context, {
  required InfoBarPopupBuilder builder,
  Alignment alignment = Alignment.bottomCenter,
  Duration duration = const Duration(seconds: 3),
}) async {
  assert(debugCheckHasOverlay(context));
  assert(debugCheckHasFluentTheme(context));

  final theme = FluentTheme.of(context);

  late OverlayEntry entry;

  var isFading = true;

  var alreadyInitialized = false;

  entry = OverlayEntry(
    builder: (context) {
      return SafeArea(
        child: Align(
          alignment: alignment,
          child: Padding(
            padding: const EdgeInsetsDirectional.symmetric(
              vertical: 24,
              horizontal: 16,
            ),
            child: StatefulBuilder(
              builder: (context, setState) {
                Future<void> close() async {
                  if (!entry.mounted) return;
                  setState(() => isFading = true);
                  await Future<void>.delayed(theme.mediumAnimationDuration);
                  if (!entry.mounted) return;
                  entry.remove();
                }

                if (!alreadyInitialized) {
                  alreadyInitialized = true;
                  () async {
                    await Future<void>.delayed(theme.mediumAnimationDuration);
                    if (!entry.mounted) return;
                    setState(() => isFading = false);
                    await Future<void>.delayed(duration);
                    await close();
                  }();
                }

                return AnimatedSwitcher(
                  duration: theme.mediumAnimationDuration,
                  switchInCurve: theme.animationCurve,
                  switchOutCurve: theme.animationCurve,
                  child: isFading
                      ? const SizedBox.shrink()
                      : PhysicalModel(
                          color: Colors.transparent,
                          elevation: 8,
                          child: builder(context, close),
                        ),
                );
              },
            ),
          ),
        ),
      );
    },
  );

  Overlay.of(context).insert(entry);
}