Add dividendOfRange parameter to FunctionSlider and related components for improved range handling

This commit is contained in:
Faris Armoush
2025-04-13 10:09:39 +03:00
parent 828db5d5e4
commit 60a1a9ad6f
4 changed files with 41 additions and 7 deletions

View File

@ -2,25 +2,34 @@ import 'package:flutter/material.dart';
class FunctionSlider extends StatelessWidget { class FunctionSlider extends StatelessWidget {
final dynamic initialValue; final dynamic initialValue;
final (double min, double max) range; final (double min, double max) range;
final void Function(double value) onChanged; final void Function(double value) onChanged;
final double dividendOfRange;
const FunctionSlider({ const FunctionSlider({
required this.onChanged, required this.onChanged,
required this.initialValue, required this.initialValue,
required this.range, required this.range,
required this.dividendOfRange,
super.key, super.key,
}); });
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final (min, max) = range;
final bool isValidRange = max > min;
final double value = initialValue is int
? (initialValue as int).toDouble()
: (initialValue as double);
final int? divisions = isValidRange ? ((max - min) / dividendOfRange).round() : null;
return Slider( return Slider(
value: initialValue is int ? initialValue.toDouble() : range.$1, value: value.clamp(min, max),
min: range.$1, min: min,
max: range.$2, max: max,
divisions: (range.$2 - range.$1).toInt(), divisions: divisions,
onChanged: onChanged, onChanged: isValidRange ? onChanged : null,
); );
} }
} }

View File

@ -52,11 +52,12 @@ class CpsDialogSliderSelector extends StatelessWidget {
entityId: device?.uuid ?? '', entityId: device?.uuid ?? '',
functionCode: selectedFunction, functionCode: selectedFunction,
operationName: selectedFunctionData.operationName, operationName: selectedFunctionData.operationName,
value: value.toInt(), value: value,
condition: selectedFunctionData.condition, condition: selectedFunctionData.condition,
), ),
), ),
), ),
dividendOfRange: _dividendOfRange,
); );
} }
@ -111,4 +112,24 @@ class CpsDialogSliderSelector extends StatelessWidget {
_ => '', _ => '',
}; };
} }
double get _dividendOfRange => switch (selectedFunctionData.functionCode) {
'sensitivity' => 1,
'moving_speed' => 1,
'space_static_val' => 1,
'space_move_val' => 1,
'presence_reference' => 5,
'moving_reference' => 5,
'moving_max_dis' => 0.5,
'static_max_dis' => 0.5,
'moving_range' => 0.1,
'presence_range' => 0.1,
'perceptual_boundary' => 0.5,
'moving_boundary' => 0.5,
'moving_rigger_time' => 0.1,
'moving_static_time' => 1.0,
'none_body_time' => 5.0,
'sports_para' => 1.0,
_ => 1,
};
} }

View File

@ -60,6 +60,7 @@ class WpsValueSelectorWidget extends StatelessWidget {
), ),
), ),
unit: _unit, unit: _unit,
dividendOfRange: 1,
); );
} }

View File

@ -15,6 +15,7 @@ class SliderValueSelector extends StatelessWidget {
final void Function(String condition) onConditionChanged; final void Function(String condition) onConditionChanged;
final void Function(double value) onSliderChanged; final void Function(double value) onSliderChanged;
final String unit; final String unit;
final double dividendOfRange;
const SliderValueSelector({ const SliderValueSelector({
required this.dialogType, required this.dialogType,
@ -25,6 +26,7 @@ class SliderValueSelector extends StatelessWidget {
required this.onSliderChanged, required this.onSliderChanged,
required this.currentCondition, required this.currentCondition,
required this.unit, required this.unit,
required this.dividendOfRange,
super.key, super.key,
}); });
@ -48,6 +50,7 @@ class SliderValueSelector extends StatelessWidget {
initialValue: initialValue, initialValue: initialValue,
range: sliderRange, range: sliderRange,
onChanged: onSliderChanged, onChanged: onSliderChanged,
dividendOfRange: dividendOfRange,
), ),
], ],
); );