'How do I add a country code dropbox in a login page in flutter?

I am trying to add a country code drop box in a login page with the mobile number ,but it is showing some or the other errors .I need an easy approach to add country code drop box with phone number



Solution 1:[1]

Add this to the pubspec.yaml in the dependencies:

    intl_phone_field: ^3.0.1

Import the following package:

   import 'package:intl_phone_field/intl_phone_field.dart';

Add this code:

    IntlPhoneField(
    decoration: InputDecoration(
    labelText: 'Phone Number',
    border: OutlineInputBorder(
        borderSide: BorderSide(),
    ),
),
initialCountryCode: 'IN',
onChanged: (phone) {
    print(phone.completeNumber);
},

)

Solution 2:[2]

Add this to your dependencies in pubsec.yaml

country_code_picker: ^2.0.2

Then import this library

import 'package:country_code_picker/country_code_picker.dart';

Add this code to your main file

               Container(
                    child: CountryCodePicker(
                        initialSelection: 'IN',
                        showCountryOnly: false,
                        showOnlyCountryWhenClosed: false,
                        favorite: ['+91' , 'IN'],
                        enabled: true,
                        hideMainText: false,
                        showFlagMain: true,
                        showFlag: true,
                        hideSearch: false,
                        showFlagDialog: true,
                        alignLeft: true,
                    )
                ),
                Container(
                    padding: const EdgeInsets.all(8),
                    child: TextField(
                        maxLength: 10,
                        controller: nameController,
                        decoration: const InputDecoration(
                            border: OutlineInputBorder(),
                            labelText: 'Mobile Number',
                        ),
                        keyboardType: TextInputType.number,
                        inputFormatters: <TextInputFormatter>[
                            FilteringTextInputFormatter.digitsOnly

                        ]
                    ),
                ),

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bhavik Dalal
Solution 2