Files
CountryCodePicker/lib/country_code.dart
Salvatore Giordano 01aa7502cc clean name
2021-03-13 12:37:04 +01:00

79 lines
1.9 KiB
Dart

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'],
code: json['code'],
dialCode: json['dial_code'],
flagUri: 'flags/${json['code'].toLowerCase()}.png',
);
}
@override
String toString() => "$dialCode";
String toLongString() => "$dialCode ${toCountryStringOnly()}";
String toCountryStringOnly() {
return '$_cleanName';
}
String? get _cleanName {
return name?.replaceAll(RegExp(r'[[\]]'), '').split(',').first;
}
}