Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 41 additions & 15 deletions lib/multiselect.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
library multiselect;


import 'package:flutter/material.dart';
import 'package:states_rebuilder/states_rebuilder.dart';


class _TheState {}

var _theState = RM.inject(() => _TheState());
Expand All @@ -29,7 +27,12 @@ class _SelectRow extends StatelessWidget {
final bool selected;
final String text;

const _SelectRow({Key? key, required this.onChange, required this.selected, required this.text}) : super(key: key);
const _SelectRow(
{Key? key,
required this.onChange,
required this.selected,
required this.text})
: super(key: key);

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -82,6 +85,9 @@ class DropDownMultiSelect<T> extends StatefulWidget {
/// this text is shown when there is no selection
final String? whenEmpty;

//define background color to dropdown dialog
final Color? dropdownColor;

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

Expand All @@ -104,7 +110,8 @@ class DropDownMultiSelect<T> extends StatefulWidget {
final Widget? hint;

/// selectedValue separator
final String separator;
final T? separator;

/// style for the selected values
final TextStyle? selectedValuesStyle;

Expand All @@ -114,6 +121,7 @@ class DropDownMultiSelect<T> extends StatefulWidget {
required this.selectedValues,
required this.onChanged,
this.whenEmpty,
this.dropdownColor,
this.icon,
this.hint,
this.hintStyle,
Expand All @@ -125,14 +133,15 @@ class DropDownMultiSelect<T> extends StatefulWidget {
this.decoration,
this.validator,
this.readOnly = false,
this.separator = ' , ',
this.separator,
}) : super(key: key);

@override
_DropDownMultiSelectState createState() => _DropDownMultiSelectState<T>();
}

class _DropDownMultiSelectState<TState> extends State<DropDownMultiSelect<TState>> {
class _DropDownMultiSelectState<TState>
extends State<DropDownMultiSelect<TState>> {
@override
Widget build(BuildContext context) {
return Container(
Expand All @@ -142,20 +151,26 @@ class _DropDownMultiSelectState<TState> extends State<DropDownMultiSelect<TState
_theState.rebuild(() => widget.childBuilder != null
? widget.childBuilder!(widget.selectedValues)
: Padding(
padding:
widget.decoration !=null ? widget.decoration!.contentPadding !=null ? widget.decoration!.contentPadding! : EdgeInsets.symmetric(horizontal: 10) : EdgeInsets.symmetric(horizontal: 10),
padding: widget.decoration != null
? widget.decoration!.contentPadding != null
? widget.decoration!.contentPadding!
: EdgeInsets.symmetric(horizontal: 10)
: EdgeInsets.symmetric(horizontal: 10),
child: Padding(

padding: const EdgeInsets.only(right: 20),
child: Text(widget.selectedValues.length > 0
? widget.selectedValues
.reduce((a, b) => a + widget.separator + b)
? widget.selectedValues.map((e) => e.toString()).reduce(
(a, b) =>
a +
(widget.separator != null
? widget.separator.toString()
: ',') +
b)
: widget.whenEmpty ?? ''),
))),


Container(
child: DropdownButtonFormField<TState>(
dropdownColor: widget.dropdownColor,
hint: widget.hint,
style: widget.hintStyle,
icon: widget.icon,
Expand All @@ -173,7 +188,9 @@ class _DropDownMultiSelectState<TState> extends State<DropDownMultiSelect<TState
isDense: widget.isDense,
onChanged: widget.enabled ? (x) {} : null,
isExpanded: false,
value: widget.selectedValues.length > 0 ? widget.selectedValues[0] : null,
value: widget.selectedValues.length > 0
? widget.selectedValues[0]
: null,
selectedItemBuilder: (context) {
return widget.options
.map((e) => DropdownMenuItem(
Expand Down Expand Up @@ -233,7 +250,16 @@ class _DropDownMultiSelectState<TState> extends State<DropDownMultiSelect<TState
child: Padding(
padding: const EdgeInsets.only(right: 20),
child: Text(
widget.selectedValues.length > 0 ? widget.selectedValues.map((e) => e.toString()).reduce((a, b) => a.toString() + ' , ' + b.toString()) : widget.whenEmpty ?? '',
widget.selectedValues.length > 0
? widget.selectedValues
.map((e) => e.toString())
.reduce((a, b) =>
a.toString() +
(widget.separator != null
? widget.separator.toString()
: ',') +
b.toString())
: widget.whenEmpty ?? '',
style: widget.selectedValuesStyle,
),
))),
Expand Down