indonesian_address_picker 0.1.2
indonesian_address_picker: ^0.1.2 copied to clipboard
Modern Indonesian address/region picker with province, city, district, and village data. Searchable, customizable, and offline-first.
import 'package:flutter/material.dart';
import 'package:indonesian_address_picker/indonesian_address_picker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Indonesian Address Picker',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xFF2563EB),
brightness: Brightness.light,
),
),
home: const DemoPage(),
);
}
}
class DemoPage extends StatefulWidget {
const DemoPage({Key? key}) : super(key: key);
@override
State<DemoPage> createState() => _DemoPageState();
}
class _DemoPageState extends State<DemoPage> with TickerProviderStateMixin {
Province? _selectedProvince;
City? _selectedCity;
District? _selectedDistrict;
Village? _selectedVillage;
late AnimationController _animationController;
late Animation<double> _fadeAnimation;
@override
void initState() {
super.initState();
_animationController = AnimationController(
duration: const Duration(milliseconds: 800),
vsync: this,
);
_fadeAnimation = CurvedAnimation(
parent: _animationController,
curve: Curves.easeInOut,
);
_animationController.forward();
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
void _showProvincePicker() {
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (context) => Container(
height: MediaQuery.of(context).size.height * 0.85,
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.vertical(top: Radius.circular(25)),
),
child: ProvincePicker(
onSelected: (province) {
setState(() {
_selectedProvince = province;
_selectedCity = null;
_selectedDistrict = null;
_selectedVillage = null;
});
},
),
),
);
}
void _showCityPicker() {
if (_selectedProvince == null) {
_showErrorSnackBar('Pilih provinsi terlebih dahulu');
return;
}
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (context) => Container(
height: MediaQuery.of(context).size.height * 0.85,
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.vertical(top: Radius.circular(25)),
),
child: CityPicker(
provinceId: _selectedProvince!.id,
onSelected: (city) {
setState(() {
_selectedCity = city;
_selectedDistrict = null;
_selectedVillage = null;
});
},
),
),
);
}
void _showDistrictPicker() {
if (_selectedCity == null) {
_showErrorSnackBar('Pilih kota terlebih dahulu');
return;
}
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (context) => Container(
height: MediaQuery.of(context).size.height * 0.85,
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.vertical(top: Radius.circular(25)),
),
child: DistrictPicker(
cityId: _selectedCity!.id,
onSelected: (district) {
setState(() {
_selectedDistrict = district;
_selectedVillage = null;
});
},
),
),
);
}
void _showVillagePicker() {
if (_selectedDistrict == null) {
_showErrorSnackBar('Pilih kecamatan terlebih dahulu');
return;
}
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (context) => Container(
height: MediaQuery.of(context).size.height * 0.85,
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.vertical(top: Radius.circular(25)),
),
child: VillagePicker(
districtId: _selectedDistrict!.id,
onSelected: (village) {
setState(() {
_selectedVillage = village;
});
_showSuccessSnackBar('Alamat lengkap berhasil dipilih!');
},
),
),
);
}
void _showErrorSnackBar(String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Row(
children: [
const Icon(Icons.error_outline, color: Colors.white),
const SizedBox(width: 12),
Expanded(child: Text(message)),
],
),
backgroundColor: Colors.red[600],
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
margin: const EdgeInsets.all(16),
),
);
}
void _showSuccessSnackBar(String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Row(
children: [
const Icon(Icons.check_circle, color: Colors.white),
const SizedBox(width: 12),
Expanded(child: Text(message)),
],
),
backgroundColor: Colors.green[600],
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
margin: const EdgeInsets.all(16),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [const Color(0xFF2563EB), const Color(0xFF7C3AED)],
),
),
child: SafeArea(
child: Column(
children: [
_buildPremiumHeader(),
// Content Area
Expanded(
child: Container(
margin: const EdgeInsets.only(top: 20),
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.vertical(
top: Radius.circular(30),
),
),
child: FadeTransition(
opacity: _fadeAnimation,
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Selection Cards
_buildSelectionCard(
icon: Icons.public,
label: 'Provinsi',
value: _selectedProvince?.name,
onTap: _showProvincePicker,
gradient: const LinearGradient(
colors: [Color(0xFF2563EB), Color(0xFF3B82F6)],
),
),
const SizedBox(height: 16),
_buildSelectionCard(
icon: Icons.location_city,
label: 'Kabupaten/Kota',
value: _selectedCity?.name,
onTap: _showCityPicker,
enabled: _selectedProvince != null,
gradient: const LinearGradient(
colors: [Color(0xFF7C3AED), Color(0xFF9333EA)],
),
),
const SizedBox(height: 16),
_buildSelectionCard(
icon: Icons.map,
label: 'Kecamatan',
value: _selectedDistrict?.name,
onTap: _showDistrictPicker,
enabled: _selectedCity != null,
gradient: const LinearGradient(
colors: [Color(0xFFEC4899), Color(0xFFF43F5E)],
),
),
const SizedBox(height: 16),
_buildSelectionCard(
icon: Icons.home_rounded,
label: 'Kelurahan/Desa',
value: _selectedVillage?.name,
onTap: _showVillagePicker,
enabled: _selectedDistrict != null,
gradient: const LinearGradient(
colors: [Color(0xFF10B981), Color(0xFF059669)],
),
),
const SizedBox(height: 32),
// Summary Card
if (_selectedProvince != null) _buildSummaryCard(),
const SizedBox(height: 24),
// Cascade Demo Button
_buildCascadeDemoButton(),
const SizedBox(height: 32),
// Footer Info
_buildFooterInfo(),
],
),
),
),
),
),
],
),
),
),
);
}
Widget _buildPremiumHeader() {
return Padding(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.2),
borderRadius: BorderRadius.circular(15),
),
child: const Icon(
Icons.location_on,
color: Colors.white,
size: 28,
),
),
const Spacer(),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 6,
),
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.2),
borderRadius: BorderRadius.circular(20),
),
child: const Row(
children: [
Icon(Icons.verified, color: Colors.white, size: 16),
SizedBox(width: 6),
Text(
'Premium',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w600,
fontSize: 12,
),
),
],
),
),
],
),
const SizedBox(height: 24),
const Text(
'Indonesian\nAddress Picker',
style: TextStyle(
color: Colors.white,
fontSize: 32,
fontWeight: FontWeight.bold,
height: 1.2,
),
),
const SizedBox(height: 12),
Text(
'🇮🇩 Complete 4-level address selection\nwith 83,000+ villages',
style: TextStyle(
color: Colors.white.withOpacity(0.9),
fontSize: 14,
height: 1.5,
),
),
],
),
);
}
Widget _buildSelectionCard({
required IconData icon,
required String label,
String? value,
required VoidCallback onTap,
bool enabled = true,
required Gradient gradient,
}) {
return Material(
color: Colors.transparent,
child: InkWell(
onTap: enabled ? onTap : null,
borderRadius: BorderRadius.circular(20),
child: Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: enabled ? Colors.white : Colors.grey[100],
borderRadius: BorderRadius.circular(20),
border: Border.all(
color: enabled ? Colors.grey[200]! : Colors.grey[300]!,
width: 1.5,
),
boxShadow: enabled
? [
BoxShadow(
color: Colors.black.withOpacity(0.05),
blurRadius: 10,
offset: const Offset(0, 4),
),
]
: [],
),
child: Row(
children: [
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
gradient: enabled ? gradient : null,
color: enabled ? null : Colors.grey[300],
borderRadius: BorderRadius.circular(12),
),
child: Icon(icon, color: Colors.white, size: 24),
),
const SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: TextStyle(
fontSize: 12,
color: Colors.grey[600],
fontWeight: FontWeight.w500,
),
),
const SizedBox(height: 4),
Text(
value ?? 'Pilih $label',
style: TextStyle(
fontSize: 16,
color: value != null
? Colors.black87
: Colors.grey[400],
fontWeight: value != null
? FontWeight.w600
: FontWeight.normal,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
),
Icon(
Icons.chevron_right_rounded,
color: enabled ? Colors.grey[400] : Colors.grey[300],
size: 28,
),
],
),
),
),
);
}
Widget _buildSummaryCard() {
return Container(
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
gradient: const LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Color(0xFF10B981), Color(0xFF059669)],
),
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: const Color(0xFF10B981).withOpacity(0.3),
blurRadius: 20,
offset: const Offset(0, 8),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.2),
borderRadius: BorderRadius.circular(10),
),
child: const Icon(
Icons.check_circle_rounded,
color: Colors.white,
size: 20,
),
),
const SizedBox(width: 12),
const Text(
'Alamat Terpilih',
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
],
),
const SizedBox(height: 20),
_buildSummaryRow('Provinsi', _selectedProvince?.name),
if (_selectedCity != null)
_buildSummaryRow('Kota', _selectedCity?.name),
if (_selectedDistrict != null)
_buildSummaryRow('Kecamatan', _selectedDistrict?.name),
if (_selectedVillage != null)
_buildSummaryRow('Kelurahan/Desa', _selectedVillage?.name),
],
),
);
}
Widget _buildSummaryRow(String label, String? value) {
if (value == null) return const SizedBox.shrink();
return Padding(
padding: const EdgeInsets.only(bottom: 12),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: 110,
child: Text(
label,
style: TextStyle(
color: Colors.white.withOpacity(0.8),
fontSize: 13,
),
),
),
const Text(': ', style: TextStyle(color: Colors.white, fontSize: 13)),
Expanded(
child: Text(
value,
style: const TextStyle(
color: Colors.white,
fontSize: 13,
fontWeight: FontWeight.w600,
),
),
),
],
),
);
}
Widget _buildCascadeDemoButton() {
return Container(
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Color(0xFF2563EB), Color(0xFF7C3AED)],
),
borderRadius: BorderRadius.circular(16),
boxShadow: [
BoxShadow(
color: const Color(0xFF2563EB).withOpacity(0.3),
blurRadius: 20,
offset: const Offset(0, 8),
),
],
),
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const CascadeDemoPage()),
);
},
borderRadius: BorderRadius.circular(16),
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 18, horizontal: 24),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(width: 12),
const Text(
'Try Cascade Picker',
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
],
),
),
),
),
);
}
Widget _buildFooterInfo() {
return Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_buildFeatureBadge(Icons.offline_bolt, '100% Offline'),
const SizedBox(width: 12),
_buildFeatureBadge(Icons.speed, 'Fast & Light'),
],
),
const SizedBox(height: 16),
Text(
'Made with ❤️ for Doni Sw',
style: TextStyle(color: Colors.grey[500], fontSize: 12),
textAlign: TextAlign.center,
),
],
);
}
Widget _buildFeatureBadge(IconData icon, String label) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
decoration: BoxDecoration(
color: Colors.grey[100],
borderRadius: BorderRadius.circular(20),
border: Border.all(color: Colors.grey[300]!),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(icon, size: 16, color: Colors.grey[700]),
const SizedBox(width: 6),
Text(
label,
style: TextStyle(
fontSize: 11,
color: Colors.grey[700],
fontWeight: FontWeight.w600,
),
),
],
),
);
}
}
class CascadeDemoPage extends StatelessWidget {
const CascadeDemoPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[50],
appBar: AppBar(
title: const Text(
'Cascade Picker Demo',
style: TextStyle(fontWeight: FontWeight.bold),
),
centerTitle: true,
elevation: 0,
flexibleSpace: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xFF2563EB), Color(0xFF7C3AED)],
),
),
),
),
body: CascadeAddressPicker(
onComplete: (province, city, district, village) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Row(
children: [
const Icon(Icons.celebration, color: Colors.white),
const SizedBox(width: 12),
Expanded(
child: Text(
'Perfect! ${village?.name ?? city.name} selected',
style: const TextStyle(fontWeight: FontWeight.w600),
),
),
],
),
backgroundColor: Colors.green[600],
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
margin: const EdgeInsets.all(16),
duration: const Duration(seconds: 3),
),
);
},
),
);
}
}