Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复BrnStepInputFormItem更新值时设置非法的TextSelection导致光标错位的问题 Close #235 #239

Merged
merged 1 commit into from
Jun 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/src/components/form/items/general/brn_step_input_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,11 @@ class BrnStepInputFormItemState extends State<BrnStepInputFormItem> {
int get _value => int.tryParse(_textEditingController.text) ?? 0;

set _value(int value) {
_textEditingController.text = value.toString();
// 如果是通过代码设置TextField的值,就将光标移动到最后
_textEditingController.value = TextEditingValue(
text: value.toString(),
selection: TextSelection.fromPosition(
TextPosition(offset: value.toString().length)));
}
}

Expand All @@ -328,10 +332,11 @@ class RangeLimitedTextInputFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
int? newNum = int.tryParse(newValue.text);
if(newNum == null && minValue == 0) {
return TextEditingValue(text: '');
if (newNum == null && minValue == 0) {
return const TextEditingValue(
text: '', selection: TextSelection.collapsed(offset: 0));
} else if (newNum != null && minValue <= newNum && newNum <= maxValue) {
return TextEditingValue(text: newNum.toString());
return newValue;
} else {
return oldValue;
}
Expand Down