10

I have a requirement to enable text selection within RichText. Also i need to call some code when user click on specific TextSpan.

I tired to run below code. But i notice that onTap is working only with _nonSelectableRichText(), while it is not working with _selectableRichText.

I don’t know if that is a bug or not… but I am asking if anyone has a solution that combines these two features?

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

class TestPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Builder(
          builder: (context) => Container(
            child: _nonSelectionRichText(context),
            //child: _selectionRichText(context),
          ),
        ),
      ),
    );
  }

  Widget _nonSelectableRichText(BuildContext context) {
    return RichText(
      text: TextSpan(
        children: [
          TextSpan(
              text: "Welcome to ",
              style: TextStyle(color: Colors.black, fontSize: 70)),
          TextSpan(
              text: "Flutter",
              style: TextStyle(color: Colors.black, fontSize: 70),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  print("Flutter"); // will work here
                }),
        ],
      ),
    );
  }

  Widget _selectableRichText(BuildContext context) {
    return SelectableText.rich(
      TextSpan(
        children: [
          TextSpan(
              text: "Welcome to ",
              style: TextStyle(color: Colors.black, fontSize: 70)),
          TextSpan(
              text: "Flutter",
              style: TextStyle(color: Colors.black, fontSize: 70),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  print("Flutter"); // will not work here
                }),
        ],
      ),
    );
  }
}
Osama Na
  • 236
  • 4
  • 9
  • 1
    There seems to be an open issue for it. https://github.com/flutter/flutter/issues/43494. Also check this open SO queston: https://stackoverflow.com/questions/59556678/missing-textspans-ontap-callback-using-selectabletext-flutter – Darshan Feb 26 '20 at 04:47
  • @Darshan, thanks for the update. Yeah, you are right. It is a bug and still not solved yet :( – Osama Na Feb 26 '20 at 11:08

1 Answers1

4

This issue has been fixed. I tried running your snippet and verified it to be working on Flutter 2.2

Omatt
  • 8,564
  • 2
  • 42
  • 144