mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-04 15:49:49 +00:00
31 lines
788 B
Dart
31 lines
788 B
Dart
import 'package:flutter/services.dart';
|
|
|
|
/// A [TextInputFormatter] that restricts input to a numerical range between [min] and [max].
|
|
class NumericalRangeFormatter extends TextInputFormatter {
|
|
final int min;
|
|
final int max;
|
|
|
|
NumericalRangeFormatter({required this.min, required this.max});
|
|
|
|
@override
|
|
TextEditingValue formatEditUpdate(
|
|
TextEditingValue oldValue,
|
|
TextEditingValue newValue,
|
|
) {
|
|
if (newValue.text.isEmpty) {
|
|
return newValue;
|
|
}
|
|
var intVal = int.tryParse(newValue.text);
|
|
if (intVal == null) {
|
|
return oldValue;
|
|
}
|
|
if (intVal < min) {
|
|
return newValue.copyWith(text: min.toString());
|
|
} else if (intVal > max) {
|
|
return oldValue.copyWith(text: max.toString());
|
|
} else {
|
|
return newValue;
|
|
}
|
|
}
|
|
}
|