Skip to content

Instantly share code, notes, and snippets.

@austinevick
Created January 11, 2024 10:49
Show Gist options
  • Save austinevick/b9d4cd55c4708946c24855e66ffdb518 to your computer and use it in GitHub Desktop.
Save austinevick/b9d4cd55c4708946c24855e66ffdb518 to your computer and use it in GitHub Desktop.
Custom range input formatter
import 'package:flutter/services.dart';
class CustomRangeTextInputFormatter extends TextInputFormatter {
final double min;
final double max;
CustomRangeTextInputFormatter({required this.min, required this.max});
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue,
TextEditingValue newValue,
) {
if (newValue.text == '') {
return const TextEditingValue();
} else if (int.parse(newValue.text) < min) {
return const TextEditingValue().copyWith(text: min.toString());
} else if (int.parse(newValue.text) > max) {
return const TextEditingValue();
} else {
const TextEditingValue().copyWith(text: max.toString());
}
return newValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment