@@ -1,5 +1,6 @@
|
|||||||
import 'package:collection/collection.dart' show IterableExtension;
|
import 'package:collection/collection.dart' show IterableExtension;
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
|
import 'package:flutter/foundation.dart' show kDebugMode;
|
||||||
|
|
||||||
import 'country_codes.dart';
|
import 'country_codes.dart';
|
||||||
import 'country_localizations.dart';
|
import 'country_localizations.dart';
|
||||||
@@ -39,6 +40,15 @@ class CountryCode {
|
|||||||
return CountryCode.fromJson(jsonCode!);
|
return CountryCode.fromJson(jsonCode!);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static CountryCode? tryFromCountryCode(String countryCode) {
|
||||||
|
try {
|
||||||
|
return CountryCode.fromCountryCode(countryCode);
|
||||||
|
} catch (e) {
|
||||||
|
if (kDebugMode) print('Failed to recognize country from countryCode: $countryCode');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
factory CountryCode.fromDialCode(String dialCode) {
|
factory CountryCode.fromDialCode(String dialCode) {
|
||||||
final Map<String, String>? jsonCode = codes.firstWhereOrNull(
|
final Map<String, String>? jsonCode = codes.firstWhereOrNull(
|
||||||
(code) => code['dial_code'] == dialCode,
|
(code) => code['dial_code'] == dialCode,
|
||||||
@@ -46,6 +56,15 @@ class CountryCode {
|
|||||||
return CountryCode.fromJson(jsonCode!);
|
return CountryCode.fromJson(jsonCode!);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static CountryCode? tryFromDialCode(String dialCode) {
|
||||||
|
try {
|
||||||
|
return CountryCode.fromDialCode(dialCode);
|
||||||
|
} catch (e) {
|
||||||
|
if (kDebugMode) print('Failed to recognize country from dialCode: $dialCode');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
CountryCode localize(BuildContext context) {
|
CountryCode localize(BuildContext context) {
|
||||||
return this
|
return this
|
||||||
..name = CountryLocalizations.of(context)?.translate(code) ?? name;
|
..name = CountryLocalizations.of(context)?.translate(code) ?? name;
|
||||||
|
|||||||
Reference in New Issue
Block a user