Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to ignore diacritics #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions lib/substring_highlight.dart
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -67,14 +72,16 @@ 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<String> termList = [term ?? '', ...(terms ?? [])];

// remove empty search terms ('') because they cause infinite loops
final List<String> termListLC = termList
.where((s) => s.isNotEmpty)
.map((s) => ignoreDiacritics ? removeDiacritics(s) : s)
.map((s) => caseSensitive ? s : s.toLowerCase())
.toList();

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
dependencies:
flutter:
sdk: flutter

diacritic: ^0.1.3
dev_dependencies:
flutter_test:
sdk: flutter
Expand Down