-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvillage_autocomplete_example.dart
More file actions
165 lines (153 loc) · 5.52 KB
/
Copy pathvillage_autocomplete_example.dart
File metadata and controls
165 lines (153 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import 'package:flutter/material.dart';
import 'package:thai_address_picker/thai_address_picker.dart';
/// ตัวอย่างการใช้งาน VillageAutocomplete widget
/// Example: Village Autocomplete widget usage
class VillageAutocompleteExample extends StatefulWidget {
const VillageAutocompleteExample({super.key});
@override
State<VillageAutocompleteExample> createState() => _VillageAutocompleteExampleState();
}
class _VillageAutocompleteExampleState extends State<VillageAutocompleteExample> {
final _repository = ThaiAddressRepository();
bool _isLoading = true;
String? _error;
VillageSuggestion? _selectedSuggestion;
@override
void initState() {
super.initState();
_init();
}
Future<void> _init() async {
try {
await _repository.initialize();
setState(() => _isLoading = false);
} catch (e) {
setState(() {
_error = e.toString();
_isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
if (_isLoading) {
return Scaffold(
appBar: AppBar(title: const Text('Village Autocomplete')),
body: const Center(child: CircularProgressIndicator()),
);
}
if (_error != null) {
return Scaffold(
appBar: AppBar(title: const Text('Village Autocomplete')),
body: Center(child: Text('Error: $_error')),
);
}
return Scaffold(
appBar: AppBar(title: const Text('Village Autocomplete')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Instructions
Card(
color: Colors.blue.shade50,
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(Icons.info_outline, color: Colors.blue.shade700),
const SizedBox(width: 8),
Text('วิธีใช้งาน', style: Theme.of(context).textTheme.titleMedium?.copyWith(fontWeight: FontWeight.bold)),
],
),
const SizedBox(height: 8),
const Text(
'1. พิมพ์ชื่อหมู่บ้าน (เช่น บ้านสวน)\n'
'2. ระบบจะแนะนำหมู่บ้านที่ตรงกัน\n'
'3. เลือกหมู่บ้านเพื่อดูข้อมูลที่ตั้ง',
),
],
),
),
),
const SizedBox(height: 24),
// Village Autocomplete Widget
VillageAutocomplete(
repository: _repository,
onSuggestionSelected: (suggestion) {
setState(() {
_selectedSuggestion = suggestion;
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'เลือก: ${suggestion.village.nameTh} - '
'${suggestion.subDistrict?.nameTh ?? ''}, '
'${suggestion.district?.nameTh ?? ''}, '
'${suggestion.province?.nameTh ?? ''}',
),
duration: const Duration(seconds: 2),
),
);
},
),
const SizedBox(height: 24),
// Result Display
if (_selectedSuggestion != null) _buildResultCard(),
],
),
),
);
}
Widget _buildResultCard() {
final suggestion = _selectedSuggestion!;
return Card(
elevation: 4,
color: Colors.green.shade50,
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(Icons.check_circle, color: Colors.green.shade700),
const SizedBox(width: 8),
Text(
'ผลลัพธ์ที่เลือก',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18, color: Colors.green.shade700),
),
],
),
const Divider(height: 24),
_buildInfoRow('🏘️ หมู่บ้าน', suggestion.village.nameTh),
const SizedBox(height: 8),
_buildInfoRow('🏠 ตำบล/แขวง', suggestion.subDistrict?.nameTh ?? '-'),
const SizedBox(height: 8),
_buildInfoRow('🏙️ อำเภอ/เขต', suggestion.district?.nameTh ?? '-'),
const SizedBox(height: 8),
_buildInfoRow('📍 จังหวัด', suggestion.province?.nameTh ?? '-'),
],
),
),
);
}
Widget _buildInfoRow(String label, String value) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: 140,
child: Text(label, style: TextStyle(fontSize: 14, color: Colors.grey.shade700)),
),
Expanded(
child: Text(value, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600)),
),
],
);
}
}