Skip to content

Commit

Permalink
Move RangeSlider to new components
Browse files Browse the repository at this point in the history
  • Loading branch information
rmahmadkhan committed Nov 19, 2024
1 parent 714b2a8 commit 85f5d68
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 210 deletions.
178 changes: 0 additions & 178 deletions modules/ensemble/lib/widget/input/slider.dart

This file was deleted.

17 changes: 10 additions & 7 deletions modules/ensemble/lib/widget/input/slider/slider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class EnsembleSlider extends StatefulWidget
Map<String, Function> setters() {
return {
// Basic Properties
'initialValue': (value) =>
_controller.value = Utils.optionalDouble(value) ?? 0,
'initialValue': (value) => _controller.value =
Utils.getRangeValues(value) ?? const RangeValues(0, 0),
'min': (value) =>
_controller.minValue = Utils.getDouble(value, fallback: 0.0),
'max': (value) =>
Expand All @@ -60,11 +60,11 @@ class EnsembleSlider extends StatefulWidget
_controller.trackStyle = TrackStyleComposite.from(_controller, value),
'tickMarkStyle': (value) => _controller.tickMarkStyle =
TickMarkStyleComposite.from(_controller, value),
'thumbStyle': (value) => _controller.thumbStyle =
ThumbStyleComposite.from(_controller, value),
'overlayStyle': (value) => _controller.overlayStyle =
'thumbStyle': (value) =>
_controller.thumbStyle = ThumbStyleComposite.from(_controller, value),
'overlayStyle': (value) => _controller.overlayStyle =
OverlayStyleComposite.from(_controller, value),
'valueIndicatorStyle': (value) => _controller.valueIndicatorStyle =
'valueIndicatorStyle': (value) => _controller.valueIndicatorStyle =
ValueIndicatorStyleComposite.from(_controller, value),

// @deprecated properties
Expand All @@ -85,6 +85,9 @@ class EnsembleSlider extends StatefulWidget
// Event Handler
'onChange': (definition) => _controller.onChange =
EnsembleAction.from(definition, initiator: this),

'isRange': (value) =>
_controller.isRange = Utils.getBool(value, fallback: false),
};
}
}
}
11 changes: 7 additions & 4 deletions modules/ensemble/lib/widget/input/slider/slider_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'composites/value_indicator_style.dart';

class SliderController extends FormFieldController {
// Basic Values
double value = 0.0;
RangeValues value = const RangeValues(0.0, 1.0);
double minValue = 0.0;
double maxValue = 1.0;
int? divisions;
Expand All @@ -26,7 +26,7 @@ class SliderController extends FormFieldController {
set tickMarkStyle(TickMarkStyleComposite value) => _tickMarkStyle = value;

ThumbStyleComposite? _thumbStyle;
ThumbStyleComposite get thumbStyle =>
ThumbStyleComposite get thumbStyle =>
_thumbStyle ??= ThumbStyleComposite(this);
set thumbStyle(ThumbStyleComposite value) => _thumbStyle = value;

Expand All @@ -38,7 +38,7 @@ class SliderController extends FormFieldController {
ValueIndicatorStyleComposite? _valueIndicatorStyle;
ValueIndicatorStyleComposite get valueIndicatorStyle =>
_valueIndicatorStyle ??= ValueIndicatorStyleComposite(this);
set valueIndicatorStyle(ValueIndicatorStyleComposite value) =>
set valueIndicatorStyle(ValueIndicatorStyleComposite value) =>
_valueIndicatorStyle = value;

// @deprecated. backward compatibility
Expand All @@ -52,4 +52,7 @@ class SliderController extends FormFieldController {

// Event Handler
EnsembleAction? onChange;
}

// Optional property to determine if the slider is a range slider
bool isRange = false;
}
63 changes: 42 additions & 21 deletions modules/ensemble/lib/widget/input/slider/slider_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,54 @@ class SliderState extends FormFieldWidgetState<EnsembleSlider> {

return SliderTheme(
data: themeData,
child: Slider(
value: widget.controller.value,
min: widget.controller.minValue,
max: widget.controller.maxValue,
divisions: widget.controller.divisions,
label: widget.controller.value.toStringAsFixed(decimalPlaces),
onChanged: isEnabled()
? (value) {
setState(() {
widget.controller.value = value;
});
if (widget.controller.onChange != null) {
ScreenController().executeAction(
context,
widget.controller.onChange!,
event: EnsembleEvent(widget),
);
}
}
: null,
),
child: widget.controller.isRange
? RangeSlider(
labels: RangeLabels(
widget.controller.value.start
.toStringAsFixed(decimalPlaces),
widget.controller.value.end
.toStringAsFixed(decimalPlaces),
),
min: widget.controller.minValue,
max: widget.controller.maxValue,
values: widget.controller.value,
divisions: widget.controller.divisions,
onChanged: _onChanged,
)
: Slider(
value: widget.controller.value.start,
min: widget.controller.minValue,
max: widget.controller.maxValue,
divisions: widget.controller.divisions,
label: widget.controller.value.start
.toStringAsFixed(decimalPlaces),
onChanged: _onChanged,
),
);
},
),
);
}

void _onChanged(dynamic value) {
if (!isEnabled()) return;

setState(() {
if (value is RangeValues) {
widget.controller.value = value;
} else if (value is double) {
widget.controller.value = RangeValues(value, value);
}
});
if (widget.controller.onChange != null) {
ScreenController().executeAction(
context,
widget.controller.onChange!,
event: EnsembleEvent(widget),
);
}
}

int calculateDecimalPlaces(double min, double max, int? divisions) {
if (divisions == null) return 1;
double interval = (max - min) / divisions;
Expand Down

0 comments on commit 85f5d68

Please sign in to comment.