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.
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:form_builder_thai_provinces/form_builder_thai_provinces.dart';
void main() => runApp(const ExampleApp());
/// The example application root.
class ExampleApp extends StatelessWidget {
/// Creates the example app.
const ExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'form_builder_thai_provinces example',
theme: ThemeData(useMaterial3: true),
home: const ExamplePage(),
);
}
}
/// A page with a [FormBuilder] holding a [FormBuilderThaiAddress] and a submit
/// button that validates and shows the saved value.
class ExamplePage extends StatefulWidget {
/// Creates the example page.
const ExamplePage({super.key});
@override
State<ExamplePage> createState() => _ExamplePageState();
}
class _ExamplePageState extends State<ExamplePage> {
final _formKey = GlobalKey<FormBuilderState>();
FormBuilderThaiAddressMode _mode = FormBuilderThaiAddressMode.cascading;
String _result = '';
void _submit() {
final saved = _formKey.currentState!.saveAndValidate();
if (!saved) {
setState(() => _result = 'Invalid — please complete the address.');
return;
}
final value =
_formKey.currentState!.value['address'] as ThaiAddressSelection?;
setState(() => _result = 'Saved: ${value?.format() ?? '(none)'}');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Thai address — FormBuilder')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: FormBuilder(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SegmentedButton<FormBuilderThaiAddressMode>(
segments: const [
ButtonSegment(
value: FormBuilderThaiAddressMode.cascading,
label: Text('Cascading'),
),
ButtonSegment(
value: FormBuilderThaiAddressMode.autocomplete,
label: Text('Autocomplete'),
),
],
selected: {_mode},
onSelectionChanged: (s) => setState(() => _mode = s.first),
),
const SizedBox(height: 16),
FormBuilderThaiAddress(
// Re-key so the field rebuilds cleanly when the mode flips.
key: ValueKey(_mode),
name: 'address',
mode: _mode,
validator: ThaiAddressValidators.required(),
),
const SizedBox(height: 16),
FilledButton(onPressed: _submit, child: const Text('Submit')),
const SizedBox(height: 12),
Text(_result),
],
),
),
),
);
}
}