|
| 1 | +import 'package:core/core.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_news_app_web_dashboard_full_source_code/l10n/l10n.dart'; |
| 4 | + |
| 5 | +/// A reusable dropdown form field for selecting a country. |
| 6 | +class CountryDropdownFormField extends StatelessWidget { |
| 7 | + /// {@macro country_dropdown_form_field} |
| 8 | + const CountryDropdownFormField({ |
| 9 | + required this.countries, |
| 10 | + required this.onChanged, |
| 11 | + this.initialValue, |
| 12 | + this.labelText, |
| 13 | + super.key, |
| 14 | + }); |
| 15 | + |
| 16 | + /// The list of countries to display in the dropdown. |
| 17 | + final List<Country> countries; |
| 18 | + |
| 19 | + /// The currently selected country. |
| 20 | + final Country? initialValue; |
| 21 | + |
| 22 | + /// The callback that is called when the user selects a country. |
| 23 | + final ValueChanged<Country?> onChanged; |
| 24 | + |
| 25 | + /// The text to display as the label for the form field. |
| 26 | + final String? labelText; |
| 27 | + |
| 28 | + @override |
| 29 | + Widget build(BuildContext context) { |
| 30 | + final l10n = AppLocalizationsX(context).l10n; |
| 31 | + |
| 32 | + return DropdownButtonFormField<Country?>( |
| 33 | + value: initialValue, |
| 34 | + decoration: InputDecoration( |
| 35 | + labelText: labelText ?? l10n.countryName, |
| 36 | + border: const OutlineInputBorder(), |
| 37 | + ), |
| 38 | + items: [ |
| 39 | + DropdownMenuItem(value: null, child: Text(l10n.none)), |
| 40 | + ...countries.map( |
| 41 | + (c) => DropdownMenuItem(value: c, child: Text(c.name)), |
| 42 | + ), |
| 43 | + ], |
| 44 | + onChanged: onChanged, |
| 45 | + ); |
| 46 | + } |
| 47 | +} |
0 commit comments