Skip to content

Commit

Permalink
Made dynamic types available to use in multiselect
Browse files Browse the repository at this point in the history
  • Loading branch information
Maurice Hennig committed Jul 19, 2021
1 parent 1e82df7 commit 6116a5c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
37 changes: 30 additions & 7 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class Home extends StatefulWidget {
}

class _HomeState extends State<Home> {

List<String> selected = [];
List<dynamic> selected = [];

@override
Widget build(BuildContext context) {
Expand All @@ -37,16 +36,40 @@ class _HomeState extends State<Home> {
child: Padding(
padding: const EdgeInsets.all(20.0),
child: DropDownMultiSelect(
onChanged: (List<String> x) {
setState(() {
selected =x;
});
onChanged: (List<dynamic> x) {
print(x);
},
options: ['a' , 'b' , 'c' , 'd'],
options: [new TestClass("a", 1), new TestClass("b", 2), "c", "d"],
selectedValues: selected,
whenEmpty: 'Select Something',
),
),
));
}
}

class TestClass {
final String caption;
final int value;

TestClass(this.caption, this.value);

@override
String toString() {
// TODO: implement toString
return this.caption;
}

@override
bool operator ==(dynamic other) {
if (other is TestClass) {
return other.value == value;
}

return false;
}

@override
// TODO: implement hashCode
int get hashCode => super.hashCode;
}
18 changes: 10 additions & 8 deletions lib/multiselect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ class _SelectRow extends StatelessWidget {
///
class DropDownMultiSelect extends StatefulWidget {
/// The options form which a user can select
final List<String> options;
final List<dynamic> options;

/// Selected Values
final List<String> selectedValues;
final List<dynamic> selectedValues;

/// This function is called whenever a value changes
final Function(List<String>) onChanged;
final Function(List<dynamic>) onChanged;

/// defines whether the field is dense
final bool isDense;
Expand All @@ -61,10 +61,10 @@ class DropDownMultiSelect extends StatefulWidget {
final String? whenEmpty;

/// a function to build custom childern
final Widget Function(List<String> selectedValues)? childBuilder;
final Widget Function(List<dynamic> selectedValues)? childBuilder;

/// a function to build custom menu items
final Widget Function(String option)? menuItembuilder;
final Widget Function(dynamic option)? menuItembuilder;

const DropDownMultiSelect({
Key? key,
Expand Down Expand Up @@ -97,12 +97,14 @@ class _DropDownMultiSelectState extends State<DropDownMultiSelect> {
EdgeInsets.symmetric(vertical: 15, horizontal: 10),
child: Text(widget.selectedValues.length > 0
? widget.selectedValues
.reduce((a, b) => a + ' , ' + b)
.reduce(
(a, b) => a.toString() + ' , ' + b.toString())
.toString()
: widget.whenEmpty ?? '')),
alignment: Alignment.centerLeft)),
Align(
alignment: Alignment.centerLeft,
child: DropdownButtonFormField<String>(
child: DropdownButtonFormField<dynamic>(
decoration: widget.decoration != null
? widget.decoration
: InputDecoration(
Expand Down Expand Up @@ -130,7 +132,7 @@ class _DropDownMultiSelectState extends State<DropDownMultiSelect> {
? widget.menuItembuilder!(x)
: _SelectRow(
selected: widget.selectedValues.contains(x),
text: x,
text: x.toString(),
onChange: (isSelected) {
if (isSelected) {
var ns = widget.selectedValues;
Expand Down

0 comments on commit 6116a5c

Please sign in to comment.