|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_form_builder/flutter_form_builder.dart'; |
| 3 | + |
| 4 | +class RelatedFields extends StatefulWidget { |
| 5 | + const RelatedFields({Key? key}) : super(key: key); |
| 6 | + |
| 7 | + @override |
| 8 | + State<RelatedFields> createState() => _RelatedFieldsState(); |
| 9 | +} |
| 10 | + |
| 11 | +class _RelatedFieldsState extends State<RelatedFields> { |
| 12 | + final _formKey = GlobalKey<FormBuilderState>(); |
| 13 | + String country = ''; |
| 14 | + String city = ''; |
| 15 | + List<String> cities = []; |
| 16 | + |
| 17 | + @override |
| 18 | + void initState() { |
| 19 | + country = _allCountries.first; |
| 20 | + city = _allUsaCities.first; |
| 21 | + cities = _allUsaCities; |
| 22 | + super.initState(); |
| 23 | + } |
| 24 | + |
| 25 | + @override |
| 26 | + Widget build(BuildContext context) { |
| 27 | + return FormBuilder( |
| 28 | + key: _formKey, |
| 29 | + child: Column( |
| 30 | + children: <Widget>[ |
| 31 | + const SizedBox(height: 20), |
| 32 | + FormBuilderDropdown<String>( |
| 33 | + name: 'country', |
| 34 | + decoration: const InputDecoration( |
| 35 | + label: Text('Countries'), |
| 36 | + ), |
| 37 | + initialValue: country, |
| 38 | + onChanged: (value) { |
| 39 | + setState(() { |
| 40 | + country = value ?? ''; |
| 41 | + city = ''; |
| 42 | + changeCities(); |
| 43 | + }); |
| 44 | + }, |
| 45 | + items: _allCountries |
| 46 | + .map((e) => DropdownMenuItem( |
| 47 | + value: e, |
| 48 | + child: Text(e), |
| 49 | + )) |
| 50 | + .toList(), |
| 51 | + ), |
| 52 | + const SizedBox(height: 10), |
| 53 | + FormBuilderDropdown<String>( |
| 54 | + name: 'city', |
| 55 | + decoration: const InputDecoration( |
| 56 | + label: Text('Cities'), |
| 57 | + ), |
| 58 | + initialValue: city, |
| 59 | + items: cities |
| 60 | + .map((e) => DropdownMenuItem( |
| 61 | + value: e, |
| 62 | + child: Text(e), |
| 63 | + )) |
| 64 | + .toList(), |
| 65 | + ), |
| 66 | + const SizedBox(height: 10), |
| 67 | + MaterialButton( |
| 68 | + color: Theme.of(context).colorScheme.secondary, |
| 69 | + child: const Text( |
| 70 | + "Submit", |
| 71 | + style: TextStyle(color: Colors.white), |
| 72 | + ), |
| 73 | + onPressed: () { |
| 74 | + _formKey.currentState!.saveAndValidate(); |
| 75 | + debugPrint(_formKey.currentState?.instantValue.toString() ?? ''); |
| 76 | + }, |
| 77 | + ), |
| 78 | + ], |
| 79 | + ), |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + void changeCities() { |
| 84 | + switch (country) { |
| 85 | + case 'France': |
| 86 | + cities = _allFranceCities; |
| 87 | + break; |
| 88 | + case 'United States': |
| 89 | + cities = _allUsaCities; |
| 90 | + break; |
| 91 | + default: |
| 92 | + cities = []; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +const _allCountries = [ |
| 98 | + 'United States', |
| 99 | + 'France', |
| 100 | +]; |
| 101 | + |
| 102 | +const _allUsaCities = [ |
| 103 | + 'California', |
| 104 | + 'Another city', |
| 105 | +]; |
| 106 | + |
| 107 | +const _allFranceCities = [ |
| 108 | + 'Paris', |
| 109 | + 'Another city', |
| 110 | +]; |
0 commit comments