Skip to content

Instantly share code, notes, and snippets.

@luo3house
Last active November 15, 2023 01:38
Show Gist options
  • Save luo3house/124f1a2902f5d9d3c681053f2c95d8f5 to your computer and use it in GitHub Desktop.
Save luo3house/124f1a2902f5d9d3c681053f2c95d8f5 to your computer and use it in GitHub Desktop.
snap value by min, max, step
import 'package:flutter/foundation.dart';
// V0.85, S0.1 -> 0.9
// V0.84, S0.1 -> 0.8
// V0.48, S1.0 -> 0
// V4.80, S5.0 -> 5
// V6.10, S3.0 -> 6
double snapValue(double value, double min, double max, double step) {
final wideValue = () {
if (step == 0) return value; // no restricted
return ((value / step).round() * step);
}();
return clampDouble(wideValue, min, max);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment