Skip to content

Commit

Permalink
Fix merge conflicts PR #270
Browse files Browse the repository at this point in the history
  • Loading branch information
danvick committed May 10, 2020
2 parents 995a2a7 + 58d7545 commit 6585986
Show file tree
Hide file tree
Showing 6 changed files with 529 additions and 17 deletions.
35 changes: 30 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class MyHomePageState extends State<MyHomePage> {
bool readOnly = false;
bool showSegmentedControl = true;
final GlobalKey<FormBuilderState> _fbKey = GlobalKey<FormBuilderState>();
final GlobalKey<FormFieldState> _specifyTextFieldKey =
GlobalKey<FormFieldState>();
final GlobalKey<FormFieldState> _specifyTextFieldKey = GlobalKey<FormFieldState>();

ValueChanged _onChanged = (val) => print(val);
var genderOptions = ['Male', 'Female', 'Other'];
Expand Down Expand Up @@ -486,8 +485,8 @@ class MyHomePageState extends State<MyHomePage> {
"English",
"Spanish",
"Somali",
"Other"
];
"Other"];

return InputDecorator(
decoration: InputDecoration(
labelText: "What's your preferred language?"),
Expand Down Expand Up @@ -544,7 +543,33 @@ class MyHomePageState extends State<MyHomePage> {
return "Two or more images required.";
}
return null;
}
}],
),
FormBuilderCountryPicker(
defaultSelectedCountryIsoCode: 'US',
attribute: "country",
cursorColor: Colors.black,
style: TextStyle(color: Colors.black, fontSize: 18),
priorityListByIsoCode: ['US'],
valueTransformer: (value) {
return value.isoCode;
},
decoration: InputDecoration(border: OutlineInputBorder(), labelText: "Country"),
validators: [
FormBuilderValidators.required(errorText: 'This field required.'),
],
),
FormBuilderPhoneField(
attribute: 'phone_number',
keyboardType: TextInputType.phone,
defaultSelectedCountryIsoCode: 'US',
cursorColor: Colors.black,
style: TextStyle(color: Colors.black, fontSize: 18),
decoration: InputDecoration(border: OutlineInputBorder(), labelText: "Phone Number"),
priorityListByIsoCode: ['US'],
validators: [
FormBuilderValidators.numeric(errorText: 'Invalid phone number'),
FormBuilderValidators.required(errorText: 'This field reqired')
],
),
FormBuilderSignaturePad(
Expand Down
18 changes: 10 additions & 8 deletions lib/flutter_form_builder.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
library flutter_form_builder;

export './src/form_builder.dart';
export './src/form_builder_custom_field.dart';
export './src/form_builder_field_option.dart';
export './src/form_builder_validators.dart';
export 'package:flutter_typeahead/flutter_typeahead.dart';

export './src/fields/form_builder_checkbox.dart';
export './src/fields/form_builder_checkbox_list.dart';
export './src/fields/form_builder_chips_choice.dart';
Expand All @@ -13,17 +11,21 @@ export './src/fields/form_builder_color_picker.dart';
export './src/fields/form_builder_date_range_picker.dart';
export './src/fields/form_builder_date_time_picker.dart';
export './src/fields/form_builder_dropdown.dart';
export './src/fields/form_builder_image_picker.dart';
export './src/fields/form_builder_image_picker.dart';
export './src/fields/form_builder_phone_field.dart';
export './src/fields/form_builder_radio.dart';
export './src/fields/form_builder_range_slider.dart';
export './src/fields/form_builder_rate.dart';
export './src/fields/form_builder_segmented_control.dart';
export './src/fields/form_builder_signature_pad.dart';
export './src/fields/form_builder_slider.dart';
export './src/fields/form_builder_stepper.dart';
export './src/fields/form_builder_switch.dart';
export './src/fields/form_builder_text_field.dart';
export './src/fields/form_builder_touch_spin.dart';
export './src/fields/form_builder_typeahead.dart';
export './src/fields/form_builder_signature_pad.dart';
export './src/fields/form_builder_image_picker.dart';

export 'package:flutter_typeahead/flutter_typeahead.dart';
export './src/form_builder.dart';
export './src/form_builder_custom_field.dart';
export './src/form_builder_field_option.dart';
export './src/form_builder_validators.dart';
190 changes: 190 additions & 0 deletions lib/src/fields/form_builder_country_picker.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
import 'package:country_pickers/country.dart';
import 'package:country_pickers/country_pickers.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';

class FormBuilderCountryPicker extends StatefulWidget {
final String attribute;
final List<FormFieldValidator> validators;
final bool readOnly;
final InputDecoration decoration;
final ValueChanged onChanged;
final ValueTransformer valueTransformer;

final TextStyle style;
final FormFieldSetter onSaved;

// For country dialog
final String searchText;
final EdgeInsets titlePadding;
final bool isSearchable;
final Text dialogTitle;
final String defaultSelectedCountryIsoCode;
final List<String> priorityListByIsoCode;
final List<String> countryFilterByIsoCode;
final TextStyle dialogTextStyle;
final bool isCupertinoPicker;
final double cupertinoPickerSheetHeight;
final Color cursorColor;

FormBuilderCountryPicker(
{Key key,
@required this.attribute,
this.validators = const [],
this.readOnly = false,
this.decoration = const InputDecoration(),
this.style,
this.onChanged,
this.valueTransformer,
this.onSaved,
this.searchText,
this.titlePadding,
this.dialogTitle,
this.isSearchable,
@required this.defaultSelectedCountryIsoCode,
this.priorityListByIsoCode,
this.countryFilterByIsoCode,
this.dialogTextStyle,
this.isCupertinoPicker,
this.cupertinoPickerSheetHeight,
this.cursorColor})
: assert(defaultSelectedCountryIsoCode != null),
super(key: key);

@override
_FormBuilderCountryPickerState createState() => _FormBuilderCountryPickerState();
}

class _FormBuilderCountryPickerState extends State<FormBuilderCountryPicker> {
bool _readOnly = false;
final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>();
FormBuilderState _formState;
Country _selectedDialogCountry;

void _openCupertinoCountryPicker() => showCupertinoModalPopup<void>(
context: context,
builder: (BuildContext context) {
return CountryPickerCupertino(
pickerSheetHeight: widget.cupertinoPickerSheetHeight ?? 300.0,
onValuePicked: (Country country) => setState(() => _selectedDialogCountry = country),
itemFilter: widget.countryFilterByIsoCode != null
? (c) => widget.countryFilterByIsoCode.contains(c.isoCode)
: null,
priorityList: widget.priorityListByIsoCode != null
? List.generate(widget.priorityListByIsoCode.length,
(index) => CountryPickerUtils.getCountryByIsoCode(widget.priorityListByIsoCode[index]))
: null,
);
},
);

void _openCountryPickerDialog() => showDialog(
context: context,
builder: (context) => Theme(
data: Theme.of(context).copyWith(
cursorColor: Theme.of(context).primaryColor,
primaryColor: widget.cursorColor ?? Theme.of(context).primaryColor,
),
child: CountryPickerDialog(
titlePadding: widget.titlePadding ?? EdgeInsets.all(8.0),
searchCursorColor: widget.cursorColor ?? Theme.of(context).primaryColor,
searchInputDecoration: InputDecoration(hintText: widget.searchText ?? 'Search...'),
isSearchable: widget.isSearchable ?? true,
title: widget.dialogTitle ??
Text(
'Select Your Country',
style: widget.dialogTextStyle ?? widget.style,
),
onValuePicked: (Country country) => setState(() => _selectedDialogCountry = country),
itemFilter: widget.countryFilterByIsoCode != null
? (c) => widget.countryFilterByIsoCode.contains(c.isoCode)
: null,
priorityList: widget.priorityListByIsoCode != null
? List.generate(widget.priorityListByIsoCode.length,
(index) => CountryPickerUtils.getCountryByIsoCode(widget.priorityListByIsoCode[index]))
: null,
itemBuilder: _buildDialogItem,
),
),
);

Widget _buildDialogItem(Country country) => Container(
child: ListTile(
contentPadding: EdgeInsets.zero,
leading: CountryPickerUtils.getDefaultFlagImage(country),
title: Text("${country.name}"),
visualDensity: VisualDensity.compact,
),
);

@override
void initState() {
_formState = FormBuilder.of(context);
_formState?.registerFieldKey(widget.attribute, _fieldKey);
_selectedDialogCountry = CountryPickerUtils.getCountryByIsoCode(widget.defaultSelectedCountryIsoCode);

super.initState();
}

@override
void dispose() {
_formState?.unregisterFieldKey(widget.attribute);
super.dispose();
}

@override
Widget build(BuildContext context) {
_readOnly = (_formState?.readOnly == true) ? true : widget.readOnly;

return FormField<Country>(
key: _fieldKey,
enabled: !_readOnly,
initialValue: CountryPickerUtils.getCountryByIsoCode(widget.defaultSelectedCountryIsoCode),
validator: (val) {
for (int i = 0; i < widget.validators.length; i++) {
if (widget.validators[i](val) != null) return widget.validators[i](val);
}
return null;
},
onSaved: (val) {
dynamic transformed;
if (widget.valueTransformer != null) {
transformed = widget.valueTransformer(_selectedDialogCountry);
_formState?.setAttributeValue(widget.attribute, transformed);
} else
_formState?.setAttributeValue(widget.attribute, _selectedDialogCountry.name);
if (widget.onSaved != null) {
widget.onSaved(transformed ?? _selectedDialogCountry.name);
}
},
builder: (FormFieldState<Country> field) {
return GestureDetector(
onTap: widget.isCupertinoPicker != null
? (widget.isCupertinoPicker ? _openCupertinoCountryPicker : _openCountryPickerDialog)
: _openCountryPickerDialog,
child: InputDecorator(
decoration: widget.decoration.copyWith(
errorText: field.errorText,
),
child: Row(
children: [
CountryPickerUtils.getDefaultFlagImage(_selectedDialogCountry),
SizedBox(
width: 10,
),
Expanded(
child: Text(
"${_selectedDialogCountry.name}",
style: widget.style,
),
),
],
),
),
);
},
);
}
}
Loading

0 comments on commit 6585986

Please sign in to comment.