ditwin_country_code 0.0.1 copy "ditwin_country_code: ^0.0.1" to clipboard
ditwin_country_code: ^0.0.1 copied to clipboard

A customizable country code picker for Flutter with automatic location-based detection and manual selection.

🌍 Ditwin Country Code Picker #

A customizable country code picker for Flutter with automatic location-based detection and manual selection.


📢 Features #

✅ Fetch country code automatically using GPS (Defaults to India 🇮🇳 if location access is denied).
✅ Search and manually select a country from a list.
✅ Displays country flag alongside dial code.
✅ Uses geolocator and geocoding to determine user location.
✅ Supports Google Fonts (Plus Jakarta Sans).
No Snackbar Alerts when location is denied (Silent fallback to India).


📦 Installation #

1️⃣ Add dependencies to pubspec.yaml #

dependencies:
  flutter:
    sdk: flutter
  google_fonts: ^6.1.0
  geolocator: ^10.1.0
  geocoding: ^2.1.0

2️⃣ Import the package in your Dart file #

import 'package:ditwin_country_picker/ditwin_country_picker.dart';

🔧 Usage #

Basic Implementation #

DitwinCountryCodePicker(
  onChanged: (String dialCode) {
    print("Selected Country Dial Code: $dialCode");
  },
);

With Initial Country Code #

DitwinCountryCodePicker(
  initialCountryCode: "US", // Defaults to United States
  onChanged: (String dialCode) {
    print("Selected Country Dial Code: $dialCode");
  },
);

🛠 API Reference #

DitwinCountryCodePicker #

Property Type Description
onChanged Function(String) Callback triggered when a country is selected.
initialCountryCode String? Pre-selects a country code (Optional).

🚀 Internal Changes #

_getUserLocationAndSetCountry() Updated #

  • Previous Behavior: Showed a Snackbar alert if location permission was denied.
  • New Behavior: Silently falls back to India (IN) without showing a Snackbar.
  • Why? Improves user experience by avoiding unnecessary alerts.

📝 Updated _getUserLocationAndSetCountry()

Future<void> _getUserLocationAndSetCountry() async {
  try {
    Position position = await _determinePosition();
    List<Placemark> placemarks =
        await placemarkFromCoordinates(position.latitude, position.longitude);

    if (placemarks.isNotEmpty) {
      String? countryCode = placemarks.first.isoCountryCode;
      if (countryCode != null) {
        _setCountryFromCode(countryCode);
        return;
      }
    }
  } catch (e) {
    debugPrint("Error getting location: $e");
  }

  // Silent fallback to India (🇮🇳)
  _setCountryFromCode("IN");
}

📄 License #

MIT License - Free for personal & commercial use.


🌟 Author #

👨‍💻 Developed by Prasanth J
📧 Email: jprasanth2006@gmail.com
🔗 LinkedIn: @rjprasanth

0
likes
130
points
26
downloads

Publisher

unverified uploader

Weekly Downloads

A customizable country code picker for Flutter with automatic location-based detection and manual selection.

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

flutter, flutter_screenutil, flutter_svg, geocoding, geolocator, google_fonts, intl

More

Packages that depend on ditwin_country_code