From 406f82cc831a0bf60d0340f6d06f92c9a52781a0 Mon Sep 17 00:00:00 2001 From: carrizomiguel Date: Wed, 8 Nov 2023 11:40:07 -0500 Subject: [PATCH] feat: :sparkles: ignore diacritics --- lib/substring_highlight.dart | 48 ++++++++++++++++++++---------------- pubspec.yaml | 1 + 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/lib/substring_highlight.dart b/lib/substring_highlight.dart index c7d50ab..103e557 100644 --- a/lib/substring_highlight.dart +++ b/lib/substring_highlight.dart @@ -1,31 +1,31 @@ library substring_highlight; +import 'package:diacritic/diacritic.dart'; import 'package:flutter/material.dart'; 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.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!) + this.ignoreDiacritics = false, + }) : assert(term != null || terms != null); /// By default the search terms are case insensitive. Pass false to force case sensitive matches. final bool caseSensitive; @@ -65,9 +65,14 @@ class SubstringHighlight extends StatelessWidget { /// If true then match complete words only (instead of characters or substrings within words). This feature is in ALPHA... use 'words' AT YOUR OWN RISK!!! final bool words; + /// `false` by default. If set true then texts that contains diacritics, for example: 'é', 'è' or 'ê', will match the terms. + final bool ignoreDiacritics; + @override Widget build(BuildContext context) { - final String textLC = caseSensitive ? text : text.toLowerCase(); + final textNormalized = ignoreDiacritics ? removeDiacritics(text) : text; + final String textLC = + caseSensitive ? textNormalized : textNormalized.toLowerCase(); // corner case: if both term and terms array are passed then combine final List termList = [term ?? '', ...(terms ?? [])]; @@ -76,6 +81,7 @@ class SubstringHighlight extends StatelessWidget { final List termListLC = termList .where((s) => s.isNotEmpty) .map((s) => caseSensitive ? s : s.toLowerCase()) + .map((s) => ignoreDiacritics ? removeDiacritics(s) : s) .toList(); List children = []; diff --git a/pubspec.yaml b/pubspec.yaml index b3c0d76..64109c6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,6 +7,7 @@ environment: sdk: ">=2.12.0 <3.0.0" dependencies: + diacritic: ^0.1.4 flutter: sdk: flutter