Skip to content

Commit

Permalink
Merge pull request #19 from tappeddev/refactor_onchange
Browse files Browse the repository at this point in the history
refactor_onchange
  • Loading branch information
stefanschaller authored May 14, 2024
2 parents 2c11648 + 47b8ee3 commit 20ed240
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
2 changes: 1 addition & 1 deletion example/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>11.0</string>
<string>12.0</string>
</dict>
</plist>
6 changes: 3 additions & 3 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down Expand Up @@ -352,7 +352,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -401,7 +401,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down
26 changes: 13 additions & 13 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
url: "https://pub.dev"
source: hosted
version: "1.17.2"
version: "1.18.0"
cupertino_icons:
dependency: "direct main"
description:
Expand Down Expand Up @@ -103,10 +103,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
url: "https://pub.dev"
source: hosted
version: "1.9.1"
version: "1.10.0"
non_uniform_border:
dependency: transitive
description:
Expand Down Expand Up @@ -140,18 +140,18 @@ packages:
dependency: transitive
description:
name: stack_trace
sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
url: "https://pub.dev"
source: hosted
version: "1.11.0"
version: "1.11.1"
stream_channel:
dependency: transitive
description:
name: stream_channel
sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8"
sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7
url: "https://pub.dev"
source: hosted
version: "2.1.1"
version: "2.1.2"
string_scanner:
dependency: transitive
description:
Expand Down Expand Up @@ -179,10 +179,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8"
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
url: "https://pub.dev"
source: hosted
version: "0.6.0"
version: "0.6.1"
vector_math:
dependency: transitive
description:
Expand All @@ -195,10 +195,10 @@ packages:
dependency: transitive
description:
name: web
sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10
sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152
url: "https://pub.dev"
source: hosted
version: "0.1.4-beta"
version: "0.3.0"
sdks:
dart: ">=3.1.0-185.0.dev <4.0.0"
dart: ">=3.2.0-194.0.dev <4.0.0"
flutter: ">=3.3.1"
36 changes: 25 additions & 11 deletions lib/src/text_field/base_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ class BaseTextFieldState extends State<BaseTextField>
@visibleForTesting
FocusNode get focusNode => _focusNode;

// We previous used the onChange event from the [TextFormField], since adding a listener to the [TextEditingController] is also called
// when we focus / unfocus the [TextFormField].
// 💾 This was the previous documentation:
// 💾 use onChange instead of [TextEditingController.addListener]
// 💾 because this will notify a text change when we loose focus
// 💾 when routing back. This will trigger a new search which is wrong.
// Since we now use the listener, we need to distinct the value by ourself.
late String _previousTextValue = widget.text;

bool _textFieldIsValid = true;

@override
Expand All @@ -141,6 +150,9 @@ class BaseTextFieldState extends State<BaseTextField>
_textEditingController = TextEditingController();
}

_textEditingController
.addListener(() => _onChanged(_textEditingController.text));

_focusNode.addListener(_addFocusNodeListener);
}

Expand Down Expand Up @@ -197,17 +209,6 @@ class BaseTextFieldState extends State<BaseTextField>
enableSuggestions: widget.enableSuggestions,
focusNode: _focusNode,
controller: _textEditingController,
// use onChange instead of [TextEditingController.addListener]
// because this will notify a text change when we loose focus
// when routing back. This will trigger a new search which is wrong.
onChanged: (string) {
// we always want to validate the new input when the current state is invalid
if (!_textFieldIsValid) {
_formFieldKey.currentState?.validate();
}

widget.onChanged(string);
},
decoration: widget.decoration,
);
}
Expand Down Expand Up @@ -248,6 +249,19 @@ class BaseTextFieldState extends State<BaseTextField>
_formFieldKey.currentState?.validate();
}

void _onChanged(String value) {
if (_previousTextValue == value) return;

_previousTextValue = value;

// we always want to validate the new input when the current state is invalid
if (!_textFieldIsValid) {
_formFieldKey.currentState?.validate();
}

widget.onChanged(value);
}

void _addFocusNodeListener() {
widget.onFocusChanged?.call(_focusNode.hasFocus);

Expand Down

0 comments on commit 20ed240

Please sign in to comment.