Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/genui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- Remove invalid `'functionCall'` envelope from `checksToExpression` and support tolerant unwrapping in `_evaluateStream`.
- Align truthiness evaluation in `isTruthy` across primitives, data model objects, and structured `ValidationResult` objects, while warning on malformed expressions or missing functions.
- Migrate `CheckBox`, `Slider`, and `ChoicePicker` to `ValidationHelper.validateStream` to evaluate rules individually and display authored error messages.
- Fixed `Slider` deriving `divisions` from range, allowing continuous sliding
by default (`divisions: null`), preventing assertion crashes on sub-unit
ranges, fixing thumb value text formatting and literal value fallback, and
handling inverted ranges (`max < min`).

## 0.10.4

Expand Down
27 changes: 20 additions & 7 deletions packages/genui/lib/src/catalog/basic_catalog_widgets/slider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ extension type _SliderData.fromMap(JsonMap _json) {
}
}

String _formatSliderValue(num val) {
if (val == val.roundToDouble()) {
return val.toInt().toString();
}
final String fixed = val.toStringAsFixed(6);
final String trimmed = fixed.replaceAll(RegExp(r'\.?0+$'), '');
return trimmed.isEmpty ? '0' : trimmed;
}
Comment thread
gspencergoog marked this conversation as resolved.
Resolved

/// A Material Design slider.
///
/// This widget allows the user to select a value from a range by sliding a
Expand Down Expand Up @@ -89,25 +98,29 @@ final slider = CatalogItem(
}
}

final double min = sliderData.min;
final double max = sliderData.max < min ? min : sliderData.max;
final double currentVal = (effectiveValue ?? min).toDouble().clamp(
min,
max,
);

final Widget sliderWidget = Padding(
padding: const EdgeInsetsDirectional.only(end: 16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: Slider(
value: (effectiveValue ?? sliderData.min).toDouble(),
min: sliderData.min,
max: sliderData.max,
divisions: (sliderData.max - sliderData.min).toInt(),
value: currentVal,
min: min,
max: max,
onChanged: (newValue) {
itemContext.dataContext.update(DataPath(path), newValue);
},
),
),
Text(
value?.toStringAsFixed(0) ?? sliderData.min.toStringAsFixed(0),
),
Text(_formatSliderValue(currentVal)),
],
),
);
Expand Down
243 changes: 173 additions & 70 deletions packages/genui/test/catalog/core_widgets/slider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,57 @@ import 'package:genui/genui.dart';

import '../../test_infra/message_builders.dart';

Future<SurfaceController> _pumpSlider(
WidgetTester tester, {
Map<String, Object?> properties = const {},
double? initialModelValue,
}) async {
final surfaceController = SurfaceController(
catalogs: [
Catalog([BasicCatalogItems.slider], catalogId: 'test_catalog'),
],
);
const surfaceId = 'testSurface';
final List<JsonMap> components = [
component(id: 'root', type: 'Slider', properties: properties),
];
surfaceController.handleMessage(
updateComponents(surfaceId: surfaceId, components: components),
);
surfaceController.handleMessage(
createSurface(surfaceId: surfaceId, catalogId: 'test_catalog'),
);
if (initialModelValue != null && properties['value'] is Map) {
final path = (properties['value'] as Map)['path'] as String?;
if (path != null) {
surfaceController
.contextFor(surfaceId)
.dataModel
.update(DataPath(path), initialModelValue);
}
}

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Surface(surfaceContext: surfaceController.contextFor(surfaceId)),
),
),
);
await tester.pumpAndSettle();
return surfaceController;
}

void main() {
testWidgets('Slider widget renders and handles changes', (
WidgetTester tester,
) async {
final surfaceController = SurfaceController(
catalogs: [
Catalog([BasicCatalogItems.slider], catalogId: 'test_catalog'),
],
);
const surfaceId = 'testSurface';
final List<JsonMap> components = [
component(
id: 'root',
type: 'Slider',
properties: {
'value': {'path': '/myValue'},
},
),
];
surfaceController.handleMessage(
updateComponents(surfaceId: surfaceId, components: components),
);
surfaceController.handleMessage(
createSurface(surfaceId: surfaceId, catalogId: 'test_catalog'),
);
surfaceController
.contextFor(surfaceId)
.dataModel
.update(DataPath('/myValue'), 0.5);

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Surface(
surfaceContext: surfaceController.contextFor(surfaceId),
),
),
),
final SurfaceController surfaceController = await _pumpSlider(
tester,
properties: {
'value': {'path': '/myValue'},
},
initialModelValue: 0.5,
);

final Slider slider = tester.widget<Slider>(find.byType(Slider));
Expand All @@ -54,49 +67,21 @@ void main() {
await tester.drag(find.byType(Slider), const Offset(100, 0));
expect(
surfaceController
.contextFor(surfaceId)
.contextFor('testSurface')
.dataModel
.getValue<double>(DataPath('/myValue')),
greaterThan(0.5),
);
});

testWidgets('Slider widget renders label', (WidgetTester tester) async {
final surfaceController = SurfaceController(
catalogs: [
Catalog([BasicCatalogItems.slider], catalogId: 'test_catalog'),
],
);
const surfaceId = 'testSurface';
final List<JsonMap> components = [
component(
id: 'root',
type: 'Slider',
properties: {
'value': {'path': '/myValue'},
'label': 'Volume',
},
),
];
surfaceController.handleMessage(
updateComponents(surfaceId: surfaceId, components: components),
);
surfaceController.handleMessage(
createSurface(surfaceId: surfaceId, catalogId: 'test_catalog'),
);
surfaceController
.contextFor(surfaceId)
.dataModel
.update(DataPath('/myValue'), 0.5);

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Surface(
surfaceContext: surfaceController.contextFor(surfaceId),
),
),
),
await _pumpSlider(
tester,
properties: {
'value': {'path': '/myValue'},
'label': 'Volume',
},
initialModelValue: 0.5,
);

expect(find.text('Volume'), findsOneWidget);
Expand Down Expand Up @@ -164,4 +149,122 @@ void main() {

expect(find.text('Must be at least 0.5'), findsNothing);
});

testWidgets('Slider widget is continuous (divisions is null) by default', (
WidgetTester tester,
) async {
await _pumpSlider(
tester,
properties: {
'value': {'path': '/myValue'},
},
initialModelValue: 0.5,
);

final Slider slider = tester.widget<Slider>(find.byType(Slider));
expect(slider.divisions, isNull);
expect(slider.min, 0.0);
expect(slider.max, 1.0);
expect(slider.value, 0.5);
});

testWidgets('Slider widget does not crash on sub-unit ranges', (
WidgetTester tester,
) async {
await _pumpSlider(
tester,
properties: {
'value': {'path': '/myValue'},
'min': 0.0,
'max': 0.5,
},
initialModelValue: 0.2,
);

expect(tester.takeException(), isNull);
final Slider slider = tester.widget<Slider>(find.byType(Slider));
expect(slider.divisions, isNull);
expect(slider.min, 0.0);
expect(slider.max, 0.5);
expect(slider.value, 0.2);
});

testWidgets(
'Slider widget displays literal value without data model binding',
(WidgetTester tester) async {
await _pumpSlider(
tester,
properties: {'value': 0.5, 'min': 0.0, 'max': 1.0},
);

final Slider slider = tester.widget<Slider>(find.byType(Slider));
expect(slider.value, 0.5);
expect(find.text('0.5'), findsOneWidget);
},
);

testWidgets(
'Slider widget formats whole numbers and decimal numbers correctly',
(WidgetTester tester) async {
final SurfaceController controller = await _pumpSlider(
tester,
properties: {
'value': {'path': '/myValue'},
'min': 0.0,
'max': 100.0,
},
initialModelValue: 10.0,
);

// Whole numbers should not display trailing .0
expect(find.text('10'), findsOneWidget);

// Decimal numbers should preserve fractional part without trailing zeroes
controller
.contextFor('testSurface')
.dataModel
.update(DataPath('/myValue'), 0.25);
await tester.pumpAndSettle();
expect(find.text('0.25'), findsOneWidget);

// Sub-unit / high precision values up to 6 decimals should be preserved
controller
.contextFor('testSurface')
.dataModel
.update(DataPath('/myValue'), 0.005);
await tester.pumpAndSettle();
expect(find.text('0.005'), findsOneWidget);
},
);

testWidgets(
'Slider widget clamps label display when initial value is out of bounds',
(WidgetTester tester) async {
await _pumpSlider(
tester,
properties: {'value': -5.0, 'min': 0.0, 'max': 10.0},
);

final Slider slider = tester.widget<Slider>(find.byType(Slider));
expect(slider.value, 0.0);
expect(find.text('0'), findsOneWidget);
},
);

testWidgets(
'Slider widget handles inverted range (max < min) by clamping to min',
(WidgetTester tester) async {
await _pumpSlider(
tester,
properties: {'value': 5.0, 'min': 10.0, 'max': 0.0},
);

expect(tester.takeException(), isNull);
final Slider slider = tester.widget<Slider>(find.byType(Slider));
expect(slider.min, 10.0);
expect(slider.max, 10.0);
expect(slider.value, 10.0);
expect(find.text('10'), findsOneWidget);
},
);
}
Loading