1. override
Widget build(BuildContext context)

Describes the part of the user interface represented by this widget.

The framework calls this method in a number of different situations:

  • After calling initState.
  • After calling didUpdateConfig.
  • After receiving a call to setState.
  • After a dependency of this State object changes (e.g., an InheritedWidget referenced by the previous build changes).
  • After calling deactivate and then reinserting the State object into the tree at another location.

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, the given BuildContext, and the internal state of this State object.

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. The BuildContext argument is always the same as the context property of this State object and will remain the same for the lifetime of this object. The BuildContext argument is provided redundantly here so that this method matches the signature for a WidgetBuilder.

Source

@override
Widget build(BuildContext context) {
  EdgeInsets padding = MediaQuery.of(context).padding;
  ThemeData themeData = Theme.of(context);
  if (!config.resizeToAvoidBottomPadding)
    padding = new EdgeInsets.fromLTRB(padding.left, padding.top, padding.right, 0.0);

  if (_snackBars.isNotEmpty) {
    final ModalRoute<dynamic> route = ModalRoute.of(context);
    if (route == null || route.isCurrent) {
      if (_snackBarController.isCompleted && _snackBarTimer == null)
        _snackBarTimer = new Timer(_snackBars.first._widget.duration, () {
          assert(_snackBarController.status == AnimationStatus.forward ||
                 _snackBarController.status == AnimationStatus.completed);
          hideCurrentSnackBar(reason: SnackBarClosedReason.timeout);
        });
    } else {
      _snackBarTimer?.cancel();
      _snackBarTimer = null;
    }
  }

  final List<LayoutId> children = new List<LayoutId>();

  Widget body;
  if (config.appBarBehavior != AppBarBehavior.anchor) {
    body = new NotificationListener<ScrollNotification>(
      onNotification: _handleScrollNotification,
      child: config.body,
    );
  } else {
    body = config.body;
  }
  _addIfNonNull(children, body, _ScaffoldSlot.body);

  if (config.appBarBehavior == AppBarBehavior.anchor) {
    final double expandedHeight = (config.appBar?.expandedHeight ?? 0.0) + padding.top;
    final Widget appBar = new ConstrainedBox(
      constraints: new BoxConstraints(maxHeight: expandedHeight),
      child: _getModifiedAppBar(padding: padding)
    );
    _addIfNonNull(children, appBar, _ScaffoldSlot.appBar);
  } else {
    children.add(new LayoutId(child: _buildScrollableAppBar(context, padding), id: _ScaffoldSlot.appBar));
  }
  // Otherwise the AppBar will be part of a [app bar, body] Stack. See
  // AppBarBehavior.scroll below.

  if (_snackBars.isNotEmpty)
    _addIfNonNull(children, _snackBars.first._widget, _ScaffoldSlot.snackBar);

  if (config.persistentFooterButtons != null) {
    children.add(new LayoutId(
      id: _ScaffoldSlot.persistentFooter,
      child: new Container(
        decoration: new BoxDecoration(
          border: new Border(
            top: new BorderSide(
              color: themeData.dividerColor
            ),
          ),
        ),
        child: new ButtonTheme.bar(
          child: new ButtonBar(
            children: config.persistentFooterButtons
          ),
        ),
      ),
    ));
  }

  if (config.bottomNavigationBar != null) {
    children.add(new LayoutId(
      id: _ScaffoldSlot.bottomNavigationBar,
      child: config.bottomNavigationBar
    ));
  }

  if (_currentBottomSheet != null || _dismissedBottomSheets.isNotEmpty) {
    final List<Widget> bottomSheets = <Widget>[];
    if (_dismissedBottomSheets.isNotEmpty)
      bottomSheets.addAll(_dismissedBottomSheets);
    if (_currentBottomSheet != null)
      bottomSheets.add(_currentBottomSheet._widget);
    Widget stack = new Stack(
      children: bottomSheets,
      alignment: FractionalOffset.bottomCenter
    );
    _addIfNonNull(children, stack, _ScaffoldSlot.bottomSheet);
  }

  children.add(new LayoutId(
    id: _ScaffoldSlot.floatingActionButton,
    child: new _FloatingActionButtonTransition(
      child: config.floatingActionButton
    )
  ));

  if (themeData.platform == TargetPlatform.iOS) {
    children.add(new LayoutId(
      id: _ScaffoldSlot.statusBar,
      child: new GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: _handleStatusBarTap
      )
    ));
  }

  if (config.drawer != null) {
    children.add(new LayoutId(
      id: _ScaffoldSlot.drawer,
      child: new DrawerController(
        key: _drawerKey,
        child: config.drawer
      )
    ));
  } else if (_shouldHandleBackGesture()) {
    // Add a gesture for navigating back.
    children.add(new LayoutId(
      id: _ScaffoldSlot.drawer,
      child: new Align(
        alignment: FractionalOffset.centerLeft,
        child: new GestureDetector(
          key: _backGestureKey,
          onHorizontalDragStart: _handleDragStart,
          onHorizontalDragUpdate: _handleDragUpdate,
          onHorizontalDragEnd: _handleDragEnd,
          onHorizontalDragCancel: _handleDragCancel,
          behavior: HitTestBehavior.translucent,
          excludeFromSemantics: true,
          child: new Container(width: _kBackGestureWidth)
        )
      )
    ));
  }

  EdgeInsets appPadding = (config.appBarBehavior != AppBarBehavior.anchor) ? EdgeInsets.zero : padding;
  Widget application = new CustomMultiChildLayout(
    children: children,
    delegate: new _ScaffoldLayout(
      padding: appPadding,
      statusBarHeight: padding.top,
      appBarBehavior: config.appBarBehavior
    )
  );

  return new Material(
    color: config.backgroundColor ?? themeData.scaffoldBackgroundColor,
    child: application,
  );
}