build method
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
contextusing 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 prefix = isInt ? value.round().toString() : value.toStringAsFixed(2);
final leftFlex = (sliderWidthFactor.clamp(0.05, 1.0) * 100).round();
final rightFlex = 100 - leftFlex;
final slider = Semantics(
// CupertinoSlider carries no descriptive label; name it + read out the live value so screen readers announce
// "Confidence Threshold, 0.25" instead of an anonymous adjustable. Visuals are unchanged.
label: label,
value: prefix,
slider: true,
child: CupertinoSlider(
value: value.clamp(min, max),
min: min,
max: max,
divisions: divisions,
// iOS YOLOView uses pure white for the filled portion; CupertinoSlider's default `activeColor` is the system
// accent, which is blue on iOS. Force white to match the reference.
activeColor: Colors.white,
thumbColor: Colors.white,
onChanged: onChanged,
),
);
return Column(
// Left-justified: the caption and the slider both hug the left edge (matching yolo-ios-app). The Row below
// fills the full width, which also forces this Column to full width so the parent can't center it.
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
'$prefix $label',
maxLines: 1,
overflow: TextOverflow.visible,
softWrap: false,
style: const TextStyle(
color: Colors.white,
fontSize: 13,
fontWeight: FontWeight.w400,
),
),
// The slider track occupies the left `sliderWidthFactor` of the width; the Spacer fills the rest. A Row fills
// its width (unlike FractionallySizedBox, which shrink-wrapped and let the whole row get center-aligned).
Transform.translate(
offset: const Offset(0, -6),
child: Row(
children: [
Expanded(flex: leftFlex, child: slider),
if (rightFlex > 0) Spacer(flex: rightFlex),
],
),
),
],
);
}