This repository was archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 459
/
Copy pathcountry_code.dart
72 lines (57 loc) · 1.88 KB
/
country_code.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
import 'package:collection/collection.dart' show IterableExtension;
import 'package:country_code_picker/country_codes.dart';
import 'package:country_code_picker/country_localizations.dart';
import 'package:flutter/cupertino.dart';
mixin ToAlias {}
@deprecated
class CElement = CountryCode with ToAlias;
/// Country element. This is the element that contains all the information
class CountryCode {
/// the name of the country
String? name;
/// the flag of the country
final String? flagUri;
/// the country code (IT,AF..)
final String? code;
/// the dial code (+39,+93..)
final String? dialCode;
CountryCode({
this.name,
this.flagUri,
this.code,
this.dialCode,
});
@Deprecated('Use `fromCountryCode` instead.')
factory CountryCode.fromCode(String isoCode) {
return CountryCode.fromCountryCode(isoCode);
}
factory CountryCode.fromCountryCode(String countryCode) {
final Map<String, String>? jsonCode = codes.firstWhereOrNull(
(code) => code['code'] == countryCode,
);
return CountryCode.fromJson(jsonCode!);
}
factory CountryCode.fromDialCode(String dialCode) {
final Map<String, String>? jsonCode = codes.firstWhereOrNull(
(code) => code['dial_code'] == dialCode,
);
return CountryCode.fromJson(jsonCode!);
}
CountryCode localize(BuildContext context) {
return this
..name =
CountryLocalizations.of(context)?.translate(this.code) ?? this.name;
}
factory CountryCode.fromJson(Map<String, dynamic> json) {
return CountryCode(
name: json['name'] is List ? json['name'].first : json['name'],
code: json['code'],
dialCode: json['dial_code'],
flagUri: 'flags/${json['code'].toLowerCase()}.png',
);
}
@override
String toString() => "$dialCode";
String toLongString() => "$dialCode ${toCountryStringOnly()}";
String toCountryStringOnly() => name ?? '';
}