Merge pull request #35 from Iconica-Development/bugfix/phone_field

Bugfix/phone field
This commit is contained in:
Jacques Doeleman 2024-02-21 15:40:18 +01:00 committed by GitHub
commit fddf100fb0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 65 additions and 48 deletions

View file

@ -72,3 +72,7 @@
## 3.2.0 ## 3.2.0
* Added `FlutterFormInputPhone` * Added `FlutterFormInputPhone`
## 3.2.1
* Added `PhoneNumber` model to save the `FlutterFormInputPhone` result.
* Added more customization for `FlutterFormInputPhone`.

View file

@ -68,7 +68,7 @@ packages:
path: ".." path: ".."
relative: true relative: true
source: path source: path
version: "3.1.0" version: "3.2.0"
flutter_lints: flutter_lints:
dependency: "direct dev" dependency: "direct dev"
description: description:

View file

@ -85,7 +85,6 @@ class _BoolWidgetState extends State<BoolWidget> {
focusNode: widget.focusNode, focusNode: widget.focusNode,
onChanged: onChanged, onChanged: onChanged,
); );
break;
case BoolWidgetType.checkbox: case BoolWidgetType.checkbox:
child = Checkbox( child = Checkbox(
@ -97,7 +96,6 @@ class _BoolWidgetState extends State<BoolWidget> {
} }
}, },
); );
break;
} }
return Column( return Column(

View file

@ -107,7 +107,7 @@ class _DateInputFieldState extends State<DateTimeInputField> {
userInput = unformatted != null userInput = unformatted != null
? widget.dateFormat.format(unformatted) ? widget.dateFormat.format(unformatted)
: userInput; : userInput;
break;
case FlutterFormDateTimeType.dateTime: case FlutterFormDateTimeType.dateTime:
await getInputFromUser(FlutterFormDateTimeType.date) await getInputFromUser(FlutterFormDateTimeType.date)
.then((value) async { .then((value) async {
@ -132,7 +132,7 @@ class _DateInputFieldState extends State<DateTimeInputField> {
} }
} }
}); });
break;
case FlutterFormDateTimeType.range: case FlutterFormDateTimeType.range:
if (context.mounted) { if (context.mounted) {
userInput = await showDateRangePicker( userInput = await showDateRangePicker(
@ -147,7 +147,7 @@ class _DateInputFieldState extends State<DateTimeInputField> {
: '', : '',
); );
} }
break;
case FlutterFormDateTimeType.time: case FlutterFormDateTimeType.time:
if (context.mounted) { if (context.mounted) {
userInput = await showTimePicker( userInput = await showTimePicker(

View file

@ -4,6 +4,7 @@ export 'date_picker/date_picker.dart';
export 'number_picker/number_picker.dart'; export 'number_picker/number_picker.dart';
export 'phone/countries.dart'; export 'phone/countries.dart';
export 'phone/phone.dart'; export 'phone/phone.dart';
export 'phone/phone_number_model.dart';
export 'scroll_picker/scroll_picker.dart'; export 'scroll_picker/scroll_picker.dart';
export 'slider/slider.dart'; export 'slider/slider.dart';
export 'text/password.dart'; export 'text/password.dart';

View file

@ -17,25 +17,27 @@ class FlutterFormInputPhone extends StatefulWidget {
this.onSaved, this.onSaved,
this.validator, this.validator,
this.onFieldSubmitted, this.onFieldSubmitted,
this.style, this.numberFieldStyle,
this.dialCodeSelectorStyle,
this.enabled = true, this.enabled = true,
this.priorityCountries = const ['NL', 'BE', 'LU'], this.priorityCountries = const ['NL', 'BE', 'LU'],
this.countrySelectorUnderline, this.textAlignVertical = TextAlignVertical.top,
this.countrySelectorWidth = 100, this.dialCodeSelectorPadding = const EdgeInsets.only(top: 6),
}); });
final InputDecoration? decoration; final InputDecoration? decoration;
final Widget? label; final Widget? label;
final String? initialValue; final PhoneNumber? initialValue;
final Function(String?)? onSaved; final Function(PhoneNumber?)? onSaved;
final String? Function(String?)? validator; final String? Function(PhoneNumber?)? validator;
final Function(String?)? onChanged; final Function(PhoneNumber?)? onChanged;
final Function(String?)? onFieldSubmitted; final Function(PhoneNumber?)? onFieldSubmitted;
final TextStyle? style; final TextStyle? numberFieldStyle;
final TextStyle? dialCodeSelectorStyle;
final bool enabled; final bool enabled;
final List<String>? priorityCountries; final List<String>? priorityCountries;
final Widget? countrySelectorUnderline; final EdgeInsets dialCodeSelectorPadding;
final double countrySelectorWidth; final TextAlignVertical textAlignVertical;
@override @override
State<FlutterFormInputPhone> createState() => _FlutterFormInputPhoneState(); State<FlutterFormInputPhone> createState() => _FlutterFormInputPhoneState();
@ -70,7 +72,10 @@ class _FlutterFormInputPhoneState extends State<FlutterFormInputPhone> {
} }
} }
_selectedCountry = _countryList.first; _selectedCountry = _countryList.firstWhereOrNull(
(country) => widget.initialValue?.dialCode == country.dialCode,
) ??
_countryList.first;
} }
@override @override
@ -80,15 +85,32 @@ class _FlutterFormInputPhoneState extends State<FlutterFormInputPhone> {
label: widget.label ?? const Text('Phone number'), label: widget.label ?? const Text('Phone number'),
); );
return Row( return FlutterFormInputPlainText(
children: [ label: widget.label,
Container( style: widget.numberFieldStyle,
padding: const EdgeInsets.only(top: 16), initialValue: widget.initialValue?.number,
width: widget.countrySelectorWidth, onSaved: (value) => widget.onSaved?.call(
PhoneNumber(dialCode: _selectedCountry.dialCode, number: value),
),
validator: (value) => widget.validator?.call(
PhoneNumber(dialCode: _selectedCountry.dialCode, number: value),
),
onChanged: (value) => widget.onChanged?.call(
PhoneNumber(dialCode: _selectedCountry.dialCode, number: value),
),
onFieldSubmitted: (value) => widget.onFieldSubmitted?.call(
PhoneNumber(dialCode: _selectedCountry.dialCode, number: value),
),
decoration: inputDecoration.copyWith(
contentPadding: EdgeInsets.zero,
prefixIcon: Padding(
padding: widget.dialCodeSelectorPadding,
child: DropdownButton( child: DropdownButton(
padding: EdgeInsets.zero,
elevation: 0,
value: _selectedCountry, value: _selectedCountry,
style: widget.style, style: widget.dialCodeSelectorStyle,
underline: widget.countrySelectorUnderline, underline: const SizedBox.shrink(),
items: [ items: [
for (var country in _countryList) ...[ for (var country in _countryList) ...[
DropdownMenuItem( DropdownMenuItem(
@ -115,27 +137,10 @@ class _FlutterFormInputPhoneState extends State<FlutterFormInputPhone> {
}, },
), ),
), ),
const SizedBox( ),
width: 16, keyboardType: TextInputType.phone,
), enabled: widget.enabled,
Expanded( textAlignVertical: widget.textAlignVertical,
child: FlutterFormInputPlainText(
style: widget.style,
initialValue: widget.initialValue,
onSaved: (value) =>
widget.onSaved?.call('${_selectedCountry.dialCode}$value'),
validator: (value) =>
widget.validator?.call('${_selectedCountry.dialCode}$value'),
onChanged: (value) =>
widget.onChanged?.call('${_selectedCountry.dialCode}$value'),
onFieldSubmitted: (value) => widget.onFieldSubmitted
?.call('${_selectedCountry.dialCode}$value'),
decoration: inputDecoration.copyWith(),
keyboardType: TextInputType.phone,
enabled: widget.enabled,
),
),
],
); );
} }
} }

View file

@ -0,0 +1,9 @@
class PhoneNumber {
const PhoneNumber({
this.dialCode,
this.number,
});
final String? dialCode;
final String? number;
}

View file

@ -1,10 +1,10 @@
name: flutter_input_library name: flutter_input_library
description: A new Flutter package project. description: A new Flutter package project.
version: 3.2.0 version: 3.2.1
repository: https://github.com/Iconica-Development/flutter_input_library repository: https://github.com/Iconica-Development/flutter_input_library
environment: environment:
sdk: ">=2.18.2 <3.0.0" sdk: ^3.0.0
flutter: ">=1.17.0" flutter: ">=1.17.0"
dependencies: dependencies: