build method Null safety
- BuildContext context
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
context
using 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 Color dividerColor = CupertinoColors.separator.resolveFrom(context);
final double dividerHeight = 1.0 / MediaQuery.of(context).devicePixelRatio;
// Long divider is used for wrapping the top and bottom of rows.
// Only used in CupertinoListSectionType.base mode.
final Widget longDivider = Container(
color: dividerColor,
height: dividerHeight,
);
// Short divider is used between rows.
final Widget shortDivider = Container(
margin: EdgeInsetsDirectional.only(
start: dividerMargin + additionalDividerMargin),
color: dividerColor,
height: dividerHeight,
);
Widget? headerWidget;
if (header != null) {
headerWidget = DefaultTextStyle(
style: CupertinoTheme.of(context).textTheme.textStyle.merge(
type == CupertinoListSectionType.base
? TextStyle(
fontSize: 13.0,
color: CupertinoDynamicColor.resolve(
_kHeaderFooterColor, context))
: const TextStyle(
fontSize: 20.0, fontWeight: FontWeight.bold),
),
child: header!,
);
}
Widget? footerWidget;
if (footer != null) {
footerWidget = DefaultTextStyle(
style: type == CupertinoListSectionType.base
? CupertinoTheme.of(context).textTheme.textStyle.merge(TextStyle(
fontSize: 13.0,
color: CupertinoDynamicColor.resolve(
_kHeaderFooterColor, context),
))
: CupertinoTheme.of(context).textTheme.textStyle,
child: footer!,
);
}
BorderRadius? childrenGroupBorderRadius;
DecoratedBox? decoratedChildrenGroup;
if (children != null && children!.isNotEmpty) {
// We construct childrenWithDividers as follows:
// Insert a short divider between all rows.
// If it is a `CupertinoListSectionType.base` type, add a long divider
// to the top and bottom of the rows.
final List<Widget> childrenWithDividers = <Widget>[];
if (type == CupertinoListSectionType.base) {
childrenWithDividers.add(longDivider);
}
children!.sublist(0, children!.length - 1).forEach((Widget widget) {
childrenWithDividers.add(widget);
childrenWithDividers.add(shortDivider);
});
childrenWithDividers.add(children!.last);
if (type == CupertinoListSectionType.base) {
childrenWithDividers.add(longDivider);
}
switch (type) {
case CupertinoListSectionType.insetGrouped:
childrenGroupBorderRadius = _kDefaultInsetGroupedBorderRadius;
break;
case CupertinoListSectionType.base:
childrenGroupBorderRadius = BorderRadius.zero;
break;
}
// Refactored the decorate children group in one place to avoid repeating it
// twice down bellow in the returned widget.
decoratedChildrenGroup = DecoratedBox(
decoration: decoration ??
BoxDecoration(
color: CupertinoDynamicColor.resolve(
decoration?.color ??
CupertinoColors.secondarySystemGroupedBackground,
context),
borderRadius: childrenGroupBorderRadius,
),
child: Column(children: childrenWithDividers),
);
}
return DecoratedBox(
decoration: BoxDecoration(
color: CupertinoDynamicColor.resolve(backgroundColor, context)),
child: Column(
children: <Widget>[
if (type == CupertinoListSectionType.base)
SizedBox(height: topMargin),
if (headerWidget != null)
Align(
alignment: AlignmentDirectional.centerStart,
child: Padding(
padding: type == CupertinoListSectionType.base
? _kDefaultHeaderMargin
: _kInsetGroupedDefaultHeaderMargin,
child: headerWidget,
),
),
if (children != null && children!.isNotEmpty)
Padding(
padding: margin,
child: clipBehavior == Clip.none
? decoratedChildrenGroup
: ClipRRect(
borderRadius: childrenGroupBorderRadius,
clipBehavior: clipBehavior,
child: decoratedChildrenGroup,
),
),
if (footerWidget != null)
Align(
alignment: AlignmentDirectional.centerStart,
child: Padding(
padding: type == CupertinoListSectionType.base
? _kDefaultFooterMargin
: _kInsetGroupedDefaultFooterMargin,
child: footerWidget,
),
),
],
),
);
}