Skip to content
Open
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
6 changes: 4 additions & 2 deletions lib/country_picker_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class CountryPickerDialog extends StatefulWidget {
final List<Country> filteredCountries;
final PickerDialogStyle? style;
final String languageCode;
final double flagSize;

const CountryPickerDialog({
Key? key,
Expand All @@ -55,6 +56,7 @@ class CountryPickerDialog extends StatefulWidget {
required this.onCountryChanged,
required this.selectedCountry,
required this.filteredCountries,
required this.flagSize,
this.style,
}) : super(key: key);

Expand Down Expand Up @@ -124,11 +126,11 @@ class _CountryPickerDialogState extends State<CountryPickerDialog> {
? Image.asset(
'assets/flags/${_filteredCountries[index].code.toLowerCase()}.png',
package: 'intl_phone_field',
width: 32,
width: widget.flagSize,
)
: Text(
_filteredCountries[index].flag,
style: const TextStyle(fontSize: 18),
style: TextStyle(fontSize: widget.flagSize),
),
contentPadding: widget.style?.listTilePadding,
title: Text(
Expand Down
139 changes: 75 additions & 64 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';
Expand Down Expand Up @@ -249,54 +250,63 @@ class IntlPhoneField extends StatefulWidget {
/// If null, default magnification configuration will be used.
final TextMagnifierConfiguration? magnifierConfiguration;

const IntlPhoneField({
Key? key,
this.formFieldKey,
this.initialCountryCode,
this.languageCode = 'en',
this.disableAutoFillHints = false,
this.obscureText = false,
this.textAlign = TextAlign.left,
this.textAlignVertical,
this.onTap,
this.readOnly = false,
this.initialValue,
this.keyboardType = TextInputType.phone,
this.controller,
this.focusNode,
this.decoration = const InputDecoration(),
this.style,
this.dropdownTextStyle,
this.onSubmitted,
this.validator,
this.onChanged,
this.countries,
this.onCountryChanged,
this.onSaved,
this.showDropdownIcon = true,
this.dropdownDecoration = const BoxDecoration(),
this.inputFormatters,
this.enabled = true,
this.keyboardAppearance,
@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,
this.textInputAction,
this.autovalidateMode = AutovalidateMode.onUserInteraction,
this.showCountryFlag = true,
this.cursorColor,
this.disableLengthCheck = false,
this.flagsButtonPadding = EdgeInsets.zero,
this.invalidNumberMessage = 'Invalid Mobile Number',
this.cursorHeight,
this.cursorRadius = Radius.zero,
this.cursorWidth = 2.0,
this.showCursor = true,
this.pickerDialogStyle,
this.flagsButtonMargin = EdgeInsets.zero,
this.magnifierConfiguration,
}) : super(key: key);
/// Size of the country flag.
///
/// On web, this controls the image width.
/// On mobile, this controls the emoji font size.
///
/// Defaults to 32.0 on web and 18.0 on mobile.
final double? flagSize;

const IntlPhoneField(
{Key? key,
this.formFieldKey,
this.initialCountryCode,
this.languageCode = 'en',
this.disableAutoFillHints = false,
this.obscureText = false,
this.textAlign = TextAlign.left,
this.textAlignVertical,
this.onTap,
this.readOnly = false,
this.initialValue,
this.keyboardType = TextInputType.phone,
this.controller,
this.focusNode,
this.decoration = const InputDecoration(),
this.style,
this.dropdownTextStyle,
this.onSubmitted,
this.validator,
this.onChanged,
this.countries,
this.onCountryChanged,
this.onSaved,
this.showDropdownIcon = true,
this.dropdownDecoration = const BoxDecoration(),
this.inputFormatters,
this.enabled = true,
this.keyboardAppearance,
@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,
this.textInputAction,
this.autovalidateMode = AutovalidateMode.onUserInteraction,
this.showCountryFlag = true,
this.cursorColor,
this.disableLengthCheck = false,
this.flagsButtonPadding = EdgeInsets.zero,
this.invalidNumberMessage = 'Invalid Mobile Number',
this.cursorHeight,
this.cursorRadius = Radius.zero,
this.cursorWidth = 2.0,
this.showCursor = true,
this.pickerDialogStyle,
this.flagsButtonMargin = EdgeInsets.zero,
this.magnifierConfiguration,
this.flagSize})
: super(key: key);

@override
State<IntlPhoneField> createState() => _IntlPhoneFieldState();
Expand Down Expand Up @@ -355,25 +365,25 @@ class _IntlPhoneFieldState extends State<IntlPhoneField> {
}
}

Future<void> _changeCountry() async {
Future<void> _changeCountry({required double flagSize}) async {
filteredCountries = _countryList;
await showDialog(
context: context,
useRootNavigator: false,
builder: (context) => StatefulBuilder(
builder: (ctx, setState) => CountryPickerDialog(
languageCode: widget.languageCode.toLowerCase(),
style: widget.pickerDialogStyle,
filteredCountries: filteredCountries,
searchText: widget.searchText,
countryList: _countryList,
selectedCountry: _selectedCountry,
onCountryChanged: (Country country) {
_selectedCountry = country;
widget.onCountryChanged?.call(country);
setState(() {});
},
),
languageCode: widget.languageCode.toLowerCase(),
style: widget.pickerDialogStyle,
filteredCountries: filteredCountries,
searchText: widget.searchText,
countryList: _countryList,
selectedCountry: _selectedCountry,
onCountryChanged: (Country country) {
_selectedCountry = country;
widget.onCountryChanged?.call(country);
setState(() {});
},
flagSize: flagSize),
),
);
if (mounted) setState(() {});
Expand Down Expand Up @@ -448,13 +458,14 @@ class _IntlPhoneFieldState extends State<IntlPhoneField> {
}

Container _buildFlagsButton() {
final double effectiveFlagSize = widget.flagSize ?? (kIsWeb ? 32.0 : 18.0);
return Container(
margin: widget.flagsButtonMargin,
child: DecoratedBox(
decoration: widget.dropdownDecoration,
child: InkWell(
borderRadius: widget.dropdownDecoration.borderRadius as BorderRadius?,
onTap: widget.enabled ? _changeCountry : null,
onTap: widget.enabled ? () => _changeCountry(flagSize: effectiveFlagSize) : null,
child: Padding(
padding: widget.flagsButtonPadding,
child: Row(
Expand All @@ -475,11 +486,11 @@ class _IntlPhoneFieldState extends State<IntlPhoneField> {
? Image.asset(
'assets/flags/${_selectedCountry.code.toLowerCase()}.png',
package: 'intl_phone_field',
width: 32,
width: effectiveFlagSize,
)
: Text(
_selectedCountry.flag,
style: const TextStyle(fontSize: 18),
style: TextStyle(fontSize: effectiveFlagSize),
),
const SizedBox(width: 8),
],
Expand Down