1 | initial version |
You can achieve this by using the 'intlphonefield' package.
Here are the steps to add a country picker to a Flutter mobile text field:
Add the 'intlphonefield' package to your project by adding the following line to your pubspec.yaml file:
dependencies:
intl_phone_field: ^0.1.4
Run 'flutter pub get' to install the package.
Import the package in the file where you want to use it:
import 'package:intl_phone_field/intl_phone_field.dart';
Create a new 'IntlPhoneField' widget and add it to your widget tree. You can customize the widget by changing the properties like decoration, initialValue, etc. Here's an example:
IntlPhoneField(
decoration: InputDecoration(
labelText: 'Phone number',
border: OutlineInputBorder(
borderSide: BorderSide(),
),
),
initialCountryCode: 'US',
onChanged: (phone) {
print(phone.completeNumber);
},
)
This will create a text field with a country picker. The 'initialCountryCode' property sets the default country code to be displayed in the picker. The 'onChanged' callback will be called whenever the user enters or changes a phone number in the text field.
Save the file and run the app to see the country picker in action.