build method
Describes the part of the user interface represented by this widget.
The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.
The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.
Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.
The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.
The implementation of this method must only depend on:
- the fields of the widget, which themselves must not change over time, and
- any ambient state obtained from the
contextusing BuildContext.dependOnInheritedWidgetOfExactType.
If a widget's build method is to depend on anything else, use a StatefulWidget instead.
See also:
- StatelessWidget, which contains the discussion on performance considerations.
Implementation
@override
Widget build(BuildContext context) {
final theme = ShadTheme.of(context);
final effectiveDialogTheme = switch (variant) {
ShadDialogVariant.primary => theme.primaryDialogTheme,
ShadDialogVariant.alert => theme.alertDialogTheme,
};
final effectiveBackgroundColor =
backgroundColor ??
effectiveDialogTheme.backgroundColor ??
theme.colorScheme.background;
final effectiveCloseIcon =
closeIcon ??
(closeIconData == null && effectiveDialogTheme.closeIconData == null
? null
: ShadIconButton.ghost(
icon: Icon(
size: 16,
closeIconData ??
effectiveDialogTheme.closeIconData ??
LucideIcons.x,
),
width: 20,
height: 20,
padding: EdgeInsets.zero,
foregroundColor: theme.colorScheme.foreground.withValues(
alpha: .5,
),
hoverBackgroundColor: const Color(0x00000000),
hoverForegroundColor: theme.colorScheme.foreground,
pressedForegroundColor: theme.colorScheme.foreground,
onPressed: () => Navigator.of(context).pop(),
));
final effectiveCloseIconPosition =
closeIconPosition ??
effectiveDialogTheme.closeIconPosition ??
ShadPosition.directional(
top: 8,
end: 8,
textDirection: Directionality.of(context),
);
final effectiveRadius =
radius ?? effectiveDialogTheme.radius ?? theme.radius;
final effectiveExpandActionsWhenTiny =
expandActionsWhenTiny ??
effectiveDialogTheme.expandActionsWhenTiny ??
true;
final effectiveConstraints =
constraints ??
effectiveDialogTheme.constraints ??
const BoxConstraints(maxWidth: 512);
final effectiveBorder =
border ??
effectiveDialogTheme.border ??
Border.all(color: theme.colorScheme.border);
final effectiveShadows =
shadows ?? effectiveDialogTheme.shadows ?? ShadShadows.lg;
final effectiveRemoveBorderRadiusWhenTiny =
removeBorderRadiusWhenTiny ??
effectiveDialogTheme.removeBorderRadiusWhenTiny ??
true;
final effectivePadding =
padding ?? effectiveDialogTheme.padding ?? const EdgeInsets.all(24);
final effectiveGap = gap ?? effectiveDialogTheme.gap ?? 8;
final effectiveTitleStyle =
(titleStyle ?? effectiveDialogTheme.titleStyle ?? theme.textTheme.large)
.fallback(color: theme.colorScheme.foreground);
final effectiveDescriptionStyle =
(descriptionStyle ??
effectiveDialogTheme.descriptionStyle ??
theme.textTheme.muted)
.fallback(
color: theme.colorScheme.mutedForeground,
);
final effectiveAlignment =
alignment ?? effectiveDialogTheme.alignment ?? Alignment.center;
final effectiveMainAxisAlignment =
mainAxisAlignment ??
effectiveDialogTheme.mainAxisAlignment ??
MainAxisAlignment.start;
final effectiveCrossAxisAlignment =
crossAxisAlignment ??
effectiveDialogTheme.crossAxisAlignment ??
CrossAxisAlignment.start;
final effectiveScrollable =
scrollable ?? effectiveDialogTheme.scrollable ?? true;
final effectiveScrollPadding =
scrollPadding ?? effectiveDialogTheme.scrollPadding;
final effectiveActionsGap =
actionsGap ?? effectiveDialogTheme.actionsGap ?? 8;
final effectiveUseSafeArea =
useSafeArea ?? effectiveDialogTheme.useSafeArea ?? true;
final effectiveTitlePinned =
titlePinned ?? effectiveDialogTheme.titlePinned ?? false;
final effectiveDescriptionPinned =
descriptionPinned ?? effectiveDialogTheme.descriptionPinned ?? false;
final effectiveActionsPinned =
actionsPinned ?? effectiveDialogTheme.actionsPinned ?? true;
final dialog = ConstrainedBox(
constraints: effectiveConstraints,
child: ShadResponsiveBuilder(
builder: (context, breakpoint) {
final sm = breakpoint >= theme.breakpoints.sm;
final effectiveActionsAxis =
actionsAxis ??
effectiveDialogTheme.actionsAxis ??
(sm ? Axis.horizontal : Axis.vertical);
final effectiveActionsMainAxisSize =
actionsMainAxisSize ??
effectiveDialogTheme.actionsMainAxisSize ??
MainAxisSize.min;
final effectiveActionsMainAxisAlignment =
actionsMainAxisAlignment ??
effectiveDialogTheme.actionsMainAxisAlignment ??
MainAxisAlignment.end;
final effectiveActionsVerticalDirection =
actionsVerticalDirection ??
effectiveDialogTheme.actionsVerticalDirection ??
(sm ? VerticalDirection.down : VerticalDirection.up);
final effectiveTitleTextAlign =
titleTextAlign ??
effectiveDialogTheme.titleTextAlign ??
(sm ? TextAlign.start : TextAlign.center);
final effectiveDescriptionTextAlign =
descriptionTextAlign ??
effectiveDialogTheme.descriptionTextAlign ??
(sm ? TextAlign.start : TextAlign.center);
Widget? effectiveActions = actions.isEmpty
? null
: BoxyFlexible.align(
crossAxisAlignment: CrossAxisAlignment.end,
child: Flex(
direction: effectiveActionsAxis,
mainAxisSize: effectiveActionsMainAxisSize,
mainAxisAlignment: effectiveActionsMainAxisAlignment,
verticalDirection: effectiveActionsVerticalDirection,
spacing: effectiveActionsGap,
children: actions,
),
);
if (!sm &&
effectiveExpandActionsWhenTiny &&
effectiveActions != null) {
effectiveActions = ShadTheme(
data: theme.copyWith(
primaryButtonTheme: theme.primaryButtonTheme.copyWith(
width: double.infinity,
),
secondaryButtonTheme: theme.secondaryButtonTheme.copyWith(
width: double.infinity,
),
outlineButtonTheme: theme.outlineButtonTheme.copyWith(
width: double.infinity,
),
ghostButtonTheme: theme.ghostButtonTheme.copyWith(
width: double.infinity,
),
destructiveButtonTheme: theme.destructiveButtonTheme.copyWith(
width: double.infinity,
),
),
child: effectiveActions,
);
}
final effectiveTitle = title != null
? DefaultTextStyle(
style: effectiveTitleStyle,
textAlign: effectiveTitleTextAlign,
child: title!,
)
: null;
final effectiveDescription = description != null
? DefaultTextStyle(
style: effectiveDescriptionStyle,
textAlign: effectiveDescriptionTextAlign,
child: description!,
)
: null;
final effectiveChild = child != null
? DefaultTextStyle(
style: effectiveDescriptionStyle,
child: child!,
)
: null;
Widget widget = BoxyColumn(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: effectiveMainAxisAlignment,
crossAxisAlignment: effectiveCrossAxisAlignment,
children: [
// Only show title if not scrollable or scrollable but not pinned
if (effectiveTitle != null &&
(!effectiveScrollable || !effectiveTitlePinned))
effectiveTitle,
// Only show description if not scrollable or scrollable but not
// pinned
if (effectiveDescription != null &&
(!effectiveScrollable || !effectiveDescriptionPinned))
effectiveDescription,
if (effectiveChild != null) Flexible(child: effectiveChild),
// Only show actions if not scrollable or scrollable but not
// pinned
if (effectiveActions != null &&
(!effectiveScrollable || !effectiveActionsPinned))
effectiveActions,
].separatedBy(SizedBox(height: effectiveGap)),
);
if (effectiveScrollable) {
widget = BoxyColumn(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: effectiveMainAxisAlignment,
crossAxisAlignment: effectiveCrossAxisAlignment,
children: [
// Pinned title
if (effectiveTitle != null && effectiveTitlePinned)
effectiveTitle,
// Pinned description
if (effectiveDescription != null && effectiveDescriptionPinned)
effectiveDescription,
Flexible(
child: SingleChildScrollView(
padding: effectiveScrollPadding,
child: widget,
),
),
// Pinned actions
if (effectiveActions != null && effectiveActionsPinned)
effectiveActions,
].separatedBy(SizedBox(height: effectiveGap)),
);
}
widget = Stack(
children: [
Padding(
padding: effectivePadding,
child: widget,
),
if (effectiveCloseIcon != null)
effectiveCloseIcon.positionedWith(effectiveCloseIconPosition),
],
);
if (effectiveUseSafeArea) {
widget = SafeArea(child: widget);
}
return DecoratedBox(
decoration: BoxDecoration(
color: effectiveBackgroundColor,
borderRadius: (!sm && effectiveRemoveBorderRadiusWhenTiny)
? null
: effectiveRadius,
border: effectiveBorder,
boxShadow: effectiveShadows,
),
child: widget,
);
},
),
);
// Get the current view padding
final viewPadding = MediaQuery.viewInsetsOf(context);
return Align(
alignment: effectiveAlignment,
child: Padding(
padding: viewPadding,
child: dialog,
),
);
}