build method

  1. @override
Widget build(
  1. BuildContext context
)
override

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:

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 dynamic defaultStyle;
  if (vExt == null || vExt is VDef) {
    defaultStyle = Theme.of(context).brightness == Brightness.dark
        ? getDefaultColorSchemeDark()!
        : getDefaultColorScheme()!;
  } else {
    defaultStyle = Theme.of(context).brightness == Brightness.dark
        ? getAltColorSchemeDark()!
        : getAltColorScheme()!;
  }

  final backgroundColorDisabled =
      style?.backgroundColorDisabled ?? defaultStyle.disabled;
  final backgroundColorActive =
      style?.backgroundColorActive ?? defaultStyle.active;
  final foregroundColorDisabled =
      style?.foregroundColorDisabled ?? defaultStyle.surface1;
  final foregroundColorActive =
      style?.foregroundColorActive ?? defaultStyle.surface1;
  final overlayColorPressed =
      style?.overlayColorPressed ?? defaultStyle.activePressed;
  final overlayColorFocused = style?.overlayColorFocused;
  final shadowColor = style?.shadowColor ?? defaultStyle.transparent;
  final elevation =
      style?.elevation ?? defaultStyle.vButtonProperties?.elevation;
  final minimumSize =
      style?.minimumSize ?? defaultStyle.vButtonProperties?.minimumSize;
  final borderRadiusActive = style?.borderRadiusActive ??
      defaultStyle.vButtonProperties.borderRadiusActive;
  final borderRadiusDisabled = style?.borderRadiusDisabled ??
      defaultStyle.vButtonProperties.borderRadiusDisabled;
  final borderSideActive = style?.borderSideActive ??
      defaultStyle.vButtonProperties.borderSideActive;
  final borderSideDisabled = style?.borderSideDisabled ??
      defaultStyle.vButtonProperties.borderSideDisabled;
  final EdgeInsetsGeometry scaledPadding = ButtonStyleButton.scaledPadding(
    const EdgeInsetsDirectional.fromSTEB(16, 0, 16, 0),
    const EdgeInsetsDirectional.fromSTEB(8, 0, 8, 0),
    const EdgeInsetsDirectional.fromSTEB(4, 0, 4, 0),
    MediaQuery.textScalerOf(context).scale(1),
  );
  EdgeInsetsGeometry padding = style?.padding ?? scaledPadding;

  return ElevatedButton(
    onPressed: onPressed,
    style: ButtonStyle(
      padding: MaterialStateProperty.all(padding),
      backgroundColor: MaterialStateProperty.resolveWith<Color?>(
          (Set<MaterialState> states) {
        return states.contains(MaterialState.disabled)
            ? backgroundColorDisabled
            : backgroundColorActive;
      }),
      // Foreground color is the color for text and icon
      foregroundColor: MaterialStateProperty.resolveWith<Color?>(
          (Set<MaterialState> states) {
        return states.contains(MaterialState.disabled)
            ? foregroundColorDisabled
            : foregroundColorActive;
      }),
      // Overlay color is the color when having action
      overlayColor: MaterialStateProperty.resolveWith<Color?>(
          (Set<MaterialState> states) {
        if (states.contains(MaterialState.pressed)) {
          return overlayColorPressed;
        }
        if (states.contains(MaterialState.focused)) {
          return overlayColorFocused;
        }
        return null;
      }),
      // Make the button flat
      elevation: MaterialStateProperty.all(elevation),
      // Get rid of shadow
      shadowColor: MaterialStateProperty.all(shadowColor),
      // Figma size requirement
      minimumSize: MaterialStateProperty.all(minimumSize),
      // Figma border requirement
      shape: MaterialStateProperty.resolveWith<OutlinedBorder?>(
          (Set<MaterialState> states) {
        if (states.contains(MaterialState.disabled)) {
          return RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(borderRadiusDisabled),
            side: borderSideDisabled,
          );
        } else {
          return RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(borderRadiusActive),
            side: borderSideActive,
          );
        }
      }),
    ),
    child: child,
  );
}