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 spacing = context.streamSpacing;
final inheritedStyle = context.streamSplitButtonTheme.styleOf(props.variant);
final themeStyle = inheritedStyle?.merge(props.themeStyle) ?? props.themeStyle;
final buttonVariant = props.variant._buttonStyle;
// Resolved once and shared: the surface below and the halves above are the
// same button style, so they cannot render as different colors.
final buttonStyle = resolveStreamButtonThemeStyle(
context,
style: buttonVariant,
type: StreamButtonType.solid,
isFloating: false,
themeStyle: themeStyle?.buttonStyle,
);
final defaults = _StreamSplitButtonDefaults(context, props.variant);
final isEnabled = props.onLeadingPressed != null || props.onTrailingPressed != null;
final states = <WidgetState>{if (!isEnabled) WidgetState.disabled};
final shape = buttonStyle.shape?.resolve(states) ?? const StadiumBorder();
// Neither variant is outlined, but a theme may still ask for a border; when
// it does it is drawn once around the whole control rather than each half.
final borderColor = buttonStyle.borderColor?.resolve(states);
final effectiveSeparatorColor = (themeStyle?.separatorColor ?? defaults.separatorColor).resolve(states);
final effectiveSeparatorThickness = themeStyle?.separatorThickness ?? defaults.separatorThickness;
final effectiveSeparatorHeight = themeStyle?.separatorHeight ?? defaults.separatorHeight;
// The halves are inset by [inset] on every edge of the surface, which puts
// a pair of small buttons under a surface the height of a medium one.
final inset = spacing.xxs;
// The halves sit on the shared surface, so they paint neither their own
// background nor their own border. Their box is the painted circle; the tap
// target around it comes from [_HitTarget], since MaterialTapTargetSize can
// only grow both axes at once and the design grows only the height.
final halfStyle = buttonStyle.copyWith(
backgroundColor: const WidgetStatePropertyAll(StreamColors.transparent),
borderColor: const WidgetStatePropertyAll(null),
elevation: const WidgetStatePropertyAll(0),
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
);
Widget half({required Widget icon, required VoidCallback? onPressed, required String? tooltip}) {
// The merge lifts the half's semantics node up to the tap target, so
// assistive tech reports the region that actually responds to a tap
// rather than the smaller square the button paints.
return MergeSemantics(
child: _HitTarget(
minSize: Size(_halfButtonSize.value, kMinInteractiveDimension),
child: StreamButton.icon(
icon: icon,
onPressed: onPressed,
style: buttonVariant,
size: _halfButtonSize,
tooltip: tooltip,
themeStyle: halfStyle,
),
),
);
}
return Padding(
// We only add some horizontal padding to match the extra vertical padding from the _HitTarget
padding: EdgeInsets.symmetric(horizontal: inset),
child: Stack(
alignment: Alignment.center,
children: [
// Painted behind the halves rather than around them: their tap targets
// are taller than the surface and overhang it top and bottom.
Positioned.fill(
child: Center(
child: SizedBox(
width: double.infinity,
height: _halfButtonSize.value + inset * 2,
child: DecoratedBox(
decoration: ShapeDecoration(
color: buttonStyle.backgroundColor?.resolve(states),
shape: switch (borderColor) {
final color? => shape.copyWith(side: BorderSide(color: color)),
_ => shape,
},
),
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: inset),
child: Row(
mainAxisSize: MainAxisSize.min,
spacing: inset,
children: [
half(icon: props.leadingIcon, onPressed: props.onLeadingPressed, tooltip: props.leadingTooltip),
SizedBox(
width: effectiveSeparatorThickness,
height: effectiveSeparatorHeight,
child: ColoredBox(color: effectiveSeparatorColor ?? StreamColors.transparent),
),
half(
icon: props.trailingIcon,
onPressed: props.onTrailingPressed,
tooltip: props.trailingTooltip,
),
],
),
),
],
),
);
}