import 'package:flutter/material.dart';

class FunctionSlider extends StatelessWidget {
  final dynamic initialValue;
  final (double min, double max) range;
  final void Function(double value) onChanged;
  final double dividendOfRange;

  const FunctionSlider({
    required this.onChanged,
    required this.initialValue,
    required this.range,
    required this.dividendOfRange,
    super.key,
  });

  @override
  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(
      value: value.clamp(min, max),
      min: min,
      max: max,
      divisions: divisions,
      onChanged: isValidRange ? onChanged : null,
    );
  }
}
