import 'package:flutter/material.dart'; import 'package:tangheem/classes/colors.dart'; import 'package:tangheem/models/aya_tangheem_type_mapped.dart'; class TextHighLightLengthWidget extends StatelessWidget { final String text; final TextAlign textAlign; int startIndex; int endIndex; final String highlightAyaNos; final String highlightAya; final List ayahTextList; final double fontSize; final Color highLightColor; TextHighLightLengthWidget({ Key key, this.text, this.textAlign = TextAlign.center, this.startIndex, this.endIndex, this.highlightAyaNos = "", this.highlightAya = "", this.ayahTextList, this.fontSize = 18, this.highLightColor = ColorConsts.secondaryOrange, }) : super(key: key); TextStyle textStyle = TextStyle( fontFamily: "UthmanicHafs", fontSize: 18, color: ColorConsts.primaryBlue, fontWeight: FontWeight.bold, ); @override Widget build(BuildContext context) { List _spans = []; // if (startIndex > endIndex) { // endIndex = endIndex + startIndex; // } int actualStartIndex = 0; if (highlightAyaNos.isNotEmpty) for (int i = 0; i < ayahTextList.length; i++) { var eachAyah = ayahTextList[i]; 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 { break; } } // print("startIndex:$startIndex"); if (startIndex < 0) { startIndex = 0; } 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)); return _richText(TextSpan(children: _spans)); } TextSpan _normalText(String text) { return TextSpan( text: text, style: textStyle.copyWith(fontSize: fontSize), ); } TextSpan _highlightText(String text) { return TextSpan( text: text, style: textStyle.copyWith(color: highLightColor, fontSize: fontSize), ); } 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 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 _spans = []; // int _start = 0; // List _lowerCaseHighlights = []; // // highlights.forEach((element) { // _lowerCaseHighlights.add(element.toLowerCase()); // }); // // while (true) { // Map _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 _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); // } // }