PQText.regexRich constructor

PQText.regexRich(
  1. String text,
  2. RegExp regExp,
  3. TextStyle regExpStyle, {
  4. Key? key,
  5. TextDecoration? decoration,
  6. TextAlign textAlign = TextAlign.start,
  7. int? maxLines,
  8. Color? color = const Color(0xff000000),
  9. double fontSize = 17,
  10. String? fontFamily,
  11. FontWeight? fontWeight = FontWeight.normal,
  12. double? wordSpacing,
  13. double? letterSpacing,
  14. StrutStyle? strutStyle,
  15. TextOverflow? overflow = TextOverflow.ellipsis,
  16. double? textScaleFactor = 1,
  17. double? lineHeight,
})

通过正则去匹配文字,修改匹配成功的文本样式

Implementation

PQText.regexRich(
  String text,
  RegExp regExp,
  TextStyle regExpStyle, {
  Key? key,
  this.decoration,
  this.textAlign = TextAlign.start,
  this.maxLines,
  this.color = const Color(0xff000000),
  this.fontSize = 17,
  this.fontFamily,
  this.fontWeight = FontWeight.normal,
  this.wordSpacing,
  this.letterSpacing,
  this.strutStyle,
  this.overflow = TextOverflow.ellipsis,
  this.textScaleFactor = 1,
  double? lineHeight,
}) : super(key: key) {
  if (lineHeight != null) {
    height = lineHeight / fontSize;
  }
  title = '';

  final list = YHMatch.regex(regExp, text);
  children = [];
  if (list.isEmpty) {
    title = text;
  }
  list.forEach((element) {
    final start = element.highlight ? element.start + 1 : element.start;
    final end = element.highlight ? element.end - 1 : element.end;
    children!.add(TextSpan(
      text: text.substring(start, end),
      style: element.highlight ? regExpStyle : null,
    ));
  });
}