diff --git a/example/pubspec.lock b/example/pubspec.lock index 1202879..54b1447 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -7,7 +7,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.5.0" + version: "2.6.1" boolean_selector: dependency: transitive description: @@ -87,7 +87,7 @@ packages: path: ".." relative: true source: path - version: "0.1.6" + version: "0.1.7" path: dependency: transitive description: @@ -141,7 +141,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.19" + version: "0.3.0" typed_data: dependency: transitive description: @@ -157,4 +157,4 @@ packages: source: hosted version: "2.1.0" sdks: - dart: ">=2.12.0-0.0 <3.0.0" + dart: ">=2.12.0 <3.0.0" diff --git a/lib/multiselect_formfield.dart b/lib/multiselect_formfield.dart index 7f2c823..6e11a5e 100644 --- a/lib/multiselect_formfield.dart +++ b/lib/multiselect_formfield.dart @@ -1,5 +1,6 @@ library multiselect_formfield; +import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; import 'package:multiselect_formfield/multiselect_dialog.dart'; @@ -11,6 +12,7 @@ class MultiSelectFormField extends FormField { final List? dataSource; final String? textField; final String? valueField; + final Key? key; final Function? change; final Function? open; final Function? close; @@ -32,8 +34,9 @@ class MultiSelectFormField extends FormField { FormFieldSetter? onSaved, FormFieldValidator? validator, dynamic initialValue, - bool autovalidate = false, + AutovalidateMode? autovalidate = AutovalidateMode.disabled, this.title = const Text('Title'), + this.key, this.hintWidget = const Text('Tap to select one or more'), this.required = false, this.errorText = 'Please select one or more options', @@ -60,17 +63,18 @@ class MultiSelectFormField extends FormField { this.checkBoxCheckColor, }) : super( onSaved: onSaved, + key:key, validator: validator, initialValue: initialValue, - autovalidate: autovalidate, + autovalidateMode: autovalidate, builder: (FormFieldState state) { List _buildSelectedOptions(state) { List selectedOptions = []; if (state.value != null) { state.value.forEach((item) { - var existingItem = dataSource!.singleWhere(((itm) => itm[valueField] == item), - orElse: () => null); + var existingItem = dataSource!.firstWhereOrNull(((itm) => itm[valueField] == item)); + if (existingItem != null) selectedOptions.add(Chip( labelStyle: chipLabelStyle, backgroundColor: chipBackGroundColor, diff --git a/pubspec.lock b/pubspec.lock index 9e492de..ad6d1df 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,7 +7,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.5.0" + version: "2.6.1" boolean_selector: dependency: transitive description: @@ -127,7 +127,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.19" + version: "0.3.0" typed_data: dependency: transitive description: @@ -143,4 +143,4 @@ packages: source: hosted version: "2.1.0" sdks: - dart: ">=2.12.0-0.0 <3.0.0" + dart: ">=2.12.0 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 8677820..0db96a9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: multiselect_formfield description: A multi select form field using alert dialog to select multiple items with checkboxes and showing as chips. -version: 0.1.6 +version: 0.1.7 author: Carlos E. Torres homepage: https://github.com/cetorres/multiselect_formfield