Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 3 custom parameters: header Widget, onReset/0 method, onSelected/2 method to solve the problem of external components calling operation FilterState data #46

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
62 changes: 42 additions & 20 deletions lib/src/filter_list_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class FilterListWidget<T extends Object> extends StatelessWidget {
required this.validateSelectedItem,
this.validateRemoveItem,
required this.choiceChipLabel,
required this.onItemSearch,
this.onItemSearch,
this.selectedListData,
this.onApplyButtonClick,
this.choiceChipBuilder,
Expand All @@ -81,6 +81,9 @@ class FilterListWidget<T extends Object> extends StatelessWidget {
ControlButtonType.All,
ControlButtonType.Reset
],
this.header,
this.onReset,
this.onSelected,
}) : super(key: key);

/// Filter theme
Expand Down Expand Up @@ -112,6 +115,12 @@ class FilterListWidget<T extends Object> extends StatelessWidget {
/// Default is `Navigator.pop(context, null)`
final void Function()? onCloseWidgetPress;

final Widget? header;
///
final void Function()? onReset;
///
final void Function(T, bool)? onSelected;

/// If true then it hide complete header section.
final bool? hideHeader;

Expand All @@ -137,7 +146,7 @@ class FilterListWidget<T extends Object> extends StatelessWidget {
final ValidateRemoveItem<T>? validateRemoveItem;

/// The `onItemSearch` is delegate which filter the list on the basis of search field text.
final SearchPredict<T> onItemSearch; /*required*/
final SearchPredict<T>? onItemSearch;

/// The `choiceChipLabel` is callback which required [String] value to display text on choice chip.
final LabelDelegate<T> choiceChipLabel; /*required*/
Expand Down Expand Up @@ -170,6 +179,33 @@ class FilterListWidget<T extends Object> extends StatelessWidget {
/// {@endtemplate}
final List<ControlButtonType> controlButtons;

Widget _buildHeader(BuildContext context) {
if (header != null) {
return header!;
}
if (hideHeader!) {
return const SizedBox();
}
return Header(
headlineText: headlineText,
hideSearchField: hideSearchField,
hideCloseIcon: hideCloseIcon,
headerCloseIcon: headerCloseIcon,
onSearch: (String value) {
if (value.isEmpty) {
FilterState
.of<T>(context)
.items = listData;
return;
}
if (onItemSearch != null) {
FilterState.of<T>(context)
.filter((item) => onItemSearch!(item, value));
}
},
onCloseWidgetPress: onCloseWidgetPress,
);
}
Widget _body(BuildContext context) {
final theme = FilterListTheme.of(context);
return Container(
Expand All @@ -178,24 +214,7 @@ class FilterListWidget<T extends Object> extends StatelessWidget {
children: <Widget>[
Column(
children: <Widget>[
if (hideHeader!)
const SizedBox()
else
Header(
headlineText: headlineText,
hideSearchField: hideSearchField,
hideCloseIcon: hideCloseIcon,
headerCloseIcon: headerCloseIcon,
onSearch: (String value) {
if (value.isEmpty) {
FilterState.of<T>(context).items = listData;
return;
}
FilterState.of<T>(context)
.filter((item) => onItemSearch(item, value));
},
onCloseWidgetPress: onCloseWidgetPress,
),
_buildHeader(context),
if (!hideSelectedTextCount)
Padding(
padding: const EdgeInsets.only(top: 5),
Expand All @@ -213,6 +232,7 @@ class FilterListWidget<T extends Object> extends StatelessWidget {
enableOnlySingleSelection: enableOnlySingleSelection,
validateSelectedItem: validateSelectedItem,
validateRemoveItem: validateRemoveItem,
onSelected: onSelected,
maximumSelectionLength: maximumSelectionLength,
),
),
Expand All @@ -227,6 +247,7 @@ class FilterListWidget<T extends Object> extends StatelessWidget {
applyButtonText: applyButtonText,
resetButtonText: resetButtonText,
enableOnlySingleSelection: enableOnlySingleSelection,
onReset:onReset,
onApplyButtonClick: () {
final selectedItems = FilterState.of<T>(context).selectedItems;
if (onApplyButtonClick != null) {
Expand Down Expand Up @@ -261,4 +282,5 @@ class FilterListWidget<T extends Object> extends StatelessWidget {
),
);
}

}
6 changes: 6 additions & 0 deletions lib/src/widget/choice_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ class ChoiceList<T> extends StatelessWidget {
this.choiceChipLabel,
this.enableOnlySingleSelection = false,
this.validateRemoveItem,
this.onSelected,
this.maximumSelectionLength,
}) : super(key: key);
final ValidateSelectedItem<T> validateSelectedItem;
final ChoiceChipBuilder? choiceChipBuilder;
final LabelDelegate<T>? choiceChipLabel;
final bool enableOnlySingleSelection;
final ValidateRemoveItem<T>? validateRemoveItem;
///
final void Function(T, bool)? onSelected;
final int? maximumSelectionLength;

List<Widget> _buildChoiceList(BuildContext context) {
Expand Down Expand Up @@ -63,6 +66,9 @@ class ChoiceList<T> extends StatelessWidget {
state.addSelectedItem(item);
}
}
if (onSelected != null) {
onSelected!(item, !selected);
}
},
selected: selected,
text: choiceChipLabel!(item),
Expand Down
8 changes: 7 additions & 1 deletion lib/src/widget/control_button_bar.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:filter_list/filter_list.dart';
import 'package:filter_list/src/state/filter_state.dart';
import 'package:filter_list/src/state/provider.dart';

import 'package:filter_list/src/widget/control_button.dart';
import 'package:flutter/material.dart';

Expand All @@ -17,6 +16,7 @@ class ControlButtonBar<T> extends StatelessWidget {
this.resetButtonText,
this.applyButtonText,
this.onApplyButtonClick,
this.onReset,
this.maximumSelectionLength,
required this.controlButtons,
}) : super(key: key);
Expand All @@ -27,6 +27,9 @@ class ControlButtonBar<T> extends StatelessWidget {
final VoidCallback? onApplyButtonClick;
final int? maximumSelectionLength;

///
final void Function()? onReset;

/// {@macro control_buttons}
final List<ControlButtonType> controlButtons;

Expand Down Expand Up @@ -80,6 +83,9 @@ class ControlButtonBar<T> extends StatelessWidget {
rebuildOnChange: true,
);
state.selectedItems = [];
if (onReset != null) {
onReset!();
}
},
),
SizedBox(width: theme.buttonSpacing),
Expand Down