show method

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

Displays the single-value picker dialog and returns a Future that resolves when the dialog is dismissed.

This method shows the single-value picker dialog on the screen and returns a Future that resolves to the selected item when the dialog is dismissed.

Example:

var selectedItem = await singleValuePickerDialog.show(context);
print('Selected item: $selectedItem');

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 Center(
        child: Material(
          elevation: elevation,
          borderRadius: BorderRadius.circular(dialogBorderRadius),
          child: ConstrainedBox(
            constraints: BoxConstraints(
              maxWidth: dialogWidth ??
                  (isPortrait ? screenSize.width : screenSize.height) * 0.85,
              minWidth: dialogWidth ??
                  (isPortrait ? screenSize.width : screenSize.height) * 0.85,
              maxHeight: dialogHeight ??
                  (isPortrait ? screenSize.height : screenSize.width) * 0.85,
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                if (title != null || titleWidget != null || showCloseIcon)
                  titleWidget ??
                      Container(
                        height: titleHeight,
                        padding: EdgeInsets.only(
                          left: showCloseIcon ? 2.0 : 20.0,
                          right: 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: Row(
                          children: [
                            if (showCloseIcon)
                              IconButton(
                                onPressed: () => Navigator.of(context).pop(),
                                icon: const Icon(
                                  Icons.clear,
                                  color: Colors.white,
                                ),
                              ),
                            if (showCloseIcon) const SizedBox(width: 5.0),
                            if (title != null)
                              Expanded(
                                child: Text(
                                  title!,
                                  style: titleStyle ??
                                      Theme.of(context)
                                          .textTheme
                                          .headlineMedium
                                          ?.copyWith(
                                            color: Colors.white,
                                          ),
                                ),
                              ),
                          ],
                        ),
                      ),
                Flexible(
                  fit: FlexFit.loose,
                  child: ListView.builder(
                    shrinkWrap: true,
                    itemCount: items.length,
                    itemBuilder: (context, index) {
                      return Padding(
                        padding: const EdgeInsets.symmetric(
                            horizontal: 20.0, vertical: 3.0),
                        child: InkWell(
                          borderRadius: BorderRadius.circular(8.0),
                          onTap: () =>
                              Navigator.of(context).pop(items[index]),
                          child: Container(
                            width: double.infinity,
                            padding:
                                const EdgeInsets.symmetric(horizontal: 10.0),
                            child: itemBuilder.call(
                                context, items[index], index),
                          ),
                        ),
                      );
                    },
                  ),
                ),
                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(),
                              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(),
                              child: dialogButton!.positiveButton!,
                            ),
                          ),
                      ],
                    ),
                  ),
              ],
            ),
          ),
        ),
      );
    },
  );
}