Skip to content

Mobile: GestureRecognizers in nested TextSpans do not work rightly. #48231

@wonderful89

Description

@wonderful89

In some case, I find the problem in nested TextSpans. Someone can help me ?

image

flutter version

(base) ➜  translator_edu_app git:(release/1.6.4) ✗ flutter --version
Flutter 1.12.13+hotfix.6 • channel unknown • unknown source
Framework • revision 18cd7a3601 (4 weeks ago) • 2019-12-11 06:35:39 -0800
Engine • revision 2994f7e1e6
Tools • Dart 2.7.0

Platform

iOS or Android

Steps to Reproduce

  1. click the word 'for' in first line, But it response to word 'Grain'.
  2. click the word 'for' in second line, it is right. It just add a Space before word 'for'.
  3. I try it in iOS and Android.All have that problem.
class TestTextSpanPage extends StatefulWidget {
  @override
  _TestTextSpanPageState createState() => _TestTextSpanPageState();
}

class _TestTextSpanPageState extends State<TestTextSpanPage> {
  TapGestureRecognizer _recognizer1;
  TapGestureRecognizer _recognizer2;
  TapGestureRecognizer _recognizer3;
  TapGestureRecognizer _recognizer4;

  @override
  void initState() {
    super.initState();
    _recognizer1 = TapGestureRecognizer()
      ..onTap = () {
        print("tapped 111: Grain");
      };
    _recognizer2 = TapGestureRecognizer()
      ..onTap = () {
        print("tapped 222: for");
      };
    _recognizer3 = TapGestureRecognizer()
      ..onTap = () {
        print("tapped 333: the");
      };
    _recognizer4 = TapGestureRecognizer()
      ..onTap = () {
        print("tapped 444: chickens");
      };
  }

  @override
  Widget build(BuildContext context) {
    var style = TextStyle(color: Colors.black, fontSize: 24.0);

    return Scaffold(
        appBar: AppBar(title: Text("TextSpan")),
        body: Center(
          child: Column(
            children: <Widget>[
              Container(
                child: RichText(
                  text: TextSpan(
                    children: <TextSpan>[
                      TextSpan(
                        style: style.copyWith(color: Colors.red),
                        recognizer: _recognizer1,
                        children: [
                          TextSpan(
                            text: 'Gr',
                            style: style.copyWith(color: Colors.green),
                            recognizer: _recognizer1,
                          ),
                          TextSpan(
                            text: 'ai',
                            style: style.copyWith(color: Colors.red),
                            recognizer: _recognizer1,
                          ),
                          TextSpan(
                            text: 'n',
                            style: style.copyWith(color: Colors.green),
                            recognizer: _recognizer1,
                          ),
                        ],
                      ),
                      TextSpan(text: ' '),
                      TextSpan(
                        style: style.copyWith(color: Colors.red),
                        recognizer: _recognizer2,
                        children: [
                          TextSpan(
                            text: 'f',
                            style: style.copyWith(color: Colors.green),
                            recognizer: _recognizer2,
                          ),
                          TextSpan(
                            text: 'o',
                            style: style.copyWith(color: Colors.red),
                            recognizer: _recognizer2,
                          ),
                          TextSpan(
                            text: 'r',
                            style: style.copyWith(color: Colors.green),
                            recognizer: _recognizer2,
                          ),
                        ],
                      ),
                      TextSpan(text: ' '),
                      TextSpan(
                        text: "the",
                        style: style.copyWith(color: Colors.green),
                        recognizer: _recognizer3,
                      ),
                      TextSpan(text: ' '),
                      TextSpan(
                        text: "chickens",
                        style: style.copyWith(color: Colors.green),
                        recognizer: _recognizer4,
                      ),
                    ],
                  ),
                ),
              ),
              Container(
                child: RichText(
                  text: TextSpan(
                    children: <TextSpan>[
                      TextSpan(
                        style: style.copyWith(color: Colors.red),
                        recognizer: _recognizer1,
                        children: [
                          TextSpan(
                            text: 'Gr',
                            style: style.copyWith(color: Colors.green),
                            recognizer: _recognizer1,
                          ),
                          TextSpan(
                            text: 'ai',
                            style: style.copyWith(color: Colors.red),
                            recognizer: _recognizer1,
                          ),
                          TextSpan(
                            text: 'n',
                            style: style.copyWith(color: Colors.green),
                            recognizer: _recognizer1,
                          ),
                        ],
                      ),
                      TextSpan(text: ' '),
                      TextSpan(
                        style: style.copyWith(color: Colors.red),
                        recognizer: _recognizer2,
                        children: [
                          TextSpan(
                            text: ' f',
                            style: style.copyWith(color: Colors.green),
                            recognizer: _recognizer2,
                          ),
                          TextSpan(
                            text: 'o',
                            style: style.copyWith(color: Colors.red),
                            recognizer: _recognizer2,
                          ),
                          TextSpan(
                            text: 'r',
                            style: style.copyWith(color: Colors.green),
                            recognizer: _recognizer2,
                          ),
                        ],
                      ),
                      TextSpan(text: ' '),
                      TextSpan(
                        text: "the",
                        style: style.copyWith(color: Colors.green),
                        recognizer: _recognizer3,
                      ),
                      TextSpan(text: ' '),
                      TextSpan(
                        text: "chickens",
                        style: style.copyWith(color: Colors.green),
                        recognizer: _recognizer4,
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ));
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    f: gesturesflutter/packages/flutter/gestures repository.f: material designflutter/packages/flutter/material repository.frameworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions