build method

  1. @override
Widget build (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).

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:

Implementation

@override
Widget build(BuildContext context) {
  final List<Widget> stacked = <Widget>[];

  Widget paddedContent = child;
  if (navigationBar != null) {
    final MediaQueryData existingMediaQuery = MediaQuery.of(context);

    // TODO(xster): Use real size after partial layout instead of preferred size.
    // https://github.com/flutter/flutter/issues/12912
    final double topPadding =
        navigationBar.preferredSize.height + existingMediaQuery.padding.top;

    // Propagate bottom padding and include viewInsets if appropriate
    final double bottomPadding = resizeToAvoidBottomInset
        ? existingMediaQuery.viewInsets.bottom
        : 0.0;

    // If navigation bar is opaquely obstructing, directly shift the main content
    // down. If translucent, let main content draw behind navigation bar but hint the
    // obstructed area.
    if (navigationBar.fullObstruction) {
      paddedContent = Padding(
        padding: EdgeInsets.only(top: topPadding, bottom: bottomPadding),
        child: child,
      );
    } else {
      paddedContent = MediaQuery(
        data: existingMediaQuery.copyWith(
          padding: existingMediaQuery.padding.copyWith(
            top: topPadding,
          ),
        ),
        child: Padding(
          padding: EdgeInsets.only(bottom: bottomPadding),
          child: child,
        ),
      );
    }
  }

  // The main content being at the bottom is added to the stack first.
  stacked.add(paddedContent);

  if (navigationBar != null) {
    stacked.add(Positioned(
      top: 0.0,
      left: 0.0,
      right: 0.0,
      child: navigationBar,
    ));
  }

  return DecoratedBox(
    decoration: BoxDecoration(color: backgroundColor),
    child: Stack(
      children: stacked,
    ),
  );
}