build method

  1. @override
Widget build(
  1. BuildContext _
)
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 _) {
  return LayoutBuilder(
    builder: (_, constraints) {
      return ZetaThemeOverride(
        themeMode: ThemeMode.dark,
        builder: (context) {
          final zeta = Zeta.of(context);
          final colors = zeta.colors;

          return ZetaRoundedScope(
            rounded: rounded ?? zeta.rounded,

            // Main container (for padding and background color)
            child: Container(
              height: zeta.spacing.small + zeta.spacing.xl_5 + zeta.spacing.small,
              padding: EdgeInsets.symmetric(horizontal: zeta.spacing.large),
              color: colors.surfaceDefault,

              // Main row (for all header content)
              child: Row(
                spacing: zeta.spacing.large,
                children: [
                  // Leading icon widget
                  ConstrainedBox(
                    constraints: BoxConstraints(maxWidth: zeta.spacing.xl_6, maxHeight: zeta.spacing.xl_6),
                    child: leading ??
                        ZetaIconButton(
                          icon: ZetaIcons.hamburger_menu,
                          onPressed: onHamburgerMenuPressed,
                          type: ZetaButtonType.subtle,
                          semanticLabel: leadingSemanticLabel,
                        ),
                  ),

                  // Logo
                  if (logo != null)
                    logo!
                  else
                    SvgPicture.asset(
                      'packages/zeta_flutter/assets/logos/zebra-logo.svg',
                      height: zeta.spacing.xl_4,
                      semanticsLabel: 'Zebra Logo',
                      colorFilter: ColorFilter.mode(const ZetaPrimitivesLight().pure.shade0, BlendMode.srcIn),
                    ),

                  // Platform name
                  Text(platformName, style: zeta.textStyles.titleMedium.apply(color: colors.mainDefault)),

                  Expanded(
                    child: Row(
                      children: [
                        if (navItems.isNotEmpty)
                          // Divider
                          Container(
                            key: const Key('divider-menu-items'),
                            width: ZetaBorders.small,
                            height: zeta.spacing.xl_5,
                            color: colors.borderDefault,
                          ),

                        // Nav items
                        // TODO(UX-1520): Remove IntrinsicWidth and replace with better solution
                        Row(
                          children: [
                            for (final item in navItems.take(6)) IntrinsicWidth(child: item.renderSubtle),
                          ],
                        ),

                        // Spacer dividing left and right side of header
                        const Expanded(child: Nothing()),

                        // Search bar
                        if (searchBar != null &&
                            !((navItems.length == 6 && actionItems.length == 6) && constraints.maxWidth <= 1440))
                          Container(
                            constraints:
                                BoxConstraints(maxWidth: zeta.spacing.xl_8 * 5, minWidth: zeta.spacing.xl_11),
                            margin: EdgeInsets.only(left: zeta.spacing.small),
                            child: (searchBar! is ZetaSearchBar &&
                                    (searchBar! as ZetaSearchBar).size != ZetaWidgetSize.small
                                ? (searchBar! as ZetaSearchBar).copyWith(
                                    size: ZetaWidgetSize.small,
                                    shape:
                                        rounded ?? zeta.rounded ? ZetaWidgetBorder.rounded : ZetaWidgetBorder.sharp,
                                  )
                                : searchBar),
                          ),

                        // Action items
                        // TODO(UX-1520): Remove IntrinsicWidth and replace with better solution
                        Row(
                          children: [
                            for (final item in actionItems.take(6)) IntrinsicWidth(child: item.renderSubtle),
                          ],
                        ),
                        // Divider
                        if (actionItems.isNotEmpty)
                          Container(
                            key: const Key('divider-action-items'),
                            width: ZetaBorders.small,
                            height: zeta.spacing.xl_5,
                            color: colors.borderDefault,
                          ),
                      ],
                    ),
                  ),

                  ColoredBox(
                    color: zeta.colors.surfaceDefault,
                    child: Row(
                      children: [
                        // Avatar button
                        ZetaButton(
                          label: userName ?? '',
                          type: ZetaButtonType.subtle,
                          onPressed: onAvatarButtonPressed,
                          trailingIcon: ZetaIcons.expand_more,
                          semanticLabel: avatarSemanticLabel,
                          child: avatar is ZetaAvatar
                              ? (avatar! as ZetaAvatar).copyWith(size: ZetaAvatarSize.xxxs)
                              : ZetaAvatar.fromName(
                                  name: userName ?? '',
                                  size: ZetaAvatarSize.xxxs,
                                  backgroundColor: colors.avatarPurple,
                                ),
                        ),
                        ConstrainedBox(
                          constraints: BoxConstraints(maxWidth: zeta.spacing.xl_6, maxHeight: zeta.spacing.xl_6),
                          child: trailing ??
                              ZetaIconButton(
                                icon: ZetaIcons.apps,
                                onPressed: onAppsButtonPressed,
                                type: ZetaButtonType.subtle,
                                semanticLabel: trailingSemanticLabel,
                              ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          );
        },
      );
    },
  );
}