From dab0253960747aa4eb65ee349d229f3160a57239 Mon Sep 17 00:00:00 2001 From: rdahuron Date: Tue, 23 Aug 2022 11:20:45 +0200 Subject: [PATCH] Add support to ignore diacritics --- example/pubspec.lock | 47 +++++++++++++++++++++--------------- lib/substring_highlight.dart | 12 ++++++++- pubspec.lock | 45 +++++++++++++++++++--------------- pubspec.yaml | 1 + 4 files changed, 65 insertions(+), 40 deletions(-) diff --git a/example/pubspec.lock b/example/pubspec.lock index 696a34f..4f393ac 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -7,7 +7,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.5.0" + version: "2.8.2" boolean_selector: dependency: transitive description: @@ -21,14 +21,14 @@ packages: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.1" clock: dependency: transitive description: @@ -42,14 +42,21 @@ packages: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0" + version: "1.16.0" + diacritic: + dependency: transitive + description: + name: diacritic + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.3" fake_async: dependency: transitive description: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0" flutter: dependency: "direct main" description: flutter @@ -66,21 +73,28 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.10" + version: "0.12.11" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.4" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.3.0" + version: "1.7.0" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "1.8.1" sky_engine: dependency: transitive description: flutter @@ -92,7 +106,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "1.8.2" stack_trace: dependency: transitive description: @@ -120,7 +134,7 @@ packages: path: ".." relative: true source: path - version: "1.0.27" + version: "1.0.33" term_glyph: dependency: transitive description: @@ -134,20 +148,13 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.19" - typed_data: - dependency: transitive - description: - name: typed_data - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.0" + version: "0.4.9" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.0" + version: "2.1.2" sdks: - dart: ">=2.12.0 <3.0.0" + dart: ">=2.17.0-0 <3.0.0" diff --git a/lib/substring_highlight.dart b/lib/substring_highlight.dart index c7d50ab..52bd2b5 100644 --- a/lib/substring_highlight.dart +++ b/lib/substring_highlight.dart @@ -1,5 +1,6 @@ library substring_highlight; +import 'package:diacritic/diacritic.dart'; import 'package:flutter/material.dart'; final int __int64MaxValue = double.maxFinite.toInt(); @@ -8,6 +9,7 @@ final int __int64MaxValue = double.maxFinite.toInt(); class SubstringHighlight extends StatelessWidget { SubstringHighlight( {this.caseSensitive = false, + this.ignoreDiacritics = false, this.maxLines, this.overflow = TextOverflow.clip, this.term, @@ -27,6 +29,11 @@ class SubstringHighlight extends StatelessWidget { }) : assert(term != null || terms != null); + /// By default, diacritics are not ignored, which means "e" will not match "é". + /// If this parameter is set to true, diacritics will be converted to their non-diacritic + /// form to determine matches + final bool ignoreDiacritics; + /// By default the search terms are case insensitive. Pass false to force case sensitive matches. final bool caseSensitive; @@ -67,7 +74,9 @@ class SubstringHighlight extends StatelessWidget { @override Widget build(BuildContext context) { - final String textLC = caseSensitive ? text : text.toLowerCase(); + final String textDiacritics = + ignoreDiacritics ? removeDiacritics(text) : text; + final String textLC = caseSensitive ? text : textDiacritics.toLowerCase(); // corner case: if both term and terms array are passed then combine final List termList = [term ?? '', ...(terms ?? [])]; @@ -75,6 +84,7 @@ class SubstringHighlight extends StatelessWidget { // remove empty search terms ('') because they cause infinite loops final List termListLC = termList .where((s) => s.isNotEmpty) + .map((s) => ignoreDiacritics ? removeDiacritics(s) : s) .map((s) => caseSensitive ? s : s.toLowerCase()) .toList(); diff --git a/pubspec.lock b/pubspec.lock index 49a0ce6..57cba01 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,7 +7,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.5.0" + version: "2.8.2" boolean_selector: dependency: transitive description: @@ -21,14 +21,14 @@ packages: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.1" clock: dependency: transitive description: @@ -42,14 +42,21 @@ packages: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0" + version: "1.16.0" + diacritic: + dependency: "direct main" + description: + name: diacritic + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.3" fake_async: dependency: transitive description: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0" flutter: dependency: "direct main" description: flutter @@ -66,21 +73,28 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.10" + version: "0.12.11" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.4" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.3.0" + version: "1.7.0" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "1.8.1" sky_engine: dependency: transitive description: flutter @@ -92,7 +106,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "1.8.2" stack_trace: dependency: transitive description: @@ -127,20 +141,13 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.19" - typed_data: - dependency: transitive - description: - name: typed_data - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.0" + version: "0.4.9" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.0" + version: "2.1.2" sdks: - dart: ">=2.12.0 <3.0.0" + dart: ">=2.17.0-0 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index b3c0d76..eb643a8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,6 +7,7 @@ environment: sdk: ">=2.12.0 <3.0.0" dependencies: + diacritic: ^0.1.3 flutter: sdk: flutter