logo
down
shadow

Flutter: Do I need to call GestureRecognizer.dispose in StatelessWidget (TextSpan/recognizer)?


Flutter: Do I need to call GestureRecognizer.dispose in StatelessWidget (TextSpan/recognizer)?

By : ALi Prince
Date : November 21 2020, 04:01 AM
I wish did fix the issue. You cannot do that in a StatelessWidget. You will have to convert it into a StatefulWidget and override the dispose method of State:
code :
class Foo extends StatefulWidget {
  @override
  _FooState createState() => _FooState();
}

class _FooState extends State<Foo> {
  GestureRecognizer gestureRecognizer;

  @override
  void dispose() {
    gestureRecognizer?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}


Share : facebook icon twitter icon
Flutter: Changing textstyle of TextSpan with TapGestureRecognizer

Flutter: Changing textstyle of TextSpan with TapGestureRecognizer


By : Raj
Date : March 29 2020, 07:55 AM
will help you First problem is that you have isPressed inside the _buildTextSpanWithSplittedText method, which will be overwritten on every re-paint. And if you keep that variable at a class level it will be applied to all TextSpans.
So a possible solution can be using a List, here an example:
code :
import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';

class MakeStringClickable extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MakeStringClickableState();
  }
}

class _MakeStringClickableState extends State<MakeStringClickable> {
  List<TapSection> sections;
  String textToSplit =
      'FirstWord would like to make each word clickable. On click of a particular word it\'s color should change.';
  TapGestureRecognizer r1;
  @override
  void initState() {
    sections = List<TapSection>();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        alignment: Alignment.center,
        child: _buildTextSpanWithSplittedText(textToSplit, context));
  }

  RichText _buildTextSpanWithSplittedText(
      String textToSplit, BuildContext context) {
    final splittedText = textToSplit.split(" ");
    final spans = List<TextSpan>();
    for (int i = 0; i <= splittedText.length - 1; i++) {
      var tapSection = TapSection(callBack: () {
        setState(() {});
      });
      sections.add(tapSection);
      spans.add(TextSpan(
          text: splittedText[i].toString() + " ",
          style: TextStyle(
              color: sections[i].isPressed ? Colors.black : Colors.red),
          recognizer: sections[i].recognizer));
    }
    return RichText(text: TextSpan(children: spans));
  }
}

class TapSection {
  TapGestureRecognizer recognizer;
  bool isPressed = false;
  final Function callBack;

  TapSection({this.callBack}) {
    recognizer = TapGestureRecognizer();
    recognizer.onTap = () {
      this.isPressed = !this.isPressed;
      this.callBack();
    };
  }
}
TextSpan not displaying return text flutter?

TextSpan not displaying return text flutter?


By : user3305356
Date : March 29 2020, 07:55 AM
seems to work fine I'm working on flutter app in which I used TextSpan widget for displaying the text. But when I returend the text from method TextSpan not display this text. , I tried to recreate your case.
Instead of:
code :
TextSpan(text:formatDate(comments[index].createdAt) +
          " at " formatTime(comments[index].createdAt),
           )
TextSpan(text:formatDate(comments[index].createdAt) +
          " at " + formatTime(comments[index].createdAt),
           )
body: Center(
            child: RichText(
              text: TextSpan(
                  children: [
              TextSpan(text:formatDate("2019-09-30") +
                  " at " + formatTime("2019-09-30"), style: TextStyle(color: Colors.black)),
          ]
                  ),
              ),
            ),
Finding a TextSpan to tap on with Flutter tests

Finding a TextSpan to tap on with Flutter tests


By : vladimir strolyarevs
Date : March 29 2020, 07:55 AM
I hope this helps . CommonFinders byWidgetPredicate method
InlineSpan visitChildren method
code :
final finder = find.byWidgetPredicate(
  (widget) => widget is RichText && tapTextSpan(widget, "bbb "),
);
bool findTextAndTap(InlineSpan visitor, String text) {
  if (visitor is TextSpan && visitor.text == text) {
    (visitor.recognizer as TapGestureRecognizer).onTap();

    return false;
  }

  return true;
}

bool tapTextSpan(RichText richText, String text) {
  final isTapped = !richText.text.visitChildren(
    (visitor) => findTextAndTap(visitor, text),
  );

  return isTapped;
}
Gesture detection in Flutter TextSpan

Gesture detection in Flutter TextSpan


By : Haojie Zheng
Date : March 29 2020, 07:55 AM
Does that help Is there a way to detect which word in the TextSpan was touched by the user? , You can improve by yourself
code :
import 'package:flutter/gestures.dart';
...

new RichText(
      text: new TextSpan(text: 'Non touchable. ', children: [
        new TextSpan(
          text: 'Tap here.',
          recognizer: new TapGestureRecognizer()..onTap = () => print('Tap Here onTap'),
        )
      ]),
    );
How to set rounded corner of TextSpan in Flutter

How to set rounded corner of TextSpan in Flutter


By : BramP
Date : March 29 2020, 07:55 AM
This might help you Without using Container or something else - I can see only one way to make corners rounded
code :
TextSpan(
    text: 'active',
    style: TextStyle(
        fontSize: 20.0,
        color: Colors.white,
        background: Paint()
          ..strokeWidth = 24.0
          ..color = Colors.red
          ..style = PaintingStyle.stroke
          ..strokeJoin = StrokeJoin.round))
shadow
Privacy Policy - Terms - Contact Us © bighow.org