buildTextSpan function

TextSpan buildTextSpan(
  1. List<LinkifyElement> elements, {
  2. TextStyle? style,
  3. TextStyle? linkStyle,
  4. TextStyle? tagUserLinkStyle,
  5. String? userTag,
  6. LinkCallback? onOpen,
  7. dynamic onOpenUser()?,
  8. dynamic onOpenHashtag(
    1. String
    )?,
  9. bool useMouseRegion = false,
})

Raw TextSpan builder for more control on the RichText

Implementation

TextSpan buildTextSpan(
    List<LinkifyElement> elements, {
      TextStyle? style,
      TextStyle? linkStyle,
      TextStyle? tagUserLinkStyle,
      String? userTag,
      LinkCallback? onOpen,
      Function()? onOpenUser,
      Function(String)? onOpenHashtag,
      bool useMouseRegion = false,
    }) {

  // print("atta ${elements}");
  //
  // List b = [];
  // elements.forEach((ele) {
  //   List a = ele.text.split(" ");
  //   a.forEach((textnya) {
  //     print("texx${textnya} => ${textnya.toString().contains("#")}");
  //     if (textnya.toString().contains("#")){
  //
  //     }
  //   });
  //
  // });




  return TextSpan(
    children:
    [
      TextSpan(
        text: userTag,
        style: tagUserLinkStyle,
        recognizer: userTag != null ? (TapGestureRecognizer()..onTap = onOpenUser) : null,
        children:  elements.map<InlineSpan>(
              (element) {
                // print("aa ==> ${element}");
                // if (element.text.contains("#")){
                //   List a = element.text.split(" ");
                //   a.forEach((elementtext) {
                //     return checkHastag(elementtext, style);
                //   });
                // }else{
                if (element is HashElement){
                  return TextSpan(
                    text: element.text+ " ",
                    recognizer: TapGestureRecognizer()..onTap = () {
                      onOpenHashtag!(element.text);
                    },
                    style: TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.bold
                    ),
                  );
                }else{
                  if (element is LinkableElement) {
                    if (useMouseRegion) {
                      if (userTag != null){
                        if (userTag.contains(element.text)){
                          return LinkableSpan(
                            mouseCursor: SystemMouseCursors.click,
                            inlineSpan: TextSpan(
                              text: element.text,
                              style: tagUserLinkStyle,
                              recognizer: onOpen != null ? (TapGestureRecognizer()..onTap = () => onOpen(element)) : null,
                            ),
                          );
                        }else{
                          return LinkableSpan(
                            mouseCursor: SystemMouseCursors.click,
                            inlineSpan: TextSpan(
                              text: element.text,
                              style: linkStyle,
                              recognizer: onOpen != null ? (TapGestureRecognizer()..onTap = () => onOpen(element)) : null,
                            ),
                          );
                        }
                        // if (element.text.contains("@")){
                        //   return LinkableSpan(
                        //     mouseCursor: SystemMouseCursors.click,
                        //     inlineSpan: TextSpan(
                        //       text: element.text,
                        //       style: tagUserLinkStyle,
                        //       recognizer: onOpen != null ? (TapGestureRecognizer()..onTap = () => onOpen(element)) : null,
                        //     ),
                        //   );
                        // }else{
                        //   return LinkableSpan(
                        //     mouseCursor: SystemMouseCursors.click,
                        //     inlineSpan: TextSpan(
                        //       text: element.text,
                        //       style: linkStyle,
                        //       recognizer: onOpen != null ? (TapGestureRecognizer()..onTap = () => onOpen(element)) : null,
                        //     ),
                        //   );
                        // }
                      }else{
                        return LinkableSpan(
                          mouseCursor: SystemMouseCursors.click,
                          inlineSpan: TextSpan(
                            text: element.text,
                            style: linkStyle,
                            recognizer: onOpen != null ? (TapGestureRecognizer()..onTap = () => onOpen(element)) : null,
                          ),
                        );
                      }

                    } else {
                      return TextSpan(
                        text: element.text,
                        style: linkStyle,
                        recognizer: onOpen != null ? (TapGestureRecognizer()..onTap = () => onOpen(element)) : null,
                      );
                    }
                  } else {
                    return TextSpan(
                      text: element.text,
                      style: style,
                    );
                  }
                }
          },
        ).toList()
      )

    ]



    ,
  );
}