show method

Future<T?> show(
  1. BuildContext context
)

Displays the dialog and returns a Future that resolves when the dialog is dismissed.

This method shows the dialog on the screen and returns a Future that resolves to the value returned by the dialog (of type T).

Example:

var dialog = PrimaryDialog<bool>(
  title: 'Confirmation',
  // ... other parameters ...
);

bool result = await dialog.show(context);
print(result); // true if positive button is pressed, false if negative or dialog is dismissed

Implementation

Future<T?> show(BuildContext context) async {
  Size screenSize = MediaQuery.of(context).size;
  bool isPortrait =
      MediaQuery.of(context).orientation == Orientation.portrait;
  return await showDialog<T>(
    context: context,
    barrierDismissible: barrierDismissible,
    builder: (context) {
      return Padding(
        padding: const EdgeInsets.symmetric(vertical: 50.0),
        child: Center(
          child: Material(
            elevation: elevation,
            borderRadius: BorderRadius.circular(dialogBorderRadius),
            child: SizedBox(
              width: dialogWidth ??
                  (isPortrait ? screenSize.width : screenSize.height) * 0.85,
              height: dialogHeight,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    height: titleHeight,
                    padding: const EdgeInsets.symmetric(horizontal: 20.0),
                    decoration: BoxDecoration(
                      color: titleBackgroundColor ??
                          Theme.of(context).colorScheme.primary,
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(dialogBorderRadius),
                        topRight: Radius.circular(dialogBorderRadius),
                      ),
                    ),
                    alignment: Alignment.centerLeft,
                    child: titleWidget ??
                        Text(
                          title,
                          style: titleStyle ??
                              Theme.of(context)
                                  .textTheme
                                  .headlineMedium
                                  ?.copyWith(
                                    color: Colors.white,
                                  ),
                        ),
                  ),
                  if (description != null)
                    Padding(
                      padding: const EdgeInsets.symmetric(
                        vertical: 10.0,
                        horizontal: 20.0,
                      ),
                      child: Text(
                        description!,
                        style: descriptionStyle ??
                            Theme.of(context).textTheme.bodySmall,
                      ),
                    ),
                  if (body != null)
                    Flexible(
                      fit: FlexFit.loose,
                      child: SingleChildScrollView(
                        physics: bodyScrollPhysics,
                        child: body!,
                      ),
                    ),
                  if (dialogButton?.positiveButton != null ||
                      dialogButton?.negativeButton != null)
                    Padding(
                      padding: const EdgeInsets.all(20.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          if (dialogButton!.negativeButton != null)
                            Flexible(
                              fit: FlexFit.loose,
                              child: InkWell(
                                onTap: dialogButton!
                                        .onNegativeButtonPressed ??
                                    () => Navigator.of(context).pop(false),
                                child: dialogButton!.negativeButton!,
                              ),
                            ),
                          const SizedBox(width: 10.0),
                          if (dialogButton!.positiveButton != null)
                            Flexible(
                              fit: FlexFit.loose,
                              child: InkWell(
                                onTap:
                                    dialogButton!.onPositiveButtonPressed ??
                                        () => Navigator.of(context).pop(true),
                                child: dialogButton!.positiveButton!,
                              ),
                            ),
                        ],
                      ),
                    ),
                ],
              ),
            ),
          ),
        ),
      );
    },
  );
}