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

How to customized some link pattern? #22

Open
lucasjinreal opened this issue Oct 13, 2023 · 0 comments
Open

How to customized some link pattern? #22

lucasjinreal opened this issue Oct 13, 2023 · 0 comments

Comments

@lucasjinreal
Copy link

Hi, this lib is useful.

I want customized the link pattern, is there a way to do it?

am using this logic to buld spans:

class WidgetSpanTextEditingController extends TextEditingController {
  WidgetSpanTextEditingController({String? text})
      : super.fromValue(text == null
            ? TextEditingValue.empty
            : TextEditingValue(text: text));

  @override
  TextSpan buildTextSpan(
      {required BuildContext context,
      TextStyle? style,
      required bool withComposing}) {
    TextRange? matchedRange;
    TextRange? matchedRange2;

    if (text.contains('$SPLIT_CMD_START') && text.contains('$SPLIT_CMD_END')) {
      matchedRange = _findMatchedRange(text);
      matchedRange2 = _findMatchedRange(text, trimTag: false);
    }

    if (matchedRange != null) {
      return TextSpan(
        children: [
          TextSpan(text: matchedRange.textBefore(text)),
          WidgetSpan(
              child: GestureDetector(
                  child: Padding(
                    padding: const EdgeInsets.only(
                        right: 5.0, top: 2.0, bottom: 2.0),
                    child: ClipRRect(
                        borderRadius:
                            const BorderRadius.all(Radius.circular(5.0)),
                        child: Container(
                          padding: const EdgeInsets.all(5.0),
                          color: Colors.orange,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              Text(matchedRange2!
                                      .textInside(text)
                                      .trim()
                                      .split(SPLIT_CMD_MID)[0]
                                  //style: textStyle?.copyWith(color: Colors.orange),
                                  ),
                              const SizedBox(
                                width: 5.0,
                              ),
                              InkWell(
                                child: const Icon(
                                  Icons.close,
                                  size: 15.0,
                                ),
                                onTap: () {
                                  // controller!.value = controller!.value
                                  //     .copyWith(
                                  //         text: controller!.text.replaceRange(
                                  //             start!, start! + text.length, ''),
                                  //         selection: TextSelection.fromPosition(
                                  //             TextPosition(offset: start!)));
                                  clearMatchedText(matchedRange, text);
                                },
                              )
                            ],
                          ),
                        )),
                  ),
                  onTap: () {})),
          TextSpan(text: matchedRange.textAfter(text)),
        ],
        style: style,
      );
    }

    return TextSpan(text: text, style: style);
  }

  String clearMatchedText(TextRange? range, String text) {
    if (range != null) if (range.start >= 0 && range.end <= text.length) {
      text = text.replaceRange(range.start, range.end, '');
      return text;
    }
    return text;
  }

  // TextRange _findMatchedRange(String text) {
  //   final RegExp matchPattern = RegExp(RegExp.escape('\uffff'));
  //   late TextRange matchedRange;

  //   for (final Match match in matchPattern.allMatches(text)) {
  //     matchedRange = TextRange(start: match.start, end: match.end);
  //   }

  //   return matchedRange;
  // }

  TextRange _findMatchedRange(String text, {bool trimTag = true}) {
    final RegExp matchPattern = RegExp(r'<cmd>(.*?)<\/cmd>');
    late TextRange matchedRange;

    for (final Match match in matchPattern.allMatches(text)) {
      int start = match.start;
      if (!trimTag) start += '$SPLIT_CMD_START'.length;
      int end = match.end;
      if (!trimTag) end -= '$SPLIT_CMD_END'.length;
      matchedRange = TextRange(start: start, end: end);
    }
    return matchedRange;
  }
}

but question is when deleting, it is not delete whole span, but character by character.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant