form_builder_thai_provinces 0.1.0
form_builder_thai_provinces: ^0.1.0 copied to clipboard
A flutter_form_builder field for picking a Thai province/district/subdistrict address (cascading dropdowns or type-ahead), with two-way value sync.
form_builder_thai_provinces #
A flutter_form_builder field for Thai province/district/subdistrict address
selection — cascading dropdowns or a type-ahead field, with full two-way
FormBuilder value synchronisation.
This is a thin adapter around
thai_provinces_flutter.
It carries the flutter_form_builder
dependency so the core thai_provinces_flutter package stays
dependency-free.
Install #
dependencies:
form_builder_thai_provinces: ^0.1.0
A single import gives you everything (the core widgets, ThaiAddressSelection,
ThaiAddressController, ThaiAddressLanguage and ThaiAddressValidators are
all re-exported):
import 'package:form_builder_thai_provinces/form_builder_thai_provinces.dart';
Usage #
FormBuilderThaiAddress is a FormBuilderField<ThaiAddressSelection>, so it
plugs straight into a FormBuilder and works with saveAndValidate(),
patchValue(), reset() and fields['name'].value.
Cascading dropdowns (default), with a validator #
final formKey = GlobalKey<FormBuilderState>();
FormBuilder(
key: formKey,
child: FormBuilderThaiAddress(
name: 'address',
validator: ThaiAddressValidators.required(),
// mode: FormBuilderThaiAddressMode.cascading, // default
// language: ThaiAddressLanguage.thai, // default
// showPostcode: true, // default
),
);
// On submit:
if (formKey.currentState!.saveAndValidate()) {
final ThaiAddressSelection? value =
formKey.currentState!.value['address'] as ThaiAddressSelection?;
print(value?.format());
}
Type-ahead (autocomplete) mode #
FormBuilderThaiAddress(
name: 'address',
mode: FormBuilderThaiAddressMode.autocomplete,
validator: ThaiAddressValidators.required(),
);
Driving the field from the form #
// Pre-fill / update programmatically — the UI follows:
formKey.currentState!.patchValue({
'address': ThaiAddressSelection.fromCodes(subdistrictCode: 100101),
});
// Reset back to the initialValue:
formKey.currentState!.reset();
How it works #
FormBuilderThaiAddress wraps a ThaiAddressPicker (or
ThaiAddressAutocompleteField) in a public FormBuilderField. An internal
ThaiAddressController is kept in lock-step with the field value in both
directions: picker changes call field.didChange(...), and external form
changes (reset()/patchValue()) are pushed back into the controller. The
current validation error is surfaced in the field.
License #
MIT © MaIII (ultramcu)