ย ยทย Solo Designer, Builder and Growth Expert

Help with custom widget: returning values from range slider

Have added a custom range slider to my project but need it to return the start and end values so I can save them as component states. Anyone know how to do this?

class RangeSliderWidget extends StatefulWidget {
  const RangeSliderWidget({
    Key? key,
    this.width,
    this.height,
    required this.min, // Changed to int
    required this.max, // Changed to int
    required this.start, // Changed to int
    required this.end, // Changed to int
    required this.divisions,
    this.textColor = Colors.black,
    this.sliderColor = Colors.blue,
    this.thumbColor = Colors.white,
    this.inactiveTrackColor = Colors.grey,
    this.showLabels = true,
    this.showRangeLabels = true,
    this.minLabel = 'Min: ',
    this.maxLabel = 'Max: ',
    this.labelFontSize = 16.0,
    this.valueFontSize = 16.0,
    required this.isEnabled,
  }) : super(key: key);

  final double? width;
  final double? height;
  final int min;
  final int max;
  final int start;
  final int end;
  final int divisions;
  final Color textColor;
  final Color sliderColor;
  final Color thumbColor;
  final Color inactiveTrackColor;
  final bool showLabels;
  final bool showRangeLabels;
  final String minLabel;
  final String maxLabel;
  final double labelFontSize;
  final double valueFontSize;
  final bool isEnabled;

  @override
  _RangeSliderWidgetState createState() => _RangeSliderWidgetState();
}

class _RangeSliderWidgetState extends State<RangeSliderWidget> {
  late RangeValues _currentRangeValues;

  @override
  void initState() {
    super.initState();
    // Convert start and end to doubles for RangeValues
    _currentRangeValues =
        RangeValues(widget.start.toDouble(), widget.end.toDouble());
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.width,
      height: widget.height,
      child: SliderTheme(
        data: SliderThemeData(
          trackHeight: 4.0,
          activeTrackColor: widget.sliderColor,
          inactiveTrackColor: widget.inactiveTrackColor,
          thumbColor: widget.thumbColor,
        ),
        child: RangeSlider(
          values: _currentRangeValues,
          min: widget.min.toDouble(), // Convert to double
          max: widget.max.toDouble(), // Convert to double
          divisions: widget.divisions,
          labels: widget.showRangeLabels
              ? RangeLabels(
                  _currentRangeValues.start.round().toString(),
                  _currentRangeValues.end.round().toString(),
                )
              : null,
          onChanged: widget.isEnabled
              ? (RangeValues values) {
                  setState(() {
                    _currentRangeValues = values;
                  });
                }
              : null,
        ),
      ),
    );
  }
}

1