Skip to content
This repository has been archived by the owner on Jul 20, 2022. It is now read-only.

Applied linting from lint package #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include: package:lint/analysis_options.yaml

linter:
rules:
depend_on_referenced_packages: false
11 changes: 5 additions & 6 deletions lib/country_code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,15 @@ class CountryCode {

CountryCode localize(BuildContext context) {
return this
..name =
CountryLocalizations.of(context)?.translate(this.code) ?? this.name;
..name = CountryLocalizations.of(context)?.translate(code) ?? 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',
name: json['name'] as String,
code: json['code'] as String,
dialCode: json['dial_code'] as String,
flagUri: 'flags/${(json['code'] as String).toLowerCase()}.png',
);
}

Expand Down
106 changes: 53 additions & 53 deletions lib/country_code_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:collection/collection.dart' show IterableExtension;
import 'package:country_code_picker/country_code.dart';
import 'package:country_code_picker/country_codes.dart';
import 'package:country_code_picker/selection_dialog.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:universal_platform/universal_platform.dart';
Expand All @@ -23,7 +22,7 @@ class CountryCodePicker extends StatefulWidget {
final TextStyle? searchStyle;
final TextStyle? dialogTextStyle;
final WidgetBuilder? emptySearchBuilder;
final Function(CountryCode?)? builder;
final Widget Function(CountryCode?)? builder;
final bool enabled;
final TextOverflow textOverflow;
final Icon closeIcon;
Expand Down Expand Up @@ -84,7 +83,7 @@ class CountryCodePicker extends StatefulWidget {
/// with customized codes.
final List<Map<String, String>> countryList;

CountryCodePicker({
const CountryCodePicker({
this.onChanged,
this.onInit,
this.initialSelection,
Expand Down Expand Up @@ -123,7 +122,7 @@ class CountryCodePicker extends StatefulWidget {

@override
State<StatefulWidget> createState() {
List<Map<String, String>> jsonList = countryList;
final List<Map<String, String>> jsonList = countryList;

List<CountryCode> elements =
jsonList.map((json) => CountryCode.fromJson(json)).toList();
Expand All @@ -136,10 +135,12 @@ class CountryCodePicker extends StatefulWidget {
final uppercaseCustomList =
countryFilter!.map((c) => c.toUpperCase()).toList();
elements = elements
.where((c) =>
uppercaseCustomList.contains(c.code) ||
uppercaseCustomList.contains(c.name) ||
uppercaseCustomList.contains(c.dialCode))
.where(
(c) =>
uppercaseCustomList.contains(c.code) ||
uppercaseCustomList.contains(c.name) ||
uppercaseCustomList.contains(c.dialCode),
)
.toList();
}

Expand All @@ -157,12 +158,12 @@ class CountryCodePickerState extends State<CountryCodePicker> {
@override
Widget build(BuildContext context) {
Widget _widget;
if (widget.builder != null)
if (widget.builder != null) {
_widget = InkWell(
onTap: showCountryCodePickerDialog,
child: widget.builder!(selectedItem),
);
else {
} else {
_widget = TextButton(
onPressed: widget.enabled ? showCountryCodePickerDialog : null,
child: Padding(
Expand Down Expand Up @@ -209,28 +210,31 @@ class CountryCodePickerState extends State<CountryCodePicker> {
flex: widget.alignLeft ? 0 : 1,
fit: widget.alignLeft ? FlexFit.tight : FlexFit.loose,
child: Padding(
padding: widget.alignLeft
? const EdgeInsets.only(right: 16.0, left: 8.0)
: const EdgeInsets.only(right: 16.0),
child: Icon(
Icons.arrow_drop_down,
color: Colors.grey,
size: widget.flagWidth,
)),
padding: EdgeInsets.only(
right: 16.0,
left: widget.alignLeft ? 8.0 : 0,
),
child: Icon(
Icons.arrow_drop_down,
color: Colors.grey,
size: widget.flagWidth,
),
),
),
],
),
),
);
}

return _widget;
}

@override
void didChangeDependencies() {
super.didChangeDependencies();

this.elements = elements.map((e) => e.localize(context)).toList();
elements = elements.map((e) => e.localize(context)).toList();
_onInit(selectedItem);
}

Expand All @@ -241,13 +245,13 @@ class CountryCodePickerState extends State<CountryCodePicker> {
if (oldWidget.initialSelection != widget.initialSelection) {
if (widget.initialSelection != null) {
selectedItem = elements.firstWhere(
(e) =>
(e.code!.toUpperCase() ==
widget.initialSelection!.toUpperCase()) ||
(e.dialCode == widget.initialSelection) ||
(e.name!.toUpperCase() ==
widget.initialSelection!.toUpperCase()),
orElse: () => elements[0]);
(e) =>
(e.code!.toUpperCase() ==
widget.initialSelection!.toUpperCase()) ||
(e.dialCode == widget.initialSelection) ||
(e.name!.toUpperCase() == widget.initialSelection!.toUpperCase()),
orElse: () => elements[0],
);
} else {
selectedItem = elements[0];
}
Expand All @@ -261,35 +265,39 @@ class CountryCodePickerState extends State<CountryCodePicker> {

if (widget.initialSelection != null) {
selectedItem = elements.firstWhere(
(e) =>
(e.code!.toUpperCase() ==
widget.initialSelection!.toUpperCase()) ||
(e.dialCode == widget.initialSelection) ||
(e.name!.toUpperCase() == widget.initialSelection!.toUpperCase()),
orElse: () => elements[0]);
(e) =>
(e.code!.toUpperCase() == widget.initialSelection!.toUpperCase()) ||
(e.dialCode == widget.initialSelection) ||
(e.name!.toUpperCase() == widget.initialSelection!.toUpperCase()),
orElse: () => elements[0],
);
} else {
selectedItem = elements[0];
}

favoriteElements = elements
.where((e) =>
widget.favorite.firstWhereOrNull((f) =>
e.code!.toUpperCase() == f.toUpperCase() ||
e.dialCode == f ||
e.name!.toUpperCase() == f.toUpperCase()) !=
null)
.where(
(e) =>
widget.favorite.firstWhereOrNull(
(f) =>
e.code!.toUpperCase() == f.toUpperCase() ||
e.dialCode == f ||
e.name!.toUpperCase() == f.toUpperCase(),
) !=
null,
)
.toList();
}

void showCountryCodePickerDialog() {
if (!UniversalPlatform.isAndroid && !UniversalPlatform.isIOS) {
showDialog(
showDialog<CountryCode?>(
barrierColor: widget.barrierColor ?? Colors.grey.withOpacity(0.5),
// backgroundColor: widget.backgroundColor ?? Colors.transparent,
context: context,
builder: (context) => Center(
child: Container(
constraints: BoxConstraints(maxHeight: 500, maxWidth: 400),
constraints: const BoxConstraints(maxHeight: 500, maxWidth: 400),
child: Dialog(
child: SelectionDialog(
elements,
Expand All @@ -300,9 +308,7 @@ class CountryCodePickerState extends State<CountryCodePicker> {
searchStyle: widget.searchStyle,
textStyle: widget.dialogTextStyle,
boxDecoration: widget.boxDecoration,
showFlag: widget.showFlagDialog != null
? widget.showFlagDialog
: widget.showFlag,
showFlag: widget.showFlagDialog ?? widget.showFlag,
flagWidth: widget.flagWidth,
size: widget.dialogSize,
backgroundColor: widget.dialogBackgroundColor,
Expand All @@ -324,7 +330,7 @@ class CountryCodePickerState extends State<CountryCodePicker> {
}
});
} else {
showMaterialModalBottomSheet(
showMaterialModalBottomSheet<CountryCode?>(
barrierColor: widget.barrierColor ?? Colors.grey.withOpacity(0.5),
backgroundColor: widget.backgroundColor ?? Colors.transparent,
context: context,
Expand All @@ -338,9 +344,7 @@ class CountryCodePickerState extends State<CountryCodePicker> {
searchStyle: widget.searchStyle,
textStyle: widget.dialogTextStyle,
boxDecoration: widget.boxDecoration,
showFlag: widget.showFlagDialog != null
? widget.showFlagDialog
: widget.showFlag,
showFlag: widget.showFlagDialog ?? widget.showFlag,
flagWidth: widget.flagWidth,
flagDecoration: widget.flagDecoration,
size: widget.dialogSize,
Expand All @@ -363,14 +367,10 @@ class CountryCodePickerState extends State<CountryCodePicker> {
}

void _publishSelection(CountryCode e) {
if (widget.onChanged != null) {
widget.onChanged!(e);
}
widget.onChanged?.call(e);
}

void _onInit(CountryCode? e) {
if (widget.onInit != null) {
widget.onInit!(e);
}
widget.onInit?.call(e);
}
}
9 changes: 5 additions & 4 deletions lib/country_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ class CountryLocalizations {
late Map<String, String> _localizedStrings;

Future<bool> load() async {
String jsonString = await rootBundle.loadString(
'packages/country_code_picker/i18n/${locale.languageCode}.json');
Map<String, dynamic> jsonMap = json.decode(jsonString);
final String jsonString = await rootBundle.loadString(
'packages/country_code_picker/i18n/${locale.languageCode}.json',
);
final jsonMap = json.decode(jsonString) as Map<String, dynamic>;

_localizedStrings = jsonMap.map((key, value) {
return MapEntry(key, value.toString());
Expand Down Expand Up @@ -119,7 +120,7 @@ class _CountryLocalizationsDelegate

@override
Future<CountryLocalizations> load(Locale locale) async {
CountryLocalizations localizations = new CountryLocalizations(locale);
final CountryLocalizations localizations = CountryLocalizations(locale);
await localizations.load();
return localizations;
}
Expand Down
Loading