improvements and fixes
parent
c78c2dbf98
commit
8cbc0b32e2
@ -1,135 +1,229 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:tangheem/classes/colors.dart';
|
||||
import 'package:tangheem/models/aya_tangheem_type_mapped.dart';
|
||||
|
||||
class TextHighLightWidget extends StatelessWidget {
|
||||
class TextHighLightLengthWidget extends StatelessWidget {
|
||||
final String text;
|
||||
final String valueText;
|
||||
final Color valueColor;
|
||||
final List<String> highlights;
|
||||
final TextStyle style;
|
||||
final TextAlign textAlign;
|
||||
int startIndex;
|
||||
int endIndex;
|
||||
final String highlightAyaNos;
|
||||
final String highlightAya;
|
||||
final List<AyahTextList> ayahTextList;
|
||||
final Color highLightColor;
|
||||
final double highLightFontSize;
|
||||
final Function(String) onTap;
|
||||
final Function(String) onAyaTap;
|
||||
|
||||
TextHighLightWidget(
|
||||
{Key key,
|
||||
this.text,
|
||||
this.textAlign = TextAlign.center,
|
||||
this.valueText,
|
||||
this.valueColor,
|
||||
this.highlights,
|
||||
this.highLightColor = ColorConsts.secondaryOrange,
|
||||
this.style = const TextStyle(),
|
||||
this.highLightFontSize = 18,
|
||||
this.onTap,
|
||||
this.onAyaTap});
|
||||
TextHighLightLengthWidget({
|
||||
Key key,
|
||||
this.text,
|
||||
this.textAlign = TextAlign.center,
|
||||
this.startIndex,
|
||||
this.endIndex,
|
||||
this.highlightAyaNos = "",
|
||||
this.highlightAya = "",
|
||||
this.ayahTextList,
|
||||
this.highLightColor = ColorConsts.secondaryOrange,
|
||||
}) : super(key: key);
|
||||
|
||||
TextStyle textStyle = TextStyle(
|
||||
fontFamily: "UthmanicHafs",
|
||||
fontSize: 16,
|
||||
color: ColorConsts.primaryBlue,
|
||||
fontWeight: FontWeight.bold,
|
||||
);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (text == '') {
|
||||
return _richText(_normalSpan(text));
|
||||
}
|
||||
if (highlights.isEmpty) {
|
||||
return _richText(_normalSpan(text));
|
||||
}
|
||||
for (int i = 0; i < highlights.length; i++) {
|
||||
if (highlights[i] == null) {
|
||||
assert(highlights[i] != null);
|
||||
return _richText(_normalSpan(text));
|
||||
}
|
||||
if (highlights[i].isEmpty) {
|
||||
assert(highlights[i].isNotEmpty);
|
||||
return _richText(_normalSpan(text));
|
||||
}
|
||||
}
|
||||
|
||||
List<TextSpan> _spans = [];
|
||||
int _start = 0;
|
||||
List<String> _lowerCaseHighlights = [];
|
||||
|
||||
highlights.forEach((element) {
|
||||
_lowerCaseHighlights.add(element.toLowerCase());
|
||||
});
|
||||
// if (startIndex > endIndex) {
|
||||
// endIndex = endIndex + startIndex;
|
||||
// }
|
||||
|
||||
while (true) {
|
||||
Map<int, String> _highlightsMap = Map();
|
||||
int actualStartIndex = 0;
|
||||
if (highlightAyaNos.isNotEmpty)
|
||||
for (int i = 0; i < ayahTextList.length; i++) {
|
||||
var eachAyah = ayahTextList[i];
|
||||
|
||||
for (int i = 0; i < highlights.length; i++) {
|
||||
int _index = text.toLowerCase().indexOf(_lowerCaseHighlights[i], _start);
|
||||
if (_index >= 0) {
|
||||
_highlightsMap.putIfAbsent(_index, () => highlights[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (_highlightsMap.isNotEmpty) {
|
||||
List<int> _indexes = [];
|
||||
_highlightsMap.forEach((key, value) => _indexes.add(key));
|
||||
|
||||
int _currentIndex = _indexes.reduce(min);
|
||||
String _currentHighlight = text.substring(_currentIndex, _currentIndex + _highlightsMap[_currentIndex].length);
|
||||
|
||||
if (_currentIndex == _start) {
|
||||
_spans.add(_highlightSpan(_currentHighlight));
|
||||
_start += _currentHighlight.length;
|
||||
var abList = highlightAyaNos.split(",").toList();
|
||||
if (!(abList.any((e) => e == eachAyah.ayahNo.toString()))) {
|
||||
//print("e:$e");
|
||||
//print("eachAyah.ayahNo:${eachAyah.ayahNo}");
|
||||
actualStartIndex += eachAyah.ayahText.length;
|
||||
//print("actualStartIndex:$actualStartIndex");
|
||||
} else {
|
||||
_spans.add(_normalSpan(text.substring(_start, _currentIndex)));
|
||||
_spans.add(_highlightSpan(_currentHighlight));
|
||||
_start = _currentIndex + _currentHighlight.length;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
_spans.add(_normalSpan(text.substring(_start, text.length)));
|
||||
break;
|
||||
}
|
||||
// print("startIndex:$startIndex");
|
||||
if (actualStartIndex != 0) {
|
||||
startIndex = actualStartIndex + startIndex;
|
||||
}
|
||||
endIndex = (highlightAya.length) + startIndex;
|
||||
// print("startIndexAf:$startIndex");
|
||||
String beforeText = text.substring(0, startIndex);
|
||||
//print("beforeText:$beforeText");
|
||||
// print("endIndex:$endIndex");
|
||||
//print("text:${text.length}");
|
||||
String highLightText = text.substring(startIndex, endIndex);
|
||||
// print("highLightText:$highLightText");
|
||||
// print("highLightText:${highLightText.length}");
|
||||
|
||||
String afterText = text.substring(endIndex, text.length);
|
||||
// print("afterText:$afterText");
|
||||
_spans.add(_normalText(beforeText));
|
||||
_spans.add(_highlightText(highLightText));
|
||||
_spans.add(_normalText(afterText));
|
||||
|
||||
if (valueText != null) {
|
||||
_spans.add(TextSpan(
|
||||
text: ' ' + valueText,
|
||||
style: TextStyle(color: valueColor ?? Color(0xff170026), fontSize: 12, fontWeight: FontWeight.w800),
|
||||
));
|
||||
}
|
||||
return _richText(TextSpan(children: _spans));
|
||||
}
|
||||
|
||||
TextSpan _highlightSpan(String value) {
|
||||
if (style.color == null) {
|
||||
return TextSpan(
|
||||
text: value,
|
||||
style: style.copyWith(color: highLightColor, fontSize: highLightFontSize),
|
||||
recognizer: TapGestureRecognizer()
|
||||
..onTap = () {
|
||||
if (onTap != null) {
|
||||
onTap(value);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return TextSpan(text: value, style: style.copyWith(color: valueColor));
|
||||
}
|
||||
TextSpan _normalText(String text) {
|
||||
return TextSpan(
|
||||
text: text,
|
||||
style: textStyle,
|
||||
);
|
||||
}
|
||||
|
||||
TextSpan _normalSpan(String value) {
|
||||
if (style.color == null) {
|
||||
return TextSpan(
|
||||
text: value,
|
||||
style: style.copyWith(color: valueColor),
|
||||
);
|
||||
} else {
|
||||
return TextSpan(text: value, style: style, children: [
|
||||
if (valueText != null)
|
||||
TextSpan(
|
||||
text: ' ' + valueText,
|
||||
style: style,
|
||||
)
|
||||
]);
|
||||
}
|
||||
TextSpan _highlightText(String text) {
|
||||
return TextSpan(
|
||||
text: text,
|
||||
style: textStyle.copyWith(color: highLightColor),
|
||||
);
|
||||
}
|
||||
|
||||
RichText _richText(TextSpan text) {
|
||||
return RichText(key: key, text: text, textAlign: textAlign);
|
||||
}
|
||||
}
|
||||
|
||||
// class TextHighLightWidget extends StatelessWidget {
|
||||
// final String text;
|
||||
// final String valueText;
|
||||
// final Color valueColor;
|
||||
// final List<String> highlights;
|
||||
// final TextStyle style;
|
||||
// final TextAlign textAlign;
|
||||
// final Color highLightColor;
|
||||
// final double highLightFontSize;
|
||||
// final Function(String) onTap;
|
||||
// final Function(String) onAyaTap;
|
||||
//
|
||||
// TextHighLightWidget(
|
||||
// {Key key,
|
||||
// this.text,
|
||||
// this.textAlign = TextAlign.center,
|
||||
// this.valueText,
|
||||
// this.valueColor,
|
||||
// this.highlights,
|
||||
// this.highLightColor = ColorConsts.secondaryOrange,
|
||||
// this.style = const TextStyle(),
|
||||
// this.highLightFontSize = 18,
|
||||
// this.onTap,
|
||||
// this.onAyaTap});
|
||||
//
|
||||
// @override
|
||||
// Widget build(BuildContext context) {
|
||||
// if (text == '') {
|
||||
// return _richText(_normalSpan(text));
|
||||
// }
|
||||
// if (highlights.isEmpty) {
|
||||
// return _richText(_normalSpan(text));
|
||||
// }
|
||||
// for (int i = 0; i < highlights.length; i++) {
|
||||
// if (highlights[i] == null) {
|
||||
// assert(highlights[i] != null);
|
||||
// return _richText(_normalSpan(text));
|
||||
// }
|
||||
// if (highlights[i].isEmpty) {
|
||||
// assert(highlights[i].isNotEmpty);
|
||||
// return _richText(_normalSpan(text));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// List<TextSpan> _spans = [];
|
||||
// int _start = 0;
|
||||
// List<String> _lowerCaseHighlights = [];
|
||||
//
|
||||
// highlights.forEach((element) {
|
||||
// _lowerCaseHighlights.add(element.toLowerCase());
|
||||
// });
|
||||
//
|
||||
// while (true) {
|
||||
// Map<int, String> _highlightsMap = Map();
|
||||
//
|
||||
// for (int i = 0; i < highlights.length; i++) {
|
||||
// int _index = text.toLowerCase().indexOf(_lowerCaseHighlights[i], _start);
|
||||
// if (_index >= 0) {
|
||||
// _highlightsMap.putIfAbsent(_index, () => highlights[i]);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (_highlightsMap.isNotEmpty) {
|
||||
// List<int> _indexes = [];
|
||||
// _highlightsMap.forEach((key, value) => _indexes.add(key));
|
||||
//
|
||||
// int _currentIndex = _indexes.reduce(min);
|
||||
// String _currentHighlight = text.substring(_currentIndex, _currentIndex + _highlightsMap[_currentIndex].length);
|
||||
//
|
||||
// if (_currentIndex == _start) {
|
||||
// _spans.add(_highlightSpan(_currentHighlight));
|
||||
// _start += _currentHighlight.length;
|
||||
// } else {
|
||||
// _spans.add(_normalSpan(text.substring(_start, _currentIndex)));
|
||||
// _spans.add(_highlightSpan(_currentHighlight));
|
||||
// _start = _currentIndex + _currentHighlight.length;
|
||||
// }
|
||||
// } else {
|
||||
// _spans.add(_normalSpan(text.substring(_start, text.length)));
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (valueText != null) {
|
||||
// _spans.add(TextSpan(
|
||||
// text: ' ' + valueText,
|
||||
// style: TextStyle(color: valueColor ?? Color(0xff170026), fontSize: 12, fontWeight: FontWeight.w800),
|
||||
// ));
|
||||
// }
|
||||
// return _richText(TextSpan(children: _spans));
|
||||
// }
|
||||
//
|
||||
// TextSpan _highlightSpan(String value) {
|
||||
// if (style.color == null) {
|
||||
// return TextSpan(
|
||||
// text: value,
|
||||
// style: style.copyWith(color: highLightColor, fontSize: highLightFontSize),
|
||||
// recognizer: TapGestureRecognizer()
|
||||
// ..onTap = () {
|
||||
// if (onTap != null) {
|
||||
// onTap(value);
|
||||
// }
|
||||
// });
|
||||
// } else {
|
||||
// return TextSpan(text: value, style: style.copyWith(color: valueColor));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// TextSpan _normalSpan(String value) {
|
||||
// if (style.color == null) {
|
||||
// return TextSpan(
|
||||
// text: value,
|
||||
// style: style.copyWith(color: valueColor),
|
||||
// );
|
||||
// } else {
|
||||
// return TextSpan(text: value, style: style, children: [
|
||||
// if (valueText != null)
|
||||
// TextSpan(
|
||||
// text: ' ' + valueText,
|
||||
// style: style,
|
||||
// )
|
||||
// ]);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// RichText _richText(TextSpan text) {
|
||||
// return RichText(key: key, text: text, textAlign: textAlign);
|
||||
// }
|
||||
// }
|
||||
|
||||
Loading…
Reference in New Issue