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

Implemented auto country detection #433

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 |
Expand Down
12 changes: 11 additions & 1 deletion lib/src/widgets/input_widget.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -175,6 +178,13 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {

/// [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 &&
Expand Down