-
Notifications
You must be signed in to change notification settings - Fork 90
/
main.dart
325 lines (309 loc) · 11.4 KB
/
main.dart
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import 'package:flutter/material.dart';
import 'package:phone_form_field/phone_form_field.dart';
void main() {
runApp(const MyApp());
}
// this example makes uses of lots of properties that would not be there
// in a real scenario for the sake of showing the features.
// For a simpler example see the README
class PhoneFieldView extends StatelessWidget {
static const supportedLocales = [
Locale('ar'),
// not supported by material yet
// Locale('ckb'),
Locale('de'),
Locale('el'),
Locale('en'),
Locale('es'),
Locale('fa'),
Locale('fr'),
Locale('hi'),
Locale('hu'),
Locale('it'),
// not supported by material yet
// Locale('ku'),
Locale('nb'),
Locale('nl'),
Locale('pt'),
Locale('ru'),
Locale('sv'),
Locale('tr'),
Locale('uz'),
Locale('zh'),
// ...
];
final PhoneController controller;
final FocusNode focusNode;
final CountrySelectorNavigator selectorNavigator;
final bool withLabel;
final bool outlineBorder;
final bool isCountryButtonPersistant;
final bool mobileOnly;
final Locale locale;
const PhoneFieldView({
Key? key,
required this.controller,
required this.focusNode,
required this.selectorNavigator,
required this.withLabel,
required this.outlineBorder,
required this.isCountryButtonPersistant,
required this.mobileOnly,
required this.locale,
}) : super(key: key);
PhoneNumberInputValidator? _getValidator(BuildContext context) {
List<PhoneNumberInputValidator> validators = [];
if (mobileOnly) {
validators.add(PhoneValidator.validMobile(context));
} else {
validators.add(PhoneValidator.valid(context));
}
return validators.isNotEmpty ? PhoneValidator.compose(validators) : null;
}
@override
Widget build(BuildContext context) {
return AutofillGroup(
child: Localizations.override(
context: context,
locale: locale,
child: Builder(
builder: (context) {
final label = PhoneFieldLocalization.of(context).phoneNumber;
return PhoneFormField(
focusNode: focusNode,
controller: controller,
isCountryButtonPersistent: isCountryButtonPersistant,
autofocus: false,
autofillHints: const [AutofillHints.telephoneNumber],
countrySelectorNavigator: selectorNavigator,
decoration: InputDecoration(
label: withLabel ? Text(label) : null,
border: outlineBorder
? const OutlineInputBorder()
: const UnderlineInputBorder(),
hintText: withLabel ? '' : label,
),
enabled: true,
countryButtonStyle: const CountryButtonStyle(
showFlag: true,
showIsoCode: false,
showDialCode: true,
showDropdownIcon: true,
),
validator: _getValidator(context),
autovalidateMode: AutovalidateMode.onUserInteraction,
cursorColor: Theme.of(context).colorScheme.primary,
// ignore: avoid_print
onSaved: (p) => print('saved $p'),
// ignore: avoid_print
onChanged: (p) => print('changed $p'),
);
},
),
),
);
}
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: PhoneFieldLocalization.delegates,
supportedLocales: PhoneFieldView.supportedLocales,
locale: const Locale('en'),
title: 'Phone field demo',
theme: ThemeData(
brightness: Brightness.dark,
primarySwatch: Colors.blue,
),
home: const PhoneFormFieldScreen(),
);
}
}
class PhoneFormFieldScreen extends StatefulWidget {
const PhoneFormFieldScreen({Key? key}) : super(key: key);
@override
PhoneFormFieldScreenState createState() => PhoneFormFieldScreenState();
}
class PhoneFormFieldScreenState extends State<PhoneFormFieldScreen> {
late PhoneController controller;
final FocusNode focusNode = FocusNode();
bool outlineBorder = true;
bool mobileOnly = true;
bool isCountryButtonPersistent = true;
bool withLabel = true;
CountrySelectorNavigator selectorNavigator =
const CountrySelectorNavigator.page();
Locale locale = const Locale('en');
final formKey = GlobalKey<FormState>();
@override
initState() {
super.initState();
controller = PhoneController();
controller.addListener(() => setState(() {}));
}
@override
void dispose() {
super.dispose();
controller.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Phone_form_field'),
),
body: SingleChildScrollView(
child: Center(
child: Container(
constraints: const BoxConstraints(maxWidth: 600),
child: Card(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
SwitchListTile(
value: outlineBorder,
onChanged: (v) => setState(() => outlineBorder = v),
title: const Text('Outlined border'),
),
SwitchListTile(
value: withLabel,
onChanged: (v) => setState(() => withLabel = v),
title: const Text('Label'),
),
SwitchListTile(
value: isCountryButtonPersistent,
onChanged: (v) =>
setState(() => isCountryButtonPersistent = v),
title: const Text('Persistent country chip'),
),
SwitchListTile(
value: mobileOnly,
onChanged: (v) => setState(() => mobileOnly = v),
title: const Text('Mobile phone number only'),
),
ListTile(
title: Wrap(
alignment: WrapAlignment.spaceBetween,
crossAxisAlignment: WrapCrossAlignment.center,
children: [
const Text('Language: '),
DropdownButton<Locale>(
value: locale,
onChanged: (Locale? value) {
if (value != null) {
setState(() => locale = value);
}
},
items: [
for (final locale
in PhoneFieldView.supportedLocales)
DropdownMenuItem(
value: locale,
child: Text(locale.toLanguageTag()),
),
],
),
],
),
),
ListTile(
title: Wrap(
alignment: WrapAlignment.spaceBetween,
crossAxisAlignment: WrapCrossAlignment.center,
children: [
const Text('Country selector: '),
DropdownButton<CountrySelectorNavigator>(
value: selectorNavigator,
onChanged: (CountrySelectorNavigator? value) {
if (value != null) {
setState(() => selectorNavigator = value);
}
},
items: const [
DropdownMenuItem(
value: CountrySelectorNavigator.bottomSheet(
favorites: [IsoCode.GU, IsoCode.GY]),
child: Text('Bottom sheet'),
),
DropdownMenuItem(
value: CountrySelectorNavigator
.draggableBottomSheet(),
child: Text('Draggable modal sheet'),
),
DropdownMenuItem(
value:
CountrySelectorNavigator.modalBottomSheet(),
child: Text('Modal sheet'),
),
DropdownMenuItem(
value: CountrySelectorNavigator.dialog(
width: 720,
),
child: Text('Dialog'),
),
DropdownMenuItem(
value: CountrySelectorNavigator.page(),
child: Text('Page'),
),
],
),
],
),
),
const SizedBox(height: 40),
Form(
key: formKey,
child: Column(
children: [
PhoneFieldView(
controller: controller,
focusNode: focusNode,
selectorNavigator: selectorNavigator,
withLabel: withLabel,
outlineBorder: outlineBorder,
isCountryButtonPersistant:
isCountryButtonPersistent,
mobileOnly: mobileOnly,
locale: locale,
),
],
),
),
const SizedBox(height: 12),
Text(controller.value.toString()),
Text('is valid mobile number '
'${controller.value.isValid(type: PhoneNumberType.mobile)}'),
Text(
'is valid fixed line number ${controller.value.isValid(type: PhoneNumberType.fixedLine)}'),
const SizedBox(height: 12),
ElevatedButton(
onPressed: () => formKey.currentState?.reset(),
child: const Text('reset'),
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: () {
controller.selectNationalNumber();
focusNode.requestFocus();
},
child: const Text('Select national number'),
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: () => controller.value =
PhoneNumber.parse('+33 699 999 999'),
child: const Text('Set +33 699 999 999'),
),
],
),
),
),
),
),
),
);
}
}