show method

void show(
  1. BuildContext context
)

Displays the configured sheet.

Implementation

void show(BuildContext context) {
  // Throw only if this is an expandable created for in-route embedding
  if (_type == _SheetType.expandable && !_presentAsRoute) {
    throw Exception(
      'Use buildExpandable(context) when presentAsRoute is false.',
    );
  }

  showModalBottomSheet(
    backgroundColor: Colors.transparent,
    isDismissible: barrierDismissible,
    barrierColor: barrierColor,
    isScrollControlled: true,
    useSafeArea: false,
    enableDrag: enableDrag,
    // modal-level drag (close by swipe from top edge)
    context: context,
    builder: (context) {
      switch (_type) {
        case _SheetType.standard:
          if (!enableDrag) {
            // ---- STATIC default sheet (sizes ignored; intrinsic up to a cap) ----
            final init = initialChildSize.clamp(minChildSize, maxChildSize);
            return SafeArea(
              top: false,
              child: LayoutBuilder(
                builder: (context, viewport) {
                  final cap = viewport.maxHeight * init;

                  return Container(
                    width: double.infinity,
                    decoration: BoxDecoration(
                      borderRadius: const BorderRadius.vertical(
                        top: Radius.circular(10),
                      ),
                      color: backgroundColor ?? Theme.of(context).cardColor,
                    ),
                    child: SizedBox(
                      height: cap, // <- give a tight, finite height
                      child: Column(
                        mainAxisSize: MainAxisSize.max,
                        children: [
                          if (showDragHandle &&
                              handleColor != Colors.transparent)
                            _buildHandle(handleColor),
                          // Body gets the remaining space; it may contain its own Expanded safely.
                          Expanded(
                            child: Padding(
                              padding:
                                  sheetPadding ??
                                  const EdgeInsets.symmetric(
                                    horizontal: 30,
                                    vertical: 20,
                                  ),
                              child: _standardBody!(scrollController: null),
                            ),
                          ),
                          if (showDefaultButtons)
                            _buildButtons(
                              context,
                              firstButtonColor,
                              secondButtonColor,
                              firstButtonTextColor,
                              secondButtonTextColor,
                              buttonSpacing,
                              confirmButtonText,
                              cancelButtonText,
                            ),
                        ],
                      ),
                    ),
                  );
                },
              ),
            );
          } else {
            // ---- DRAGGABLE default sheet (uses exposed sizes) ----
            final init = initialChildSize.clamp(minChildSize, maxChildSize);
            return DraggableScrollableSheet(
              expand: true,
              initialChildSize: init,
              minChildSize: minChildSize,
              maxChildSize: maxChildSize,
              builder: (context, scrollController) {
                return Container(
                  decoration: BoxDecoration(
                    borderRadius: const BorderRadius.vertical(
                      top: Radius.circular(10),
                    ),
                    color: backgroundColor ?? Theme.of(context).cardColor,
                  ),
                  child: SafeArea(
                    top: false,
                    child: Column(
                      children: [
                        if (showDragHandle &&
                            handleColor != Colors.transparent)
                          _buildHandle(handleColor),
                        // Exactly one scrollable using the provided controller.
                        Expanded(
                          child: SingleChildScrollView(
                            controller: scrollController,
                            padding:
                                sheetPadding ??
                                const EdgeInsets.symmetric(
                                  horizontal: 30,
                                  vertical: 20,
                                ),
                            child: _standardBody!(
                              scrollController: scrollController,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                );
              },
            );
          }

        case _SheetType.confirm:
          // (unchanged) always static; confirm-layout buttons live here
          return SafeArea(
            child: Container(
              constraints: BoxConstraints(
                maxHeight: MediaQuery.of(context).size.height * 0.9,
              ),
              decoration: BoxDecoration(
                borderRadius: const BorderRadius.vertical(
                  top: Radius.circular(10),
                ),
                color: backgroundColor ?? Theme.of(context).cardColor,
              ),
              padding: padding ?? const EdgeInsets.only(bottom: 16.0),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  if (showDragHandle && handleColor != Colors.transparent)
                    _buildHandle(handleColor),
                  Flexible(
                    child: SingleChildScrollView(
                      padding:
                          sheetPadding ??
                          const EdgeInsets.symmetric(
                            horizontal: 30,
                            vertical: 20,
                          ),
                      child: _confirmBody!,
                    ),
                  ),
                  if (showDefaultButtons)
                    _buildButtons(
                      context,
                      firstButtonColor,
                      secondButtonColor,
                      firstButtonTextColor,
                      secondButtonTextColor,
                      buttonSpacing,
                      confirmButtonText,
                      cancelButtonText,
                    ),
                ],
              ),
            ),
          );
        // ------------------------ DRAGGABLE SCROLLABLE ------------------------
        case _SheetType.scrollable:
          return DraggableScrollableSheet(
            expand: expand,
            initialChildSize: initialChildSize,
            minChildSize: minChildSize,
            maxChildSize: maxChildSize,
            builder: (context, scrollController) {
              return Container(
                decoration: BoxDecoration(
                  borderRadius: const BorderRadius.vertical(
                    top: Radius.circular(10),
                  ),
                  color: backgroundColor ?? Theme.of(context).cardColor,
                ),
                child: SafeArea(
                  top: false,
                  child: Column(
                    children: [
                      if (handleColor != Colors.transparent)
                        _buildHandle(handleColor),
                      Expanded(
                        child: Padding(
                          padding: sheetPadding ?? EdgeInsets.zero,
                          child: _standardBody!(
                            scrollController: scrollController,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              );
            },
          );

        // ------------------------ EXPANDABLE OVERLAY ------------------------
        case _SheetType.expandable:
          return _ExpandableSheet(
            backgroundColor: backgroundColor,
            handleColor: handleColor,
            sheetPadding: sheetPadding,
            showDragHandle: showDragHandle,
            header: _expandableHeader!,
            bodyBuilder: _expandableBody!,
            footer: _expandableFooter!,
            controller: _expandableController ?? ExpandableController(),
            minChildSize: minChildSize,
            initialChildSize: initialChildSize,
            maxChildSize: maxChildSize,
            onDismiss: () => Navigator.of(context).maybePop(),
          );
      }
    },
  ).then((value) => onClose?.call(value));
}