nepal_address_flutter 0.1.1
nepal_address_flutter: ^0.1.1 copied to clipboard
Flutter UI widgets and form fields for cascading Nepal province, district, local level, and ward selection with bilingual support.
/// Example Flutter application for `nepal_address_flutter`.
///
/// Demonstrates [NepalAddressFormField] with cascading dropdowns,
/// language switching, form validation, and address serialization.
///
/// Run this example:
/// flutter run
///
/// More widgets, search and dataset info: flutter run -t lib/showcase.dart
library;
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:nepal_address_flutter/nepal_address_flutter.dart';
void main() => runApp(const MaterialApp(home: NepalAddressExamplePage()));
class NepalAddressExamplePage extends StatefulWidget {
const NepalAddressExamplePage({super.key});
@override
State<NepalAddressExamplePage> createState() =>
_NepalAddressExamplePageState();
}
class _NepalAddressExamplePageState extends State<NepalAddressExamplePage> {
final _formKey = GlobalKey<FormState>();
// 1) language
NepalAddressLanguage _language = NepalAddressLanguage.english;
SelectedAddress? _submittedAddress;
void _submit() {
if (_formKey.currentState?.validate() ?? false) {
_formKey.currentState?.save();
setState(() {});
}
}
void _reset() {
_formKey.currentState?.reset();
setState(() => _submittedAddress = null);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Nepal Address Form Example')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// 1) language
SegmentedButton<NepalAddressLanguage>(
segments: const [
ButtonSegment(
value: NepalAddressLanguage.english,
label: Text('English'),
),
ButtonSegment(
value: NepalAddressLanguage.nepali,
label: Text('नेपाली'),
),
ButtonSegment(
value: NepalAddressLanguage.bilingual,
label: Text('Bilingual'),
),
],
selected: {_language},
onSelectionChanged: (s) => setState(() => _language = s.first),
),
const SizedBox(height: 16),
// 2) form field
Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
NepalAddressFormField(
showWard: true,
language: _language,
requiredLevels: const {
NepalAddressLevel.province,
NepalAddressLevel.district,
NepalAddressLevel.localLevel,
NepalAddressLevel.ward,
},
onSaved: (value) => _submittedAddress = value,
),
const SizedBox(height: 16),
Row(
children: [
Expanded(
child: FilledButton.icon(
onPressed: _submit,
icon: const Icon(Icons.check),
label: const Text('Submit'),
),
),
const SizedBox(width: 12),
Expanded(
child: OutlinedButton.icon(
onPressed: _reset,
icon: const Icon(Icons.refresh),
label: const Text('Reset'),
),
),
],
),
],
),
),
// 3) read the result
if (_submittedAddress != null) ...[
const SizedBox(height: 20),
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Submitted Address',
style: Theme.of(context).textTheme.titleMedium,
),
const Divider(),
Text('English: ${_submittedAddress!.en}'),
Text('Nepali: ${_submittedAddress!.np}'),
Text('Bilingual: ${_submittedAddress!.bilingual}'),
const SizedBox(height: 8),
SelectableText(
'JSON: ${jsonEncode(_submittedAddress!.toJson())}',
style: const TextStyle(fontFamily: 'monospace'),
),
],
),
),
),
],
],
),
),
);
}
}