Skip to content

Commit

Permalink
Filterable text: ensure to remove accents when comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
g123k committed Jul 28, 2023
1 parent a08a2f9 commit 6edb869
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
Expand Down
19 changes: 12 additions & 7 deletions packages/smooth_app/lib/pages/onboarding/country_selector.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -95,19 +96,23 @@ class _CountrySelectorState extends State<CountrySelector> {
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);
},
Expand Down
5 changes: 3 additions & 2 deletions packages/smooth_app/lib/widgets/smooth_text.dart
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -86,8 +87,8 @@ class TextHighlighter extends StatelessWidget {
required TextStyle? highlightedStyle,
}) {
final Iterable<RegExpMatch> highlightedParts =
RegExp(filter.toLowerCase().trim()).allMatches(
text.toLowerCase(),
RegExp(removeDiacritics(filter).toLowerCase().trim()).allMatches(
removeDiacritics(text).toLowerCase(),
);

final List<(String, TextStyle?)> parts = <(String, TextStyle?)>[];
Expand Down

0 comments on commit 6edb869

Please sign in to comment.