build method
- 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 = separatorColor ?? CupertinoColors.separator.resolveFrom(context);
final double dividerHeight = 1.0 / MediaQuery.devicePixelRatioOf(context);
// 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,
);
TextStyle style = CupertinoTheme.of(context).textTheme.textStyle;
Widget? headerWidget, footerWidget;
switch (type) {
case CupertinoListSectionType.base:
style = style.merge(TextStyle(
fontSize: 13.0,
color: CupertinoDynamicColor.resolve(_kHeaderFooterColor, context),
));
if (header != null) {
headerWidget = DefaultTextStyle(style: style, child: header!);
}
if (footer != null) {
footerWidget = DefaultTextStyle(style: style, child: footer!);
}
case CupertinoListSectionType.insetGrouped:
if (header != null) {
headerWidget = DefaultTextStyle(
style: style.merge(const TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold)),
child: header!,
);
}
if (footer != null) {
footerWidget = DefaultTextStyle(style: style, child: footer!);
}
}
Widget? 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);
}
final BorderRadius childrenGroupBorderRadius = switch (type) {
CupertinoListSectionType.insetGrouped => _kDefaultInsetGroupedBorderRadius,
CupertinoListSectionType.base => BorderRadius.zero,
};
decoratedChildrenGroup = DecoratedBox(
decoration: decoration ??
BoxDecoration(
color: CupertinoDynamicColor.resolve(
decoration?.color ??
CupertinoColors.secondarySystemGroupedBackground,
context),
borderRadius: childrenGroupBorderRadius,
),
child: Column(children: childrenWithDividers),
);
decoratedChildrenGroup = Padding(
padding: margin,
child: clipBehavior == Clip.none
? decoratedChildrenGroup
: ClipRRect(
borderRadius: childrenGroupBorderRadius,
clipBehavior: clipBehavior,
child: decoratedChildrenGroup,
),
);
}
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 (decoratedChildrenGroup != null)
decoratedChildrenGroup,
if (footerWidget != null)
Align(
alignment: AlignmentDirectional.centerStart,
child: Padding(
padding: type == CupertinoListSectionType.base
? _kDefaultFooterMargin
: _kInsetGroupedDefaultFooterMargin,
child: footerWidget,
),
),
],
),
);
}