From 2803a2043c10e8db48195722e2677b7b0e760055 Mon Sep 17 00:00:00 2001 From: Honza Bittner Date: Wed, 14 Feb 2024 17:16:52 +0100 Subject: [PATCH 1/2] Fix selection --- glade_forms/CHANGELOG.md | 3 +++ glade_forms/lib/src/core/glade_input.dart | 5 +++-- glade_forms/pubspec.yaml | 2 +- glade_forms/test/glade_input_test.dart | 19 +++++++++++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/glade_forms/CHANGELOG.md b/glade_forms/CHANGELOG.md index a5c9b7e..29caf65 100644 --- a/glade_forms/CHANGELOG.md +++ b/glade_forms/CHANGELOG.md @@ -1,3 +1,6 @@ +## 1.3.2 +- **[Fix]**: `GladeInput` now preserves selection. (Before, a cursor jumped at the end.) + ## 1.3.1 - **[Fix]**: Fixed `GladeInput.create` assert to allow null for `value` and `initialValue` when input's type is nullable. diff --git a/glade_forms/lib/src/core/glade_input.dart b/glade_forms/lib/src/core/glade_input.dart index 5e09115..6dacae2 100644 --- a/glade_forms/lib/src/core/glade_input.dart +++ b/glade_forms/lib/src/core/glade_input.dart @@ -114,7 +114,7 @@ class GladeInput extends ChangeNotifier { // synchronize text controller with value _textEditingController?.value = TextEditingValue( text: strValue, - selection: TextSelection.collapsed(offset: strValue.length), + selection: _textEditingController?.selection ?? const TextSelection.collapsed(offset: -1), ); _isPure = false; @@ -201,7 +201,8 @@ class GladeInput extends ChangeNotifier { ValueTransform? valueTransform, DefaultTranslations? defaultTranslations, }) { - assert(value != null || initialValue != null || TypeHelper.typeIsNullable(), 'If type is not nullable, at least one of value or initialValue must be set'); + assert(value != null || initialValue != null || TypeHelper.typeIsNullable(), + 'If type is not nullable, at least one of value or initialValue must be set'); final validatorInstance = validator?.call(GladeValidator()) ?? GladeValidator().build(); diff --git a/glade_forms/pubspec.yaml b/glade_forms/pubspec.yaml index 9a90c95..c237c74 100644 --- a/glade_forms/pubspec.yaml +++ b/glade_forms/pubspec.yaml @@ -1,6 +1,6 @@ name: glade_forms description: A universal way to define form validators with support of translations. -version: 1.3.1 +version: 1.3.2 repository: https://github.com/netglade/glade_forms issue_tracker: https://github.com/netglade/glade_forms/issues screenshots: diff --git a/glade_forms/test/glade_input_test.dart b/glade_forms/test/glade_input_test.dart index 81b7c28..d1170ac 100644 --- a/glade_forms/test/glade_input_test.dart +++ b/glade_forms/test/glade_input_test.dart @@ -1,3 +1,4 @@ +import 'package:flutter/widgets.dart'; import 'package:glade_forms/glade_forms.dart'; import 'package:test/test.dart'; @@ -7,4 +8,22 @@ void main() { expect(input.isValid, isTrue); }); + + test('GladeInput preserves selection on value change', () { + final input = GladeInput.create(validator: (v) => v.build(), value: 'abc', inputKey: 'a'); + + // Set selection + input.controller?.selection = const TextSelection.collapsed(offset: 1); + final before = input.controller?.selection; + + // Update text value. + input.updateValue('abXXXc'); + + // ignore: avoid-duplicate-initializers, different value + final after = input.controller?.selection; + + // Compare their offsets. + expect(before?.baseOffset, equals(after?.baseOffset)); + expect(before?.extentOffset, equals(after?.extentOffset)); + }); } From 831b9747a29cef043fbbe236c948e0cf023bbb8a Mon Sep 17 00:00:00 2001 From: Honza Bittner Date: Wed, 14 Feb 2024 17:19:35 +0100 Subject: [PATCH 2/2] Fix lints --- glade_forms/lib/src/core/glade_input.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/glade_forms/lib/src/core/glade_input.dart b/glade_forms/lib/src/core/glade_input.dart index 6dacae2..a36c955 100644 --- a/glade_forms/lib/src/core/glade_input.dart +++ b/glade_forms/lib/src/core/glade_input.dart @@ -201,8 +201,10 @@ class GladeInput extends ChangeNotifier { ValueTransform? valueTransform, DefaultTranslations? defaultTranslations, }) { - assert(value != null || initialValue != null || TypeHelper.typeIsNullable(), - 'If type is not nullable, at least one of value or initialValue must be set'); + assert( + value != null || initialValue != null || TypeHelper.typeIsNullable(), + 'If type is not nullable, at least one of value or initialValue must be set', + ); final validatorInstance = validator?.call(GladeValidator()) ?? GladeValidator().build();