From e5af982b2c192acb4e1a71ce0afbb403b4e23fa4 Mon Sep 17 00:00:00 2001 From: gorkemhacioglu Date: Fri, 5 Jul 2024 20:46:45 +0200 Subject: [PATCH 1/2] Implemented auto country detection --- lib/src/widgets/input_widget.dart | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/src/widgets/input_widget.dart b/lib/src/widgets/input_widget.dart index 94972bad18..add150471d 100644 --- a/lib/src/widgets/input_widget.dart +++ b/lib/src/widgets/input_widget.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:ui'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -51,7 +52,7 @@ class InternationalPhoneNumberInput extends StatefulWidget { final TextInputType keyboardType; final TextInputAction? keyboardAction; - final PhoneNumber? initialValue; + PhoneNumber? initialValue; final String? hintText; final String? errorMessage; @@ -66,6 +67,7 @@ class InternationalPhoneNumberInput extends StatefulWidget { final bool autoFocus; final bool autoFocusSearch; final AutovalidateMode autoValidateMode; + final bool autoCountryDetection; final bool ignoreBlank; final bool countrySelectorScrollControlled; @@ -110,6 +112,7 @@ class InternationalPhoneNumberInput extends StatefulWidget { this.autoFocus = false, this.autoFocusSearch = false, this.autoValidateMode = AutovalidateMode.disabled, + this.autoCountryDetection = false, this.ignoreBlank = false, this.countrySelectorScrollControlled = true, this.locale, @@ -175,6 +178,13 @@ class _InputWidgetState extends State { /// [initialiseWidget] sets initial values of the widget void initialiseWidget() async { + if (widget.autoCountryDetection && + PlatformDispatcher.instance.locale.countryCode != null) { + widget.initialValue = PhoneNumber( + isoCode: PlatformDispatcher.instance.locale.countryCode, + ); + } + if (widget.initialValue != null) { if (widget.initialValue!.phoneNumber != null && widget.initialValue!.phoneNumber!.isNotEmpty && From 4cac249effe65fd4d687818f68d255f0a515ab10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6rkem=20Hac=C4=B1o=C4=9Flu?= <32572262+gorkemhacioglu@users.noreply.github.com> Date: Fri, 5 Jul 2024 20:48:35 +0200 Subject: [PATCH 2/2] Update README.md Added autoCountryDetection --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 13a66f82c0..62614bd2b3 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ InternationalPhoneNumberInput({ this.autoFocus = false, this.autoFocusSearch = false, this.autoValidateMode = AutovalidateMode.disabled, + this.autoCountryDetection = false, this.ignoreBlank = false, this.countrySelectorScrollControlled = true, this.locale, @@ -157,6 +158,7 @@ SelectorConfig({ | maxLength | integer | 15 | | isEnabled | boolean | true | | autoFocus | boolean | false | +| autoCountryDetection | boolean | false | | autoValidateMode | AutoValidateMode | AutoValidateMode.disabled | | formatInput | boolean | true | | errorMessage | String | Invalid phone number |