From 933e09bbb3b7bf8ae4e4ea1ede7c9c9382c7fa11 Mon Sep 17 00:00:00 2001 From: Octavian Date: Fri, 27 Nov 2020 02:19:44 +0200 Subject: [PATCH] Added accent sensitive feature --- example/pubspec.lock | 101 ++++++++++------------------- lib/dynamic_text_highlighting.dart | 56 +++++++++++++++- pubspec.lock | 97 +++++++++------------------ 3 files changed, 118 insertions(+), 136 deletions(-) diff --git a/example/pubspec.lock b/example/pubspec.lock index 1534276..0fa67f3 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -1,62 +1,48 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: - archive: - dependency: transitive - description: - name: archive - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.13" - args: - dependency: transitive - description: - name: args - url: "https://pub.dartlang.org" - source: hosted - version: "1.6.0" async: dependency: transitive description: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.4.1" + version: "2.5.0-nullsafety.1" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" - charcode: + version: "2.1.0-nullsafety.1" + characters: dependency: transitive description: - name: charcode + name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.3" - collection: + version: "1.1.0-nullsafety.3" + charcode: dependency: transitive description: - name: collection + name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.14.12" - convert: + version: "1.2.0-nullsafety.1" + clock: dependency: transitive description: - name: convert + name: clock url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" - crypto: + version: "1.1.0-nullsafety.1" + collection: dependency: transitive description: - name: crypto + name: collection url: "https://pub.dartlang.org" source: hosted - version: "2.1.4" + version: "1.15.0-nullsafety.3" dynamic_text_highlighting: dependency: "direct dev" description: @@ -64,6 +50,13 @@ packages: relative: true source: path version: "2.2.0" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0-nullsafety.1" flutter: dependency: "direct main" description: flutter @@ -74,48 +67,27 @@ packages: description: flutter source: sdk version: "0.0.0" - image: - dependency: transitive - description: - name: image - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.12" matcher: dependency: transitive description: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.6" + version: "0.12.10-nullsafety.1" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.8" + version: "1.3.0-nullsafety.3" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.6.4" - petitparser: - dependency: transitive - description: - name: petitparser - url: "https://pub.dartlang.org" - source: hosted - version: "2.4.0" - quiver: - dependency: transitive - description: - name: quiver - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.3" + version: "1.8.0-nullsafety.1" sky_engine: dependency: transitive description: flutter @@ -127,62 +99,55 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0-nullsafety.2" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0-nullsafety.1" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.5" + version: "1.1.0-nullsafety.1" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0-nullsafety.1" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.15" + version: "0.2.19-nullsafety.2" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0-nullsafety.3" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" - xml: - dependency: transitive - description: - name: xml - url: "https://pub.dartlang.org" - source: hosted - version: "3.6.1" + version: "2.1.0-nullsafety.3" sdks: - dart: ">=2.7.0 <3.0.0" + dart: ">=2.10.0-110 <2.11.0" diff --git a/lib/dynamic_text_highlighting.dart b/lib/dynamic_text_highlighting.dart index 4e80ee5..9073952 100644 --- a/lib/dynamic_text_highlighting.dart +++ b/lib/dynamic_text_highlighting.dart @@ -10,6 +10,7 @@ class DynamicTextHighlighting extends StatelessWidget { final Color color; final TextStyle style; final bool caseSensitive; + final bool accentSensitive; //RichText final TextAlign textAlign; @@ -33,6 +34,7 @@ class DynamicTextHighlighting extends StatelessWidget { color: Colors.black, ), this.caseSensitive = true, + this.accentSensitive = true, //RichText this.textAlign = TextAlign.start, @@ -50,6 +52,7 @@ class DynamicTextHighlighting extends StatelessWidget { assert(color != null), assert(style != null), assert(caseSensitive != null), + assert(accentSensitive != null), assert(textAlign != null), assert(softWrap != null), assert(overflow != null), @@ -78,6 +81,40 @@ class DynamicTextHighlighting extends StatelessWidget { } } + // All the accents with all the variations + // First character of each element is the main ascii character + List accents = [ + 'aàáâãăäåą', + "AÀÁÂÃĂÄÅĄ", + 'cç', + 'CÇ', + 'eèéêëèěęėĕē', + 'EÈÉÊËÈĚĘĖĔĒ', + 'gğġģĝ', + 'GĞĠĢĜ', + 'iìíîïĩīĭį', + 'IÌÍÎÏĨĪĬĮ', + 'lĺļľŀł', + 'LĹĻĽĿŁ', + 'nñńņňŋ', + 'NÑŃŅŇŊ', + 'oōŏőòóôõö', + 'OŌŎŐÒÓÔÕÖ', + 'rŕŗř', + 'RŔŖŘ', + 'sśşš', + 'SŚŞŠ', + 'tţťŧ', + 'TŢŤŦ', + 'uùúûüũūŭůűų', + 'UÙÚÛÜŨŪŬŮŰŲ', + ]; + // Map with all accents + Map accentsMap = Map.fromEntries(accents + .map((accents) => accents.split('').map((e) => MapEntry(e, accents[0]))) + .toList() + .expand((i) => i)); + //Main code List _spans = List(); int _start = 0; @@ -95,14 +132,16 @@ class DynamicTextHighlighting extends StatelessWidget { if (caseSensitive) { for (int i = 0; i < highlights.length; i++) { - int _index = text.indexOf(highlights[i], _start); + int _index = accentSensitiveText(text, accentsMap) + .indexOf(accentSensitiveText(highlights[i], accentsMap), _start); if (_index >= 0) { _highlightsMap.putIfAbsent(_index, () => highlights[i]); } } } else { for (int i = 0; i < highlights.length; i++) { - int _index = _lowerCaseText.indexOf(_lowerCaseHighlights[i], _start); + int _index = accentSensitiveText(_lowerCaseText, accentsMap).indexOf( + accentSensitiveText(_lowerCaseHighlights[i], accentsMap), _start); if (_index >= 0) { _highlightsMap.putIfAbsent(_index, () => highlights[i]); } @@ -185,4 +224,17 @@ class DynamicTextHighlighting extends StatelessWidget { textHeightBehavior: textHeightBehavior, ); } + + String removeAccents(String text, Map accentsMap) { + return text + .split('') + .map((e) => accentsMap.containsKey(e) ? accentsMap[e] : e) + .toList() + .fold('', (previousValue, element) => previousValue + element); + } + + String accentSensitiveText(text, Map accentsMap) { + if (!accentSensitive) return removeAccents(text, accentsMap); + return text; + } } diff --git a/pubspec.lock b/pubspec.lock index 3107c6e..b308ac3 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,62 +1,55 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: - archive: - dependency: transitive - description: - name: archive - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.13" - args: - dependency: transitive - description: - name: args - url: "https://pub.dartlang.org" - source: hosted - version: "1.6.0" async: dependency: transitive description: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.4.1" + version: "2.5.0-nullsafety.1" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0-nullsafety.3" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.1.3" - collection: + version: "1.2.0-nullsafety.1" + clock: dependency: transitive description: - name: collection + name: clock url: "https://pub.dartlang.org" source: hosted - version: "1.14.12" - convert: + version: "1.1.0-nullsafety.1" + collection: dependency: transitive description: - name: convert + name: collection url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" - crypto: + version: "1.15.0-nullsafety.3" + fake_async: dependency: transitive description: - name: crypto + name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "2.1.4" + version: "1.2.0-nullsafety.1" flutter: dependency: "direct main" description: flutter @@ -67,48 +60,27 @@ packages: description: flutter source: sdk version: "0.0.0" - image: - dependency: transitive - description: - name: image - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.12" matcher: dependency: transitive description: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.6" + version: "0.12.10-nullsafety.1" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.8" + version: "1.3.0-nullsafety.3" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.6.4" - petitparser: - dependency: transitive - description: - name: petitparser - url: "https://pub.dartlang.org" - source: hosted - version: "2.4.0" - quiver: - dependency: transitive - description: - name: quiver - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.3" + version: "1.8.0-nullsafety.1" sky_engine: dependency: transitive description: flutter @@ -120,62 +92,55 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0-nullsafety.2" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0-nullsafety.1" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.5" + version: "1.1.0-nullsafety.1" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0-nullsafety.1" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.15" + version: "0.2.19-nullsafety.2" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0-nullsafety.3" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" - xml: - dependency: transitive - description: - name: xml - url: "https://pub.dartlang.org" - source: hosted - version: "3.6.1" + version: "2.1.0-nullsafety.3" sdks: - dart: ">=2.7.0 <3.0.0" + dart: ">=2.10.0-110 <2.11.0"