Learn how to use the country_names package in your Flutter applications.
Add the package to your pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
country_names: ^1.1.2Run flutter pub get to install the package.
Import the package and use the CountryListView widget:
import 'package:flutter/material.dart';
import 'package:country_names/country_names.dart';
class CountrySelector extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Select a Country')),
body: CountryListView(
onSelect: (country) {
print('Selected: ${country.name} (${country.code})');
Navigator.pop(context, country);
},
),
);
}
}You can customize the appearance of each country item using the itemBuilder parameter:
CountryListView(
onSelect: (country) {
// Handle country selection
},
itemBuilder: (context, country) {
return Card(
margin: EdgeInsets.symmetric(vertical: 4, horizontal: 8),
child: Padding(
padding: EdgeInsets.all(8.0),
child: Row(
children: [
Image(
image: AssetImage(country.flag),
width: 48,
height: 32,
),
SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
country.name,
style: TextStyle(fontWeight: FontWeight.bold),
),
Text(country.code),
],
),
),
],
),
),
);
},
)