diff --git a/lib/substring_highlight.dart b/lib/substring_highlight.dart index c7d50ab..110b1d2 100644 --- a/lib/substring_highlight.dart +++ b/lib/substring_highlight.dart @@ -1,35 +1,40 @@ 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.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; @@ -67,7 +72,8 @@ class SubstringHighlight extends StatelessWidget { @override Widget build(BuildContext context) { - final String textLC = caseSensitive ? text : text.toLowerCase(); + final String textLC = + caseSensitive ? text : text.toLowerCase(); // corner case: if both term and terms array are passed then combine final List termList = [term ?? '', ...(terms ?? [])]; @@ -75,6 +81,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.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