From ba7d988f4e709e3812ed6613d113d6caeaccd310 Mon Sep 17 00:00:00 2001 From: Edouard Marquez Date: Sat, 29 Jul 2023 19:16:39 +0200 Subject: [PATCH] Filterable text: ensure to remove accents when comparing (#4394) --- .../widgets/language_selector.dart | 15 ++++++++------- .../pages/onboarding/country_selector.dart | 19 ++++++++++++------- .../smooth_app/lib/widgets/smooth_text.dart | 5 +++-- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/packages/smooth_app/lib/generic_lib/widgets/language_selector.dart b/packages/smooth_app/lib/generic_lib/widgets/language_selector.dart index 71f1424cc14..bf813b3100e 100644 --- a/packages/smooth_app/lib/generic_lib/widgets/language_selector.dart +++ b/packages/smooth_app/lib/generic_lib/widgets/language_selector.dart @@ -1,3 +1,4 @@ +import 'package:diacritic/diacritic.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:openfoodfacts/openfoodfacts.dart'; @@ -145,19 +146,19 @@ class LanguageSelector extends StatelessWidget { prefixIcon: const Icon(Icons.search), controller: languageSelectorController, onChanged: (String? query) { - query = query?.trim().toLowerCase(); + query = removeDiacritics(query!.trim().toLowerCase()); setState( () { filteredList = leftovers .where((OpenFoodFactsLanguage item) => - _languages - .getNameInEnglish(item) - .toLowerCase() + removeDiacritics(_languages + .getNameInEnglish(item) + .toLowerCase()) .contains(query!.toLowerCase()) || - _languages - .getNameInLanguage(item) - .toLowerCase() + removeDiacritics(_languages + .getNameInLanguage(item) + .toLowerCase()) .contains(query.toLowerCase()) || item.code.contains(query)) .toList(); diff --git a/packages/smooth_app/lib/pages/onboarding/country_selector.dart b/packages/smooth_app/lib/pages/onboarding/country_selector.dart index 0c005dc4292..090acfcf96f 100644 --- a/packages/smooth_app/lib/pages/onboarding/country_selector.dart +++ b/packages/smooth_app/lib/pages/onboarding/country_selector.dart @@ -1,3 +1,4 @@ +import 'package:diacritic/diacritic.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; @@ -95,19 +96,23 @@ class _CountrySelectorState extends State { prefixIcon: const Icon(Icons.search), controller: _countryController, onChanged: (String? query) { - query = query?.trim().toLowerCase(); + query = removeDiacritics(query!.trim().toLowerCase()); setState( () { filteredList = _countryList .where( (Country item) => - item.name.toLowerCase().contains( - query!, - ) || - item.countryCode.toLowerCase().contains( - query, - ), + removeDiacritics( + item.name.toLowerCase()) + .contains( + query!, + ) || + removeDiacritics( + item.countryCode.toLowerCase()) + .contains( + query, + ), ) .toList(growable: false); }, diff --git a/packages/smooth_app/lib/widgets/smooth_text.dart b/packages/smooth_app/lib/widgets/smooth_text.dart index 41591e5ec3d..7d41bb1453f 100644 --- a/packages/smooth_app/lib/widgets/smooth_text.dart +++ b/packages/smooth_app/lib/widgets/smooth_text.dart @@ -1,3 +1,4 @@ +import 'package:diacritic/diacritic.dart'; import 'package:flutter/material.dart'; /// An extension on [TextStyle] that allows to have "well spaced" variant @@ -86,8 +87,8 @@ class TextHighlighter extends StatelessWidget { required TextStyle? highlightedStyle, }) { final Iterable highlightedParts = - RegExp(filter.toLowerCase().trim()).allMatches( - text.toLowerCase(), + RegExp(removeDiacritics(filter).toLowerCase().trim()).allMatches( + removeDiacritics(text).toLowerCase(), ); final List<(String, TextStyle?)> parts = <(String, TextStyle?)>[];