Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import 'package:flutter/material.dart';
/// * [SfGlobalLocalizations], which provides localizations for many languages.
///
abstract class SfLocalizations {
/// Text mask for dates that adheres to the defined locale.
/// This label is displayed when the filter popup for a column is opened in the SfDataGrid.
String get dateFormat;

/// Label that is displayed when no date is selected in a calendar widget.
/// This label is displayed under agenda section in month view.
String get noSelectedDateCalendarLabel;
Expand Down Expand Up @@ -488,6 +492,9 @@ class _SfLocalizationDelegates extends LocalizationsDelegate<SfLocalizations> {
class _DefaultLocalizations implements SfLocalizations {
const _DefaultLocalizations();

@override
String get dateFormat => 'yyyy-MM-dd';

@override
String get noSelectedDateCalendarLabel => 'No selected date';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1961,7 +1961,7 @@ class FilterCondition {
class DataGridFilterHelper {
/// Creates the [DataGridFilterHelper] for [SfDataGrid].
DataGridFilterHelper(this._dataGridStateDetails) {
checkboxFilterHelper = DataGridCheckboxFilterHelper();
checkboxFilterHelper = DataGridCheckboxFilterHelper(_dataGridStateDetails);
advancedFilterHelper = DataGridAdvancedFilterHelper(_dataGridStateDetails);
}

Expand Down Expand Up @@ -2112,7 +2112,7 @@ class DataGridFilterHelper {
}

/// Format the given cell value to the string data type to display.
String getDisplayValue(Object? value) {
String getDisplayValue(Object? value, [String? dateMask]) {
if (value != null) {
// Should return if the value defines the blank filter.
if (value == '(Blanks)') {
Expand All @@ -2125,12 +2125,35 @@ class DataGridFilterHelper {
return value is! String ? value.toString() : value;
case AdvancedFilterType.date:
final DateTime date = value as DateTime;
return date.toString().split(' ').first;

if (dateMask == null || dateMask.isEmpty) {
return date.toString().split(' ').first;
}

final String formattedDate = formatDate(date, dateMask);
return formattedDate;
}
}
return '';
}

/// Format dateTime based on mask.
static String formatDate(DateTime date, String mask) {
final Map<String, String> replacements = <String, String>{
'dd': date.day.toString().padLeft(2, '0'),
'MM': date.month.toString().padLeft(2, '0'),
'yyyy': date.year.toString(),
'yy': date.year.toString().substring(2, 4),
};

String formattedDate = mask;
replacements.forEach((String key, String value) {
formattedDate = formattedDate.replaceAll(key, value);
});

return formattedDate;
}

/// Format the given string value to the actual cell value with same data type.
Object? getActualValue(Object? value) {
if (value != null) {
Expand Down Expand Up @@ -2724,10 +2747,15 @@ class DataGridFilterHelper {
/// A class [DataGridCheckboxFilterHelper] that holds the helper properties
/// for the checkbox filter.
class DataGridCheckboxFilterHelper {
/// Creates `DataGridCheckboxFilterHelper` for `SfDataGrid`.
DataGridCheckboxFilterHelper(this._dataGridStateDetails);

/// Holds all the cell values of corresponding filter column as a
/// `FilterElement` collection.
List<FilterElement> items = <FilterElement>[];

final DataGridStateDetails _dataGridStateDetails;

/// Holds the searched check box items.
List<FilterElement> _searchedItems = <FilterElement>[];

Expand Down Expand Up @@ -2789,12 +2817,19 @@ class DataGridCheckboxFilterHelper {
return;
}

_searchedItems = filterCheckboxItems
.where((FilterElement element) => element.value
_searchedItems = filterCheckboxItems.where((FilterElement element) {
if (element.value is DateTime) {
final DateTime date = element.value as DateTime;
final String formattedDate = DataGridFilterHelper.formatDate(
date, _dataGridStateDetails().localizations.dateFormat);
return formattedDate.toLowerCase().contains(searchText.toLowerCase());
} else {
return element.value
.toString()
.toLowerCase()
.contains(searchText.toLowerCase()))
.toList();
.contains(searchText.toLowerCase());
}
}).toList();

for (final FilterElement element in _searchedItems) {
element.isSelected = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,8 @@ class _CheckboxFilterMenu extends StatelessWidget {
if (filterHelper.items.isNotEmpty) {
final FilterElement element = filterHelper.items[index];
final String displayText = dataGridConfiguration.dataGridFilterHelper!
.getDisplayValue(element.value);
.getDisplayValue(
element.value, dataGridConfiguration.localizations.dateFormat);
return _FilterPopupMenuTile(
style: style,
height: isMobile ? style.fontSize! + 34 : style.fontSize! + 26,
Expand Down Expand Up @@ -2054,7 +2055,8 @@ class _AdvancedFilterPopupMenu extends StatelessWidget {
.map<DropdownMenuItem<Object>>((FilterElement value) =>
DropdownMenuItem<Object>(
value: value.value,
child: Text(helper.getDisplayValue(value.value))))
child: Text(helper.getDisplayValue(value.value,
dataGridConfiguration.localizations.dateFormat))))
.toList(),
onChanged: enableDropdownButton(isTopButton) ? setValue : null,
),
Expand Down
Loading