Created
January 11, 2024 10:49
-
-
Save austinevick/b9d4cd55c4708946c24855e66ffdb518 to your computer and use it in GitHub Desktop.
Custom range input formatter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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