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

Enable "expands" and "maxLines" parameters. #362

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
33 changes: 26 additions & 7 deletions lib/intl_phone_field.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
library intl_phone_field;

import 'dart:async';

import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
@@ -249,6 +250,13 @@ class IntlPhoneField extends StatefulWidget {
/// If null, default magnification configuration will be used.
final TextMagnifierConfiguration? magnifierConfiguration;

/// [expands] If set to true and wrapped in a parent widget like Expanded or SizedBox,
/// the input will expand to fill the parent.
final bool expands;

/// define how many lines that you need for the IntlPhoneField (by default set by 1)
final int? maxLines;

const IntlPhoneField({
Key? key,
this.formFieldKey,
@@ -278,7 +286,8 @@ class IntlPhoneField extends StatefulWidget {
this.inputFormatters,
this.enabled = true,
this.keyboardAppearance,
@Deprecated('Use searchFieldInputDecoration of PickerDialogStyle instead') this.searchText = 'Search country',
@Deprecated('Use searchFieldInputDecoration of PickerDialogStyle instead')
this.searchText = 'Search country',
this.dropdownIconPosition = IconPosition.leading,
this.dropdownIcon = const Icon(Icons.arrow_drop_down),
this.autofocus = false,
@@ -296,6 +305,8 @@ class IntlPhoneField extends StatefulWidget {
this.pickerDialogStyle,
this.flagsButtonMargin = EdgeInsets.zero,
this.magnifierConfiguration,
this.expands = false,
this.maxLines = 1,
}) : super(key: key);

@override
@@ -319,20 +330,24 @@ class _IntlPhoneFieldState extends State<IntlPhoneField> {
if (widget.initialCountryCode == null && number.startsWith('+')) {
number = number.substring(1);
// parse initial value
_selectedCountry = countries.firstWhere((country) => number.startsWith(country.fullCountryCode),
_selectedCountry = countries.firstWhere(
(country) => number.startsWith(country.fullCountryCode),
orElse: () => _countryList.first);

// remove country code from the initial number value
number = number.replaceFirst(RegExp("^${_selectedCountry.fullCountryCode}"), "");
} else {
_selectedCountry = _countryList.firstWhere((item) => item.code == (widget.initialCountryCode ?? 'US'),
_selectedCountry = _countryList.firstWhere(
(item) => item.code == (widget.initialCountryCode ?? 'US'),
orElse: () => _countryList.first);

// remove country code from the initial number value
if (number.startsWith('+')) {
number = number.replaceFirst(RegExp("^\\+${_selectedCountry.fullCountryCode}"), "");
number =
number.replaceFirst(RegExp("^\\+${_selectedCountry.fullCountryCode}"), "");
} else {
number = number.replaceFirst(RegExp("^${_selectedCountry.fullCountryCode}"), "");
number =
number.replaceFirst(RegExp("^${_selectedCountry.fullCountryCode}"), "");
}
}

@@ -384,7 +399,8 @@ class _IntlPhoneFieldState extends State<IntlPhoneField> {
return TextFormField(
key: widget.formFieldKey,
initialValue: (widget.controller == null) ? number : null,
autofillHints: widget.disableAutoFillHints ? null : [AutofillHints.telephoneNumberNational],
autofillHints:
widget.disableAutoFillHints ? null : [AutofillHints.telephoneNumberNational],
readOnly: widget.readOnly,
obscureText: widget.obscureText,
textAlign: widget.textAlign,
@@ -398,6 +414,8 @@ class _IntlPhoneFieldState extends State<IntlPhoneField> {
cursorWidth: widget.cursorWidth,
showCursor: widget.showCursor,
onFieldSubmitted: widget.onSubmitted,
expands: widget.expands,
maxLines: widget.maxLines,
magnifierConfiguration: widget.magnifierConfiguration,
decoration: widget.decoration.copyWith(
prefixIcon: _buildFlagsButton(),
@@ -429,7 +447,8 @@ class _IntlPhoneFieldState extends State<IntlPhoneField> {
validator: (value) {
if (value == null || !isNumeric(value)) return validatorMessage;
if (!widget.disableLengthCheck) {
return value.length >= _selectedCountry.minLength && value.length <= _selectedCountry.maxLength
return value.length >= _selectedCountry.minLength &&
value.length <= _selectedCountry.maxLength
? null
: widget.invalidNumberMessage;
}