show method

Future<UniqueKey> show({
  1. required BuildContext context,
  2. String title = 'Error',
  3. String? desc,
  4. DialogType type = DialogType.error,
})

Displays a custom dialog.

The dialog is shown as an OverlayEntry at the center of the screen.

Optionally takes a title, desc (description), and type of dialog. The type influences the appearance of the dialog.

Returns a Future that resolves with a UniqueKey identifying the displayed dialog. This key can be used to hide the specific dialog later.

Implementation

Future<UniqueKey> show({
  required BuildContext context,
  String title = 'Error',
  String? desc,
  DialogType type = DialogType.error,
}) async {
  try {
    FocusScope.of(context).unfocus();
    UniqueKey key = UniqueKey();

    final overlayState = GlueUI.instance.navigatorKey.currentState?.overlay ?? Overlay.maybeOf(context);

    if (overlayState == null) {
      throw CustomException(message: 'Could not find OverlayState to show dialog.');
    }

    SchedulerBinding.instance.addPostFrameCallback((_) {
      OverlayEntry overlayEntry = OverlayEntry(
        builder: (_) {
          return GestureDetector(
            onTap: () => hide(key),
            child: Container(
              height: context.screenSize.height,
              width: context.screenSize.width,
              decoration: BoxDecoration(
                color: Colors.black.withValues(alpha: .5),
              ),
              child: AnimatedDialogWidget(
                dialog: SMDialog(
                  context: context,
                  dialogType: type,
                  alignment: Alignment.center,
                  animType: AnimType.bottomSlide,
                  width: double.infinity,
                  transitionAnimationDuration: Duration(milliseconds: 200),
                  buttonsBorderRadius: BorderRadius.circular(10),
                  dismissOnTouchOutside: false,
                  dismissOnBackKeyPress: false,
                  headerAnimationLoop: false,
                  title: title,
                  desc: desc,
                  showCloseIcon: true,
                  onClose: () => hide(key),
                ),
              ),
            ),
          );
        },
      );
      overlayState.insert(overlayEntry);
      _dialogsStack.add(_DialogEntry(overlayEntry, key));
    });
    return key;
  } catch (e, s) {
    throw CustomException(
      message: 'An error occurred while showing the dialog.',
      hiddenMessage: '{\ne: $e\ns:$s\n}',
    );
  }
}