From 6b4201604183b0cffb7d141757189d94da306b0c Mon Sep 17 00:00:00 2001 From: dongguangxu Date: Tue, 13 Jul 2021 21:05:01 +0800 Subject: [PATCH 1/3] MOD support version=<2.0 --- CHANGELOG.md | 5 +++ lib/country_code.dart | 29 +++++++----- lib/country_code_picker.dart | 81 +++++++++++++++++----------------- lib/country_localizations.dart | 8 ++-- lib/selection_dialog.dart | 42 +++++++++--------- pubspec.yaml | 10 ++--- 6 files changed, 94 insertions(+), 81 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a0299a5..bc8d0ec8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,11 @@ - switch to dialog on desktop app support - added flagDecoration + +## 1.7.5 + +- support version<=2.0 + ## 1.7.0 - Update modal_bottom_sheet to 1.0.0+1 diff --git a/lib/country_code.dart b/lib/country_code.dart index 3d1a6b47..e8af5d11 100644 --- a/lib/country_code.dart +++ b/lib/country_code.dart @@ -1,4 +1,3 @@ -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'; @@ -11,16 +10,16 @@ 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; + String name; /// the flag of the country - final String? flagUri; + final String flagUri; /// the country code (IT,AF..) - final String? code; + final String code; /// the dial code (+39,+93..) - final String? dialCode; + final String dialCode; CountryCode({ this.name, @@ -35,17 +34,27 @@ class CountryCode { } factory CountryCode.fromCountryCode(String countryCode) { - final Map? jsonCode = codes.firstWhereOrNull( + final Map jsonCode = codes.firstWhere( (code) => code['code'] == countryCode, + orElse: () => null, ); - return CountryCode.fromJson(jsonCode!); + + if (jsonCode == null) { + return null; + } + return CountryCode.fromJson(jsonCode); } factory CountryCode.fromDialCode(String dialCode) { - final Map? jsonCode = codes.firstWhereOrNull( + final Map jsonCode = codes.firstWhere( (code) => code['dial_code'] == dialCode, + orElse: () => null, ); - return CountryCode.fromJson(jsonCode!); + + if (jsonCode == null) { + return null; + } + return CountryCode.fromJson(jsonCode); } CountryCode localize(BuildContext context) { @@ -72,7 +81,7 @@ class CountryCode { return '$_cleanName'; } - String? get _cleanName { + String get _cleanName { return name?.replaceAll(RegExp(r'[[\]]'), '').split(',').first; } } diff --git a/lib/country_code_picker.dart b/lib/country_code_picker.dart index c979e12b..4aef1525 100644 --- a/lib/country_code_picker.dart +++ b/lib/country_code_picker.dart @@ -1,6 +1,5 @@ library country_code_picker; -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'; @@ -12,39 +11,39 @@ import 'package:universal_platform/universal_platform.dart'; export 'country_code.dart'; class CountryCodePicker extends StatefulWidget { - final ValueChanged? onChanged; - final ValueChanged? onInit; - final String? initialSelection; + final ValueChanged onChanged; + final ValueChanged onInit; + final String initialSelection; final List favorite; - final TextStyle? textStyle; + final TextStyle textStyle; final EdgeInsetsGeometry padding; final bool showCountryOnly; final InputDecoration searchDecoration; - final TextStyle? searchStyle; - final TextStyle? dialogTextStyle; - final WidgetBuilder? emptySearchBuilder; - final Function(CountryCode?)? builder; + final TextStyle searchStyle; + final TextStyle dialogTextStyle; + final WidgetBuilder emptySearchBuilder; + final Function(CountryCode) builder; final bool enabled; final TextOverflow textOverflow; final Icon closeIcon; /// Barrier color of ModalBottomSheet - final Color? barrierColor; + final Color barrierColor; /// Background color of ModalBottomSheet - final Color? backgroundColor; + final Color backgroundColor; /// BoxDecoration for dialog - final BoxDecoration? boxDecoration; + final BoxDecoration boxDecoration; /// the size of the selection dialog - final Size? dialogSize; + final Size dialogSize; /// Background color of selection dialog - final Color? dialogBackgroundColor; + final Color dialogBackgroundColor; /// used to customize the country list - final List? countryFilter; + final List countryFilter; /// shows the name of the country instead of the dialcode final bool showOnlyCountryWhenClosed; @@ -61,15 +60,15 @@ class CountryCodePicker extends StatefulWidget { final bool hideMainText; - final bool? showFlagMain; + final bool showFlagMain; - final bool? showFlagDialog; + final bool showFlagDialog; /// Width of the flag images final double flagWidth; /// Use this property to change the order of the options - final Comparator? comparator; + final Comparator comparator; /// Set to true if you want to hide the search part final bool hideSearch; @@ -78,7 +77,7 @@ class CountryCodePicker extends StatefulWidget { final bool showDropDownButton; /// [BoxDecoration] for the flag image - final Decoration? flagDecoration; + final Decoration flagDecoration; /// An optional argument for injecting a list of countries /// with customized codes. @@ -118,7 +117,7 @@ class CountryCodePicker extends StatefulWidget { this.dialogBackgroundColor, this.closeIcon = const Icon(Icons.close), this.countryList = codes, - Key? key, + Key key, }) : super(key: key); @override @@ -132,9 +131,9 @@ class CountryCodePicker extends StatefulWidget { elements.sort(comparator); } - if (countryFilter != null && countryFilter!.isNotEmpty) { + if (countryFilter != null && countryFilter.isNotEmpty) { final uppercaseCustomList = - countryFilter!.map((c) => c.toUpperCase()).toList(); + countryFilter.map((c) => c.toUpperCase()).toList(); elements = elements .where((c) => uppercaseCustomList.contains(c.code) || @@ -148,7 +147,7 @@ class CountryCodePicker extends StatefulWidget { } class CountryCodePickerState extends State { - CountryCode? selectedItem; + CountryCode selectedItem; List elements = []; List favoriteElements = []; @@ -160,7 +159,7 @@ class CountryCodePickerState extends State { if (widget.builder != null) _widget = InkWell( onTap: showCountryCodePickerDialog, - child: widget.builder!(selectedItem), + child: widget.builder(selectedItem), ); else { _widget = TextButton( @@ -172,7 +171,7 @@ class CountryCodePickerState extends State { mainAxisSize: MainAxisSize.min, children: [ if (widget.showFlagMain != null - ? widget.showFlagMain! + ? widget.showFlagMain : widget.showFlag) Flexible( flex: widget.alignLeft ? 0 : 1, @@ -186,7 +185,7 @@ class CountryCodePickerState extends State { ? const EdgeInsets.only(right: 16.0, left: 8.0) : const EdgeInsets.only(right: 16.0), child: Image.asset( - selectedItem!.flagUri!, + selectedItem.flagUri, package: 'country_code_picker', width: widget.flagWidth, ), @@ -197,7 +196,7 @@ class CountryCodePickerState extends State { fit: widget.alignLeft ? FlexFit.tight : FlexFit.loose, child: Text( widget.showOnlyCountryWhenClosed - ? selectedItem!.toCountryStringOnly() + ? selectedItem.toCountryStringOnly() : selectedItem.toString(), style: widget.textStyle ?? Theme.of(context).textTheme.button, @@ -242,11 +241,10 @@ class CountryCodePickerState extends State { if (widget.initialSelection != null) { selectedItem = elements.firstWhere( (e) => - (e.code!.toUpperCase() == - widget.initialSelection!.toUpperCase()) || + (e.code.toUpperCase() == + widget.initialSelection.toUpperCase()) || (e.dialCode == widget.initialSelection) || - (e.name!.toUpperCase() == - widget.initialSelection!.toUpperCase()), + (e.name.toUpperCase() == widget.initialSelection.toUpperCase()), orElse: () => elements[0]); } else { selectedItem = elements[0]; @@ -262,10 +260,9 @@ class CountryCodePickerState extends State { if (widget.initialSelection != null) { selectedItem = elements.firstWhere( (e) => - (e.code!.toUpperCase() == - widget.initialSelection!.toUpperCase()) || + (e.code.toUpperCase() == widget.initialSelection.toUpperCase()) || (e.dialCode == widget.initialSelection) || - (e.name!.toUpperCase() == widget.initialSelection!.toUpperCase()), + (e.name.toUpperCase() == widget.initialSelection.toUpperCase()), orElse: () => elements[0]); } else { selectedItem = elements[0]; @@ -273,10 +270,12 @@ class CountryCodePickerState extends State { favoriteElements = elements .where((e) => - widget.favorite.firstWhereOrNull((f) => - e.code!.toUpperCase() == f.toUpperCase() || - e.dialCode == f || - e.name!.toUpperCase() == f.toUpperCase()) != + widget.favorite.firstWhere( + (f) => + e.code.toUpperCase() == f.toUpperCase() || + e.dialCode == f || + e.name.toUpperCase() == f.toUpperCase(), + orElse: () => null) != null) .toList(); } @@ -364,13 +363,13 @@ class CountryCodePickerState extends State { void _publishSelection(CountryCode e) { if (widget.onChanged != null) { - widget.onChanged!(e); + widget.onChanged(e); } } - void _onInit(CountryCode? e) { + void _onInit(CountryCode e) { if (widget.onInit != null) { - widget.onInit!(e); + widget.onInit(e); } } } diff --git a/lib/country_localizations.dart b/lib/country_localizations.dart index 3020f78e..49bbaa1b 100644 --- a/lib/country_localizations.dart +++ b/lib/country_localizations.dart @@ -8,7 +8,7 @@ class CountryLocalizations { CountryLocalizations(this.locale); - static CountryLocalizations? of(BuildContext context) { + static CountryLocalizations of(BuildContext context) { return Localizations.of( context, CountryLocalizations, @@ -18,7 +18,7 @@ class CountryLocalizations { static const LocalizationsDelegate delegate = _CountryLocalizationsDelegate(); - late Map _localizedStrings; + Map _localizedStrings; Future load() async { String jsonString = await rootBundle.loadString( @@ -32,8 +32,8 @@ class CountryLocalizations { return true; } - String? translate(String? key) { - return _localizedStrings[key!]; + String translate(String key) { + return _localizedStrings[key]; } } diff --git a/lib/selection_dialog.dart b/lib/selection_dialog.dart index 176d0ee2..99692b61 100644 --- a/lib/selection_dialog.dart +++ b/lib/selection_dialog.dart @@ -5,24 +5,24 @@ import 'package:flutter/material.dart'; /// selection dialog used for selection of the country code class SelectionDialog extends StatefulWidget { final List elements; - final bool? showCountryOnly; + final bool showCountryOnly; final InputDecoration searchDecoration; - final TextStyle? searchStyle; - final TextStyle? textStyle; - final BoxDecoration? boxDecoration; - final WidgetBuilder? emptySearchBuilder; - final bool? showFlag; + final TextStyle searchStyle; + final TextStyle textStyle; + final BoxDecoration boxDecoration; + final WidgetBuilder emptySearchBuilder; + final bool showFlag; final double flagWidth; - final Decoration? flagDecoration; - final Size? size; + final Decoration flagDecoration; + final Size size; final bool hideSearch; - final Icon? closeIcon; + final Icon closeIcon; /// Background color of SelectionDialog - final Color? backgroundColor; + final Color backgroundColor; /// Boxshaow color of SelectionDialog that matches CountryCodePicker barrier color - final Color? barrierColor; + final Color barrierColor; /// elements passed as favorite final List favoriteElements; @@ -30,7 +30,7 @@ class SelectionDialog extends StatefulWidget { SelectionDialog( this.elements, this.favoriteElements, { - Key? key, + Key key, this.showCountryOnly, this.emptySearchBuilder, InputDecoration searchDecoration = const InputDecoration(), @@ -56,7 +56,7 @@ class SelectionDialog extends StatefulWidget { class _SelectionDialogState extends State { /// this is useful for filtering purpose - late List filteredElements; + List filteredElements; @override Widget build(BuildContext context) => Padding( @@ -86,7 +86,7 @@ class _SelectionDialogState extends State { IconButton( padding: const EdgeInsets.all(0), iconSize: 20, - icon: widget.closeIcon!, + icon: widget.closeIcon, onPressed: () => Navigator.pop(context), ), if (!widget.hideSearch) @@ -142,7 +142,7 @@ class _SelectionDialogState extends State { child: Flex( direction: Axis.horizontal, children: [ - if (widget.showFlag!) + if (widget.showFlag) Flexible( child: Container( margin: const EdgeInsets.only(right: 16.0), @@ -150,7 +150,7 @@ class _SelectionDialogState extends State { clipBehavior: widget.flagDecoration == null ? Clip.none : Clip.hardEdge, child: Image.asset( - e.flagUri!, + e.flagUri, package: 'country_code_picker', width: widget.flagWidth, ), @@ -159,7 +159,7 @@ class _SelectionDialogState extends State { Expanded( flex: 4, child: Text( - widget.showCountryOnly! + widget.showCountryOnly ? e.toCountryStringOnly() : e.toLongString(), overflow: TextOverflow.fade, @@ -173,7 +173,7 @@ class _SelectionDialogState extends State { Widget _buildEmptySearchWidget(BuildContext context) { if (widget.emptySearchBuilder != null) { - return widget.emptySearchBuilder!(context); + return widget.emptySearchBuilder(context); } return Center( @@ -193,9 +193,9 @@ class _SelectionDialogState extends State { setState(() { filteredElements = widget.elements .where((e) => - e.code!.contains(s) || - e.dialCode!.contains(s) || - e.name!.toUpperCase().contains(s)) + e.code.contains(s) || + e.dialCode.contains(s) || + e.name.toUpperCase().contains(s)) .toList(); }); } diff --git a/pubspec.yaml b/pubspec.yaml index b0c5ab80..d2921d37 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,18 +1,18 @@ name: country_code_picker description: A flutter package for showing a country code selector. In addition it gives the possibility to select a list of favorites countries, as well as to search using a simple searchbox -version: 2.0.2 +version: 1.7.5 homepage: https://github.com/imtoori/CountryCodePicker environment: - sdk: '>=2.12.0 <3.0.0' + sdk: ">=2.7.0 <3.0.0" dependencies: flutter: sdk: flutter - modal_bottom_sheet: ^2.0.0 - collection: ^1.15.0 - universal_platform: ^1.0.0+1 + modal_bottom_sheet: ^1.0.0+1 + collection: ^1.14.13 + universal_platform: ^0.1.3 flutter: assets: From 3cbca713284421e8fd1bd2700346811bd598488a Mon Sep 17 00:00:00 2001 From: dongguangxu Date: Tue, 13 Jul 2021 21:08:08 +0800 Subject: [PATCH 2/3] MOD CHANGELOG --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc8d0ec8..293d85ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,8 @@ ## 1.7.5 -- support version<=2.0 +- support version<=2.0 +- include 2.0.2 modify content ## 1.7.0 From 737aa5c57899025a84073421ee827b61fea284ed Mon Sep 17 00:00:00 2001 From: dongguangxu Date: Tue, 13 Jul 2021 21:13:45 +0800 Subject: [PATCH 3/3] MOD CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 293d85ea..234a4b7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ ## 1.7.5 - support version<=2.0 -- include 2.0.2 modify content +- include latest 2.0.2 modify content ## 1.7.0