natintosh / intl_phone_number_input

MIT License
166 stars 510 forks source link

onInputValidated runs continuously once is valid #238

Closed carman247 closed 3 years ago

carman247 commented 3 years ago

Hi,

This is a great package but it seems like once a number is validated using the onInputValidated function then that function runs continuously ... at least on Android it does.

[✓] Flutter (Channel stable, 2.2.3, on macOS 11.2.1 20D75 darwin-x64, locale en-GB)
    • Flutter version 2.2.3 at /Users/chris/Developer/flutter
    • Framework revision f4abaa0735 (3 weeks ago), 2021-07-01 12:46:11 -0700
    • Engine revision 241c87ad80
    • Dart version 2.13.4

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /Users/chris/Library/Android/sdk
    • Platform android-30, build-tools 29.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.4, Build version 12D4e
    • CocoaPods version 1.10.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)

[✓] VS Code (version 1.58.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.24.0
carman247 commented 3 years ago

Nope, ignore this .. I was setting state in the parent widget and it was updating the child

henry2man commented 3 years ago

Nope, ignore this .. I was setting state in the parent widget and it was updating the child

@carman247 I do need to call setState() because I'm storing in my widget state the result of onInputValidated() in order to show custom validation messages.

How do you managed to solve this?

EDIT: If someone has the same problem, I have solved it using a "ValueListenableBuilder ()":

/*class fields*/
ValueListenable<bool?> mobileIsValid;

[...]

/*build(...) method*/
ValueListenableBuilder<bool?>(
  valueListenable: mobileIsValid,
  child: InternationalPhoneNumberInput( /* Configuring the widget as child avoids reloading */
    onInputChanged: (number) {
      mobile = number;
    },
    onInputValidated: (valid) {
      mobileIsValid.value = valid; /*Use here a ValueListenable instead of a setState() call*/
    },
  ),
  builder: (context, value, child) {
    return CupertinoFormRow(
        prefix: const Text("Móvil"),
        error: value == null || value
            ? null
            : const Text(
                "Indica un número de móvil válido"),
        child: child!); 
  }),