Skip to content

Commit

Permalink
[Murat] Format files as dart format
Browse files Browse the repository at this point in the history
  • Loading branch information
Murat Gun committed Nov 21, 2023
1 parent 025b559 commit 68f5a1b
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 75 deletions.
8 changes: 6 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ class MyApp extends StatelessWidget {
menuType: PickerMenuType.bottomSheet,
initialCountry: Country.afghanistan,
emailValidator: (String? email) {
if (email == null || email.isEmpty) return 'Email is required';
if (email == null || email.isEmpty) {
return 'Email is required';
}
return null;
},
phoneValidator: (String? phone) {
if (phone == null || phone.isEmpty) return 'Phone is required';
if (phone == null || phone.isEmpty) {
return 'Phone is required';
}
return null;
},
pickerHeight: CountryPickerHeigth.h50, // Not Effective in PickerMenuType.page
Expand Down
5 changes: 4 additions & 1 deletion lib/src/components/country_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ class _CountryPickerState extends State<CountryPicker> {
.where((country) =>
country.alpha2.toLowerCase().contains(text.toLowerCase()) ||
country.name.toLowerCase().contains(text.toLowerCase()) ||
country.dialCode.toString().toLowerCase().contains(text.toLowerCase()))
country.dialCode
.toString()
.toLowerCase()
.contains(text.toLowerCase()))
.toList();
});
}
Expand Down
32 changes: 24 additions & 8 deletions lib/src/components/country_picker_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ class CountryPickerButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return InkWell(
onTap: _openCountryPickerMenu(menuType, context, searchInputDecoration, title, titlePadding, pickerHeight,
isSearchable, countries, onValuePicked),
onTap: _openCountryPickerMenu(
menuType,
context,
searchInputDecoration,
title,
titlePadding,
pickerHeight,
isSearchable,
countries,
onValuePicked),
child: Padding(
padding: const EdgeInsets.only(left: 8.0),
child: SizedBox(
Expand All @@ -48,7 +56,8 @@ class CountryPickerButton extends StatelessWidget {
children: <Widget>[
Text(
'+${initialValue.dialCode}',
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
style:
const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
const SizedBox(
width: 4.0,
Expand Down Expand Up @@ -82,14 +91,21 @@ void Function()? _openCountryPickerMenu(
void Function(Country) onValuePicked) {
switch (menuType) {
case PickerMenuType.dialog:
return _openCountryPickerDialog(
context, searchInputDecoration, title, titlePadding, isSearchable, pickerHeight, countries, onValuePicked);
return _openCountryPickerDialog(context, searchInputDecoration, title,
titlePadding, isSearchable, pickerHeight, countries, onValuePicked);
case PickerMenuType.bottomSheet:
return _openCountryPickerBottomSheet(
context, searchInputDecoration, title, titlePadding, isSearchable, pickerHeight, countries, onValuePicked);
context,
searchInputDecoration,
title,
titlePadding,
isSearchable,
pickerHeight,
countries,
onValuePicked);
case PickerMenuType.page:
return _openCountryPickerPage(
context, searchInputDecoration, title, titlePadding, isSearchable, countries, onValuePicked);
return _openCountryPickerPage(context, searchInputDecoration, title,
titlePadding, isSearchable, countries, onValuePicked);
default:
return null;
}
Expand Down
26 changes: 15 additions & 11 deletions lib/src/email_phone_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -194,40 +194,43 @@ class _EphoneFieldState extends State<EPhoneField> {
autovalidateMode: widget.autovalidateMode,
onChanged: (String value) {
if (_type == EphoneFieldType.phone) {
value =
EphoneFieldUtils.combinePrefix(_selectedCountry.dialCode, value, widget.phoneNumberMaskSplitter.value)!;
value = EphoneFieldUtils.combinePrefix(_selectedCountry.dialCode,
value, widget.phoneNumberMaskSplitter.value)!;
}
widget.onChanged?.call(value);
},
onSaved: (String? value) {
if (_type == EphoneFieldType.phone) {
value =
EphoneFieldUtils.combinePrefix(_selectedCountry.dialCode, value, widget.phoneNumberMaskSplitter.value);
value = EphoneFieldUtils.combinePrefix(_selectedCountry.dialCode,
value, widget.phoneNumberMaskSplitter.value);
}
widget.onSaved?.call(value);
},
onFieldSubmitted: (String? value) {
if (_type == EphoneFieldType.phone) {
value =
EphoneFieldUtils.combinePrefix(_selectedCountry.dialCode, value, widget.phoneNumberMaskSplitter.value);
value = EphoneFieldUtils.combinePrefix(_selectedCountry.dialCode,
value, widget.phoneNumberMaskSplitter.value);
}
widget.onFieldSubmitted?.call(value);
},
initialValue: widget.initialValue,
decoration: widget.decoration.copyWith(
prefixIcon: _buildCountryPicker(_type == EphoneFieldType.phone),
labelText: _type.labelText(widget.emptyLabelText, widget.emailLabelText, widget.phoneLabelText)),
labelText: _type.labelText(widget.emptyLabelText,
widget.emailLabelText, widget.phoneLabelText)),
keyboardType: _type.keyboardType,
validator: (String? value) {
if (_type == EphoneFieldType.phone) {
value =
EphoneFieldUtils.combinePrefix(_selectedCountry.dialCode, value, widget.phoneNumberMaskSplitter.value);
value = EphoneFieldUtils.combinePrefix(_selectedCountry.dialCode,
value, widget.phoneNumberMaskSplitter.value);
return widget.phoneValidator?.call(value);
}
return widget.emailValidator?.call(value);
},
inputFormatters: widget.inputFormatters ??
_type.inputFormatters(_selectedCountry, widget.phoneNumberMaskSplitter != MaskSplitCharacter.none,
_type.inputFormatters(
_selectedCountry,
widget.phoneNumberMaskSplitter != MaskSplitCharacter.none,
widget.phoneNumberMaskSplitter.value),
);
}
Expand Down Expand Up @@ -259,7 +262,8 @@ class _EphoneFieldState extends State<EPhoneField> {

/// Updates the [_type] of the input field based on the [_controller] text.
void _updateTextFieldType() {
final text = _controller.text.replaceAll(widget.phoneNumberMaskSplitter.value, '');
final text =
_controller.text.replaceAll(widget.phoneNumberMaskSplitter.value, '');
if (text.isEmpty) {
setState(() {
_type = widget.initialType;
Expand Down
3 changes: 2 additions & 1 deletion lib/src/utils/ephone_field_utils.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class EphoneFieldUtils {
static String? combinePrefix(int prefix, String? phoneNumber, String maskSplitter) {
static String? combinePrefix(
int prefix, String? phoneNumber, String maskSplitter) {
if (phoneNumber == null || phoneNumber.isEmpty) return null;
return '+${prefix.toString() + phoneNumber.replaceAll(maskSplitter, '')}';
}
Expand Down
61 changes: 43 additions & 18 deletions test/component/country_picker_button_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,38 @@ import 'package:flutter_test/flutter_test.dart';
import 'mocks/country_picker_button.dart';

void main() {
CountryPickerButtonMock mockWidget =
CountryPickerButtonMock(menuType: PickerMenuType.bottomSheet, pickerHeight: CountryPickerHeigth.h25);
testWidgets('should CountryPickerButton widget renders successfully', (widgetTester) async {
CountryPickerButtonMock mockWidget = CountryPickerButtonMock(
menuType: PickerMenuType.bottomSheet,
pickerHeight: CountryPickerHeigth.h25);
testWidgets('should CountryPickerButton widget renders successfully',
(widgetTester) async {
await widgetTester.pumpWidget(mockWidget);
expect(find.byType(CountryPickerButton), findsOneWidget);
});

testWidgets('should CountryPickerButton widget renders correct dialCode', (widgetTester) async {
testWidgets('should CountryPickerButton widget renders correct dialCode',
(widgetTester) async {
await widgetTester.pumpWidget(mockWidget);
expect(find.text("+${mockWidget.initialValue.dialCode}"), findsOneWidget);
});

testWidgets('should CountryPickerButton widget renders correct icon', (widgetTester) async {
testWidgets('should CountryPickerButton widget renders correct icon',
(widgetTester) async {
await widgetTester.pumpWidget(mockWidget);

expect(find.byIcon(mockWidget.icon), findsOneWidget);
});

testWidgets('should CountryPickerButton widget renders correct image', (widgetTester) async {
testWidgets('should CountryPickerButton widget renders correct image',
(widgetTester) async {
await widgetTester.pumpWidget(mockWidget);

expect(find.byType(Image), findsOneWidget);
});

testWidgets('should CountryPickerButton widget renders CountryPickerMenu on tap', (widgetTester) async {
testWidgets(
'should CountryPickerButton widget renders CountryPickerMenu on tap',
(widgetTester) async {
await widgetTester.pumpWidget(mockWidget);

await widgetTester.tap(find.byType(CountryPickerButton));
Expand All @@ -39,63 +46,81 @@ void main() {
expect(find.byType(CountryPickerMenu), findsOneWidget);
});

testWidgets('should country picker size as expected if pickerHeight CountryPickerHeigth.h25', (widgetTester) async {
testWidgets(
'should country picker size as expected if pickerHeight CountryPickerHeigth.h25',
(widgetTester) async {
await widgetTester.pumpWidget(mockWidget);

await widgetTester.tap(find.byType(CountryPickerButton));
await widgetTester.pumpAndSettle();

expect(find.byType(CountryPickerMenu), findsOneWidget);

final CountryPickerMenu menu = widgetTester.firstWidget(find.byType(CountryPickerMenu));
final CountryPickerMenu menu =
widgetTester.firstWidget(find.byType(CountryPickerMenu));
expect(
menu.height,
MediaQuery.of(mockWidget.ctx!).size.height * .25,
);
});

testWidgets('should country picker size as expected if pickerHeight CountryPickerHeigth.h50', (widgetTester) async {
mockWidget = CountryPickerButtonMock(menuType: PickerMenuType.bottomSheet, pickerHeight: CountryPickerHeigth.h50);
testWidgets(
'should country picker size as expected if pickerHeight CountryPickerHeigth.h50',
(widgetTester) async {
mockWidget = CountryPickerButtonMock(
menuType: PickerMenuType.bottomSheet,
pickerHeight: CountryPickerHeigth.h50);
await widgetTester.pumpWidget(mockWidget);

await widgetTester.tap(find.byType(CountryPickerButton));
await widgetTester.pumpAndSettle();

expect(find.byType(CountryPickerMenu), findsOneWidget);

final CountryPickerMenu menu = widgetTester.firstWidget(find.byType(CountryPickerMenu));
final CountryPickerMenu menu =
widgetTester.firstWidget(find.byType(CountryPickerMenu));
expect(
menu.height,
MediaQuery.of(mockWidget.ctx!).size.height * .50,
);
});

testWidgets('should country picker size as expected if pickerHeight CountryPickerHeigth.h75', (widgetTester) async {
mockWidget = CountryPickerButtonMock(menuType: PickerMenuType.bottomSheet, pickerHeight: CountryPickerHeigth.h75);
testWidgets(
'should country picker size as expected if pickerHeight CountryPickerHeigth.h75',
(widgetTester) async {
mockWidget = CountryPickerButtonMock(
menuType: PickerMenuType.bottomSheet,
pickerHeight: CountryPickerHeigth.h75);
await widgetTester.pumpWidget(mockWidget);

await widgetTester.tap(find.byType(CountryPickerButton));
await widgetTester.pumpAndSettle();

expect(find.byType(CountryPickerMenu), findsOneWidget);

final CountryPickerMenu menu = widgetTester.firstWidget(find.byType(CountryPickerMenu));
final CountryPickerMenu menu =
widgetTester.firstWidget(find.byType(CountryPickerMenu));
expect(
menu.height,
MediaQuery.of(mockWidget.ctx!).size.height * .75,
);
});

testWidgets('should country picker size as expected if pickerHeight CountryPickerHeigth.h100', (widgetTester) async {
mockWidget = CountryPickerButtonMock(menuType: PickerMenuType.bottomSheet, pickerHeight: CountryPickerHeigth.h100);
testWidgets(
'should country picker size as expected if pickerHeight CountryPickerHeigth.h100',
(widgetTester) async {
mockWidget = CountryPickerButtonMock(
menuType: PickerMenuType.bottomSheet,
pickerHeight: CountryPickerHeigth.h100);
await widgetTester.pumpWidget(mockWidget);

await widgetTester.tap(find.byType(CountryPickerButton));
await widgetTester.pumpAndSettle();

expect(find.byType(CountryPickerMenu), findsOneWidget);

final CountryPickerMenu menu = widgetTester.firstWidget(find.byType(CountryPickerMenu));
final CountryPickerMenu menu =
widgetTester.firstWidget(find.byType(CountryPickerMenu));
expect(
menu.height,
MediaQuery.of(mockWidget.ctx!).size.height,
Expand Down
33 changes: 24 additions & 9 deletions test/component/country_picker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,53 @@ import 'mocks/country_picker.dart';
import 'utils/caller_checker.dart';

void main() {
final CountryPickerMock mockWidget = CountryPickerMock(onValuePicked: EphoneFieldCallerChecker.mockOnValuePicked);
final CountryPickerMock mockWidget = CountryPickerMock(
onValuePicked: EphoneFieldCallerChecker.mockOnValuePicked);

setUp(() => EphoneFieldCallerChecker.reset());

testWidgets('should CountryPickerMenu widget renders successfully', (widgetTester) async {
testWidgets('should CountryPickerMenu widget renders successfully',
(widgetTester) async {
await widgetTester.pumpWidget(mockWidget);
expect(find.byType(CountryPicker), findsOneWidget);
});

testWidgets('should CountryPickerMenu widget renders successfully with search input', (widgetTester) async {
testWidgets(
'should CountryPickerMenu widget renders successfully with search input',
(widgetTester) async {
await widgetTester.pumpWidget(mockWidget);
expect(find.byKey(const Key("search-field")), findsOneWidget);
});

testWidgets('should CountryPickerMenu widget renders successfully with country picker list', (widgetTester) async {
testWidgets(
'should CountryPickerMenu widget renders successfully with country picker list',
(widgetTester) async {
await widgetTester.pumpWidget(mockWidget);
expect(find.byKey(const Key("country-picker-list")), findsOneWidget);
});

testWidgets('should CountryPickerMenu widget renders successfully with country card widget', (widgetTester) async {
testWidgets(
'should CountryPickerMenu widget renders successfully with country card widget',
(widgetTester) async {
await widgetTester.pumpWidget(mockWidget);
expect(find.byType(CountryCard), findsWidgets);
});

testWidgets('should CountryPickerMenu widget renders searched countries by name', (widgetTester) async {
testWidgets(
'should CountryPickerMenu widget renders searched countries by name',
(widgetTester) async {
await widgetTester.pumpWidget(mockWidget);
await widgetTester.enterText(find.byKey(const Key("search-field")), "United States");
await widgetTester.enterText(
find.byKey(const Key("search-field")), "United States");
await widgetTester.pump();
expect(find.byKey(const Key("country-picker-list")), findsOneWidget);
expect(find.text("United States"), findsWidgets);
expect(find.byType(CountryCard), findsWidgets);
});

testWidgets('should CountryPickerMenu widget renders searched countries by code', (widgetTester) async {
testWidgets(
'should CountryPickerMenu widget renders searched countries by code',
(widgetTester) async {
await widgetTester.pumpWidget(mockWidget);
await widgetTester.enterText(find.byKey(const Key("search-field")), "90");
await widgetTester.pump();
Expand All @@ -48,7 +61,9 @@ void main() {
expect(find.byType(CountryCard), findsWidgets);
});

testWidgets('should CountryPickerMenu onValuePicked triggered after selected country', (widgetTester) async {
testWidgets(
'should CountryPickerMenu onValuePicked triggered after selected country',
(widgetTester) async {
await widgetTester.pumpWidget(mockWidget);
await widgetTester.enterText(find.byKey(const Key("search-field")), "90");
await widgetTester.pump();
Expand Down
Loading

0 comments on commit 68f5a1b

Please sign in to comment.