🌍 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