Skip to content

Commit

Permalink
New feature and update goldens
Browse files Browse the repository at this point in the history
  • Loading branch information
JhonaCodes committed Jan 7, 2025
1 parent af96158 commit 8d281b7
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ migrate_working_dir/
.dart_tool/
build/
test/failures
/example/multiexample/
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.6.0
- New feature `selectAllOption`, You will have a default option to select all the items on your list.

## 1.5.5
- Some format tweaks.
- Update to `withValues` on colors.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Add the dependency to your `pubspec.yaml` file:

```yaml
dependencies:
multiselect_field: ^1.5.5
multiselect_field: ^1.6.0
```
Then, install the dependencies using:
Expand Down
4 changes: 2 additions & 2 deletions lib/core/chip_multiselect_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class ChipMultiselectField extends StatelessWidget {
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5),
side: BorderSide(
color: Colors.grey.withValues(alpha: 255 * 0.3),
color: Colors.grey.withValues(alpha: 255 * 0.4),
style: BorderStyle.solid,
width: 0.5,
strokeAlign: 0.5,
Expand All @@ -56,7 +56,7 @@ final class ChipMultiselectField extends StatelessWidget {
deleteIcon: Align(
alignment: Alignment.center,
child: Icon(Icons.close,
size: 15, color: Colors.grey.withValues(alpha: 255 * 0.5)),
size: 15, color: Colors.grey.withValues(alpha: 255 * 0.7)),
),
onDeleted: onDeleted,
);
Expand Down
74 changes: 65 additions & 9 deletions lib/core/multi_select.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class MultiSelectField<T> extends StatefulWidget {
final String? label;
final TextStyle? textStyleLabel;

final bool selectAllOption;

const MultiSelectField({
super.key,
required this.data,
Expand All @@ -138,6 +140,7 @@ class MultiSelectField<T> extends StatefulWidget {
this.titleMenuStyle,
this.textStyleSingleSelection,
this.cleanCurrentSelection = false,
this.selectAllOption = false,
this.label,
this.textStyleLabel,
});
Expand Down Expand Up @@ -169,10 +172,17 @@ class _MultiSelectFieldState<T> extends State<MultiSelectField<T>>

/// It helps to show the complete list of elements after selecting an element using the text filter.
bool _onSelected = false;
bool _selectAllActive = false;

List<Choice<T>> get _cleanData => widget
.data()
.where((ele) => ele.key != null || ele.key!.isNotEmpty)
.toList();

@override
void initState() {
super.initState();

if (widget.defaultData != null && widget.defaultData!.isNotEmpty) {
_selectedChoice.clear();
_selectedChoice.addAll(widget.defaultData!);
Expand Down Expand Up @@ -215,6 +225,15 @@ class _MultiSelectFieldState<T> extends State<MultiSelectField<T>>
_selectedChoice.clear();
}

/// Verification for select all data, if you select one per one and you have select all option, you nee to si is you're selected all data.
bool equalData = isSameData(_selectedChoice, widget.data());

if (equalData) {
_selectAllActive = true;
} else if (_selectAllActive && widget.selectAllOption && !equalData) {
_selectAllActive = false;
}

super.didUpdateWidget(oldWidget);
}

Expand Down Expand Up @@ -413,28 +432,55 @@ class _MultiSelectFieldState<T> extends State<MultiSelectField<T>>
),
),
menuChildren: [
if (widget.selectAllOption)
Padding(
padding: const EdgeInsets.only(top: 5),
child: CheckboxMenuButton(
value: _selectAllActive,
onChanged: (vale) {
_selectAllActive = !_selectAllActive;

_selectedChoice = _selectAllActive ? _cleanData : [];
widget.onSelect(_selectedChoice, false);
},
child: Text("All"),
),
),
..._onFilteredChoice
.where((element) => element.value.isNotEmpty)
.map(
(result) {
bool isGroupingTitle =
result.key == null || result.key!.isEmpty;
return SizedBox(
width:
widget.menuWidthBaseOnContent ? null : size.maxWidth,
width: widget.menuWidthBaseOnContent
? null
: widget.selectAllOption
? (size.maxWidth - 10)
: size.maxWidth,
child: MenuItemButton(
closeOnActivate:
widget.singleSelection || widget.data().length == 1,
key: (!isGroupingTitle && _isSelected(result)) &&
_selectedChoice.indexOf(result) == 0
? _selectedItemKey
: null,
trailingIcon: (!isGroupingTitle && _isSelected(result))
? const Icon(
Icons.check,
color: Colors.green,
size: 12,
)
trailingIcon: !widget.selectAllOption
? (!isGroupingTitle && _isSelected(result))
? const Icon(
Icons.check,
color: Colors.green,
size: 12,
)
: null
: null,
leadingIcon: widget.selectAllOption && !isGroupingTitle
? (_isSelected(result))
? const Icon(
Icons.check_box,
color: Colors.green,
)
: Icon(Icons.check_box_outline_blank)
: null,
style: widget.buttonStyle ??
ButtonStyle(
Expand Down Expand Up @@ -489,6 +535,10 @@ class _MultiSelectFieldState<T> extends State<MultiSelectField<T>>
MenuStyle(
elevation: const WidgetStatePropertyAll<double>(5),
visualDensity: VisualDensity.adaptivePlatformDensity,
padding: widget.selectAllOption
? WidgetStatePropertyAll<EdgeInsets>(
EdgeInsets.only(left: 10))
: null,
maximumSize: widget.menuWidthBaseOnContent &&
widget.menuHeightBaseOnContent
? null
Expand Down Expand Up @@ -591,7 +641,7 @@ class _MultiSelectFieldState<T> extends State<MultiSelectField<T>>
widget.onSelect(_selectedChoice, false);

/// Clean filtering
_onFilteredChoice = widget.data();
_onFilteredChoice = _cleanData;

// Update the UI with the latest state.
setState(() {});
Expand Down Expand Up @@ -700,3 +750,9 @@ class Choice<T> {
@override
int get hashCode => Object.hash(key, value, metadata);
}

bool isSameData<T>(List<Choice<T>> list1, List<Choice<T>> list2) {
if (list1.length != list2.length) return false;

return Set.from(list1).containsAll(list2);
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: multiselect_field
description: "A flexible dropdown field supporting single/multiple selection modes, styles, titles, etc"
version: 1.5.5
version: 1.6.0
homepage: https://github.com/JhonaCodes/multiselect_field.git

environment:
Expand Down
Binary file modified test/goldens/ci/default_data.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/goldens/ci/default_label.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/goldens/macos/default_data.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/goldens/macos/default_label.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8d281b7

Please sign in to comment.