16.4 洲省选择器
实现步骤:
第 1 步:控制器
lib/pages/my/my_address/controller.dart
1 2 3 4
| List<KeyValueModel> statesList = [];
List<int> statesSels = [];
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| void _filterStates(String countryCode) { for (var i = 0; i < continents.length; i++) { var continent = continents[i]; var country = continent.countries!.firstWhereOrNull((el) => el.code == countryCode); if (country != null) { statesList = List.generate(country.states?.length ?? 0, (index) { var state = country.states?.elementAt(index); return KeyValueModel<String>( key: state?.code ?? "-", value: state?.name ?? "-", ); }); break; } } }
|
1 2 3 4 5 6 7 8 9
| Future<void> _initData() async {
String statesCode = statesController.text; _filterStates(countryCode); statesSels = [statesList.indexWhere((el) => el.key == statesCode)];
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| void onStatesPicker() async { ActionBottomSheet.data( title: 'States', context: Get.context!, adapter: PickerDataAdapter<KeyValueModel>( pickerdata: statesList, ), selecteds: statesSels, onConfirm: (value) { if (value.isEmpty) return; statesController.text = '${value[0].key}'; }, ); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| void onCountryPicker() async { ActionBottomSheet.data( ... onConfirm: (value) { if (value.isEmpty) return; if (value.length == 2) { countryController.text = '${value[1].key}'; _filterStates(value[1].key); } }, ); }
|
第 2 步:视图
lib/pages/my/my_address/view.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Widget _buildForm() { return Form( key: controller.formKey, autovalidateMode: AutovalidateMode.onUserInteraction, child: <Widget>[
TextFormWidget( onTap: controller.onStatesPicker, readOnly: true, isMustBeEnter: true, controller: controller.statesController, labelText: LocaleKeys.addressState.tr, validator: Validatorless.multiple([ Validatorless.required("The field is obligatory"), ]), ),
...
|
提交代码到 git