From 6433c57a589c38b2dadb2a58f82b59c99a950f68 Mon Sep 17 00:00:00 2001 From: duongnh Date: Tue, 28 Mar 2023 10:08:51 +0700 Subject: [PATCH 1/3] Removes accents and diacritics from the given String. --- lib/substring_highlight.dart | 9 +++++++-- pubspec.yaml | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/substring_highlight.dart b/lib/substring_highlight.dart index c7d50ab..22e18a5 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(); @@ -67,7 +68,9 @@ class SubstringHighlight extends StatelessWidget { @override Widget build(BuildContext context) { - final String textLC = caseSensitive ? text : text.toLowerCase(); + final String textLC = caseSensitive + ? removeDiacritics(text) + : removeDiacritics(text.toLowerCase()); // corner case: if both term and terms array are passed then combine final List termList = [term ?? '', ...(terms ?? [])]; @@ -75,7 +78,9 @@ class SubstringHighlight extends StatelessWidget { // remove empty search terms ('') because they cause infinite loops final List termListLC = termList .where((s) => s.isNotEmpty) - .map((s) => caseSensitive ? s : s.toLowerCase()) + .map((s) => caseSensitive + ? removeDiacritics(s) + : removeDiacritics(s.toLowerCase())) .toList(); List children = []; diff --git a/pubspec.yaml b/pubspec.yaml index b3c0d76..e952c7b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,7 +9,7 @@ environment: dependencies: flutter: sdk: flutter - + diacritic: ^0.1.3 dev_dependencies: flutter_test: sdk: flutter From 629a07fdbdf8cbd8b97dd1c645e3d3a4bf796bf6 Mon Sep 17 00:00:00 2001 From: duongnh Date: Tue, 28 Mar 2023 10:28:41 +0700 Subject: [PATCH 2/3] Add option to ignore diacritics --- lib/substring_highlight.dart | 57 ++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/lib/substring_highlight.dart b/lib/substring_highlight.dart index 22e18a5..fb8543b 100644 --- a/lib/substring_highlight.dart +++ b/lib/substring_highlight.dart @@ -7,30 +7,34 @@ final int __int64MaxValue = double.maxFinite.toInt(); /// Widget that renders a string with sub-string highlighting. class SubstringHighlight extends StatelessWidget { - SubstringHighlight( - {this.caseSensitive = false, - this.maxLines, - this.overflow = TextOverflow.clip, - this.term, - this.terms, - required this.text, - this.textAlign = TextAlign.left, - this.textStyle = const TextStyle( - color: Colors.black, - ), - this.textStyleHighlight = const TextStyle( - color: Colors.red, - ), - this.wordDelimiters = ' .,;?!<>[]~`@#\$%^&*()+-=|\/_', - this.words = - false // default is to match substrings (hence the package name!) - - }) - : assert(term != null || terms != null); + SubstringHighlight({ + this.caseSensitive = false, + this.ignoreDiacritics = true, + this.maxLines, + this.overflow = TextOverflow.clip, + this.term, + this.terms, + required this.text, + this.textAlign = TextAlign.left, + this.textStyle = const TextStyle( + color: Colors.black, + ), + this.textStyleHighlight = const TextStyle( + color: Colors.red, + ), + this.wordDelimiters = ' .,;?!<>[]~`@#\$%^&*()+-=|\/_', + this.words = + false, // default is to match substrings (hence the package name!) + }) : assert(term != null || terms != null); /// By default the search terms are case insensitive. Pass false to force case sensitive matches. final bool caseSensitive; + /// By default, diacritics are not ignored, which means "anh" will not match "ảnh, ánh". + /// If this parameter is set to true, diacritics will be converted to their non-diacritic + /// form to determine matches + final bool ignoreDiacritics; + /// How visual overflow should be handled. final TextOverflow overflow; @@ -68,9 +72,11 @@ class SubstringHighlight extends StatelessWidget { @override Widget build(BuildContext context) { - final String textLC = caseSensitive - ? removeDiacritics(text) - : removeDiacritics(text.toLowerCase()); + final String textDiacritics = + ignoreDiacritics ? removeDiacritics(text) : text; + + final String textLC = + caseSensitive ? textDiacritics : textDiacritics.toLowerCase(); // corner case: if both term and terms array are passed then combine final List termList = [term ?? '', ...(terms ?? [])]; @@ -78,9 +84,8 @@ class SubstringHighlight extends StatelessWidget { // remove empty search terms ('') because they cause infinite loops final List termListLC = termList .where((s) => s.isNotEmpty) - .map((s) => caseSensitive - ? removeDiacritics(s) - : removeDiacritics(s.toLowerCase())) + .map((s) => ignoreDiacritics ? removeDiacritics(s) : s) + .map((s) => caseSensitive ? s : s.toLowerCase()) .toList(); List children = []; From 8ecc11286f5135114b0fb6d2f3fd5fa8034f0b9a Mon Sep 17 00:00:00 2001 From: huuduong99 <57423977+huuduong99@users.noreply.github.com> Date: Thu, 26 Oct 2023 11:16:51 +0700 Subject: [PATCH 3/3] Update substring_highlight.dart Fix getRange index failure --- lib/substring_highlight.dart | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/substring_highlight.dart b/lib/substring_highlight.dart index fb8543b..110b1d2 100644 --- a/lib/substring_highlight.dart +++ b/lib/substring_highlight.dart @@ -72,11 +72,8 @@ class SubstringHighlight extends StatelessWidget { @override Widget build(BuildContext context) { - final String textDiacritics = - ignoreDiacritics ? removeDiacritics(text) : text; - final String textLC = - caseSensitive ? textDiacritics : textDiacritics.toLowerCase(); + caseSensitive ? text : text.toLowerCase(); // corner case: if both term and terms array are passed then combine final List termList = [term ?? '', ...(terms ?? [])];