buildSlider function
Implementation
Widget buildSlider(BuildContext context, OpenUiNode node) {
final theme = node.theme;
final label = _nonEmpty(node.props.string('label'));
final f = _field(node, 'value', label);
final min = node.props.number('min')?.toDouble() ?? 0;
final maxProp = node.props.number('max')?.toDouble();
final max = maxProp != null && maxProp > min ? maxProp : min + 100;
final stepProp = node.props.number('step')?.toDouble();
final step = stepProp != null && stepProp > 0 ? stepProp : 1.0;
// `Number(field.value)`; null is an unset (undefined) field, so NaN.
final raw = f.value == null ? double.nan : jsNumber(f.value);
final value = raw.isFinite ? raw.clamp(min, max).toDouble() : min;
final unit = node.props.string('unit') ?? '';
final prefix = RegExp(r'^[$€£¥₹]').hasMatch(unit);
String readoutOf(double v) =>
prefix ? '$unit${_numberText(v)}' : '${_numberText(v)}$unit';
double snap(double v) {
final snapped = min + ((v - min) / step).round() * step;
return double.parse(snapped.clamp(min, max).toStringAsFixed(10));
}
final readout = readoutOf(value);
final count = (max - min) / step;
final steps = count.round();
final divisions = (count - steps).abs() < 1e-9 && steps >= 1 && steps <= 1000
? steps
: null;
final marks = [
for (final m in node.props.list('marks'))
if (m is Map &&
m['value'] is num &&
(m['value'] as num).isFinite &&
(m['value'] as num) >= min &&
(m['value'] as num) <= max)
(
at: ((m['value'] as num) - min) / (max - min),
label: _plain(m['label']),
),
];
final small = _text(theme, size: theme.fontSizeOf('Slider', 13), height: 1.4);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
children: [
if (label != null || unit.isNotEmpty)
Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: [
Expanded(child: Text(label ?? '', style: small)),
const SizedBox(width: 12),
Text(
readout,
softWrap: false,
style: _text(
theme,
size: theme.fontSizeOf('Slider.value', 12),
height: 1.4,
weight: theme.typography.medium,
tabular: true,
),
),
],
),
SizedBox(
height: 40,
child: SliderTheme(
data: SliderThemeData(
trackHeight: 24,
activeTrackColor: theme.rangeTrack,
inactiveTrackColor: theme.rangeTrack,
thumbColor: theme.ink,
thumbShape: const RoundSliderThumbShape(
enabledThumbRadius: 9,
elevation: 0,
pressedElevation: 0,
),
overlayShape: SliderComponentShape.noOverlay,
tickMarkShape: SliderTickMarkShape.noTickMark,
trackShape: const _RangeTrack(),
),
child: MergeSemantics(
child: Semantics(
label: label ?? OpenUiLocalizations.of(context).sliderValue,
child: Slider(
min: min,
max: max,
value: value,
divisions: divisions,
semanticFormatterCallback: (v) => readoutOf(snap(v)),
onChanged: (v) {
final next = snap(v);
f.set(next == next.truncateToDouble() ? next.toInt() : next);
},
),
),
),
),
),
if (marks.isNotEmpty)
ExcludeSemantics(
child: LayoutBuilder(
builder: (context, box) {
final style = _text(
theme,
size: theme.fontSizeOf('Slider.mark', 11),
height: 18 / 11,
color: theme.muted,
tabular: true,
);
return SizedBox(
height: 16,
child: Stack(
clipBehavior: Clip.none,
children: [
for (final m in marks)
if (m.at <= 0.02)
PositionedDirectional(
top: -2,
start: (box.maxWidth - 18) * m.at,
child: Text(m.label, softWrap: false, style: style),
)
else if (m.at >= 0.98)
PositionedDirectional(
top: -2,
end: (box.maxWidth - 18) * (1 - m.at),
child: Text(m.label, softWrap: false, style: style),
)
else
PositionedDirectional(
top: -2,
start: 9 + (box.maxWidth - 18) * m.at,
child: FractionalTranslation(
translation: const Offset(-0.5, 0),
child: Text(m.label, softWrap: false, style: style),
),
),
],
),
);
},
),
),
],
);
}