-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip_code_autocomplete_example.dart
More file actions
172 lines (159 loc) · 5.62 KB
/
Copy pathzip_code_autocomplete_example.dart
File metadata and controls
172 lines (159 loc) · 5.62 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
166
167
168
169
170
171
172
import 'package:flutter/material.dart';
import 'package:thai_address_picker/thai_address_picker.dart';
/// ตัวอย่างการใช้งาน ZipCodeAutocomplete widget
/// Example: ZipCode Autocomplete widget usage
class ZipCodeAutocompleteExample extends StatefulWidget {
const ZipCodeAutocompleteExample({super.key});
@override
State<ZipCodeAutocompleteExample> createState() => _ZipCodeAutocompleteExampleState();
}
class _ZipCodeAutocompleteExampleState extends State<ZipCodeAutocompleteExample> {
final _repository = ThaiAddressRepository();
final _searchController = TextEditingController();
bool _isLoading = true;
String? _error;
ZipCodeSuggestion? _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
void dispose() {
_searchController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
if (_isLoading) {
return Scaffold(
appBar: AppBar(title: const Text('Zip Code Autocomplete')),
body: const Center(child: CircularProgressIndicator()),
);
}
if (_error != null) {
return Scaffold(
appBar: AppBar(title: const Text('Zip Code Autocomplete')),
body: Center(child: Text('Error: $_error')),
);
}
return Scaffold(
appBar: AppBar(title: const Text('Zip Code 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. พิมพ์รหัสไปรษณีย์ (เช่น 10110)\n'
'2. ระบบจะแนะนำรหัสไปรษณีย์ที่ตรงกัน\n'
'3. เลือกรหัสเพื่อดูข้อมูลพื้นที่',
),
],
),
),
),
const SizedBox(height: 24),
// Zip Code Autocomplete Widget
ZipCodeAutocomplete(
repository: _repository,
onSuggestionSelected: (suggestion) {
setState(() {
_selectedSuggestion = suggestion;
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'เลือก: ${suggestion.zipCode} - '
'${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.zipCode),
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)),
),
],
);
}
}