fluttercandies / extended_text

A powerful extended official text for Flutter, which supports Speical Text(Image,@somebody), Custom Background, Custom overFlow, Text Selection.
MIT License
664 stars 134 forks source link

Click on the special text without triggering the onSpecialTextTap event #106

Closed kimmy-wang closed 1 year ago

kimmy-wang commented 3 years ago

Click on the special text without triggering the onSpecialTextTap event.

ExtendedText(
              StringUtil.isBlank(titles[index])
                  ? _titles[index]
                  : titles[index]!,
              onSpecialTextTap: (dynamic parameter) {
                if (parameter.toString().startsWith('@')) {
                  print('object');
                }
              },
              specialTextSpanBuilder: GitterSpecialTextSpanBuilder(),
              overflow: TextOverflow.ellipsis,
              maxLines: 1,
            )
import 'package:extended_text/extended_text.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

class AtText extends SpecialText {
  static const String flag = '@';
  final int? start;

  final bool showAtBackground;

  AtText(TextStyle textStyle, SpecialTextGestureTapCallback? onTap,
      {this.showAtBackground = false, this.start})
      : super(
          flag,
          ' ',
          textStyle,
        );

  @override
  InlineSpan finishText() {
    TextStyle? textStyle =
        this.textStyle.copyWith(color: Colors.blue, fontSize: 16.0);

    final atText = toString();

    return showAtBackground
        ? BackgroundTextSpan(
            background: Paint()..color = Colors.blue.withOpacity(0.15),
            text: atText,
            actualText: atText,
            start: start!,

            ///caret can move into special text
            deleteAll: true,
            style: textStyle,
            recognizer: (TapGestureRecognizer()
              ..onTap = () {
                if (onTap != null) onTap!(atText);
              }),
          )
        : SpecialTextSpan(
            text: atText,
            actualText: atText,
            start: start!,
            style: textStyle,
            recognizer: (TapGestureRecognizer()
              ..onTap = () {
                if (onTap != null) onTap!(atText);
              }),
          );
  }
}

class GitterSpecialTextSpanBuilder extends SpecialTextSpanBuilder {
  GitterSpecialTextSpanBuilder({this.showAtBackground = false});

  final bool showAtBackground;
  @override
  TextSpan build(String data,
      {TextStyle? textStyle, SpecialTextGestureTapCallback? onTap}) {
    if (kIsWeb) {
      return TextSpan(text: data, style: textStyle);
    }

    return super.build(data, textStyle: textStyle, onTap: onTap);
  }

  @override
  SpecialText? createSpecialText(String flag,
      {TextStyle? textStyle,
        SpecialTextGestureTapCallback? onTap,
        int? index}) {
    if (flag == '') {
      return null;
    }

    ///index is end index of start flag, so text start index should be index-(flag.length-1)
    if (isStart(flag, AtText.flag)) {
      return AtText(
        textStyle!,
        onTap,
        start: index! - (AtText.flag.length - 1),
        showAtBackground: showAtBackground,
      );
    }
    return null;
  }
}
zmtzawqlp commented 3 years ago

which version do you use

zmtzawqlp commented 3 years ago

and please provide runable demo

kimmy-wang commented 3 years ago

repo

mdddj commented 3 years ago

I also encountered this problem

zmtzawqlp commented 1 year ago

please check it on lastest version, reopen if need.