Accessibility Issue: Multiple Links on Same Line in Flutter RichText - Stack Overflow

Steps to reproduceCreate a RichText widget with multiple TextSpan or WidgetSpan elements, each represen

Steps to reproduce

  1. Create a RichText widget with multiple TextSpan or WidgetSpan elements, each representing a clickable link.

  2. Wrap each link in a Semantics widget to ensure accessibility support.

  3. Add GestureDetector to handle onTap events for each link.

  4. Test using a screen reader (TalkBack on Android or VoiceOver on iOS).

    Code sample:

MergeSemantics(
            child: RichText(
              text: TextSpan(
                style: const TextStyle(color: Colors.black, fontSize: 16.0),
                children: [
                  const TextSpan(
                    text:
                        'Android is a mobile operating system based on a modified version of the ',
                  ),
                  WidgetSpan(
                    child: Semantics(
                      label: 'Linux kernel',
                      button: true,
                      //link: true,
                      child: GestureDetector(
                        onTap: () {
                          print("Link 1 tapped!");
                        },
                        child: const Text(
                          'Linux kernel',
                          style: TextStyle(
                            color: Colors.blue,
                            decoration: TextDecoration.underline,
                          ),
                        ),
                      ),
                    ),
                  ),
                  const TextSpan(
                    text: ' and other ',
                  ),
                  WidgetSpan(
                    child: Semantics(
                      label: 'open-source',
                      button: true,
                      //link: true,
                      child: GestureDetector(
                        onTap: () {
                          print(" link 2 tapped!");
                        },
                        child: const Text(
                          'open-source',
                          style: TextStyle(
                            color: Colors.blue,
                            decoration: TextDecoration.underline,
                          ),
                        ),
                      ),
                    ),
                  ),
                  const TextSpan(
                    text: ' software.',
                  ),
                ],
              ),
            ),

Expected results

Whole content should be readable in a single tap and each link should be clickable.

Actual results

The screen reader only selects or announces the first link consistently.

The second link is often skipped or merged with surrounding text, making it inaccessible.

Steps to reproduce

  1. Create a RichText widget with multiple TextSpan or WidgetSpan elements, each representing a clickable link.

  2. Wrap each link in a Semantics widget to ensure accessibility support.

  3. Add GestureDetector to handle onTap events for each link.

  4. Test using a screen reader (TalkBack on Android or VoiceOver on iOS).

    Code sample:

MergeSemantics(
            child: RichText(
              text: TextSpan(
                style: const TextStyle(color: Colors.black, fontSize: 16.0),
                children: [
                  const TextSpan(
                    text:
                        'Android is a mobile operating system based on a modified version of the ',
                  ),
                  WidgetSpan(
                    child: Semantics(
                      label: 'Linux kernel',
                      button: true,
                      //link: true,
                      child: GestureDetector(
                        onTap: () {
                          print("Link 1 tapped!");
                        },
                        child: const Text(
                          'Linux kernel',
                          style: TextStyle(
                            color: Colors.blue,
                            decoration: TextDecoration.underline,
                          ),
                        ),
                      ),
                    ),
                  ),
                  const TextSpan(
                    text: ' and other ',
                  ),
                  WidgetSpan(
                    child: Semantics(
                      label: 'open-source',
                      button: true,
                      //link: true,
                      child: GestureDetector(
                        onTap: () {
                          print(" link 2 tapped!");
                        },
                        child: const Text(
                          'open-source',
                          style: TextStyle(
                            color: Colors.blue,
                            decoration: TextDecoration.underline,
                          ),
                        ),
                      ),
                    ),
                  ),
                  const TextSpan(
                    text: ' software.',
                  ),
                ],
              ),
            ),

Expected results

Whole content should be readable in a single tap and each link should be clickable.

Actual results

The screen reader only selects or announces the first link consistently.

The second link is often skipped or merged with surrounding text, making it inaccessible.

Share Improve this question edited Nov 19, 2024 at 12:10 Nishore kumar asked Nov 19, 2024 at 8:23 Nishore kumarNishore kumar 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Try using recognizer in TextSpan instead of using WidgetSpan and GestureDetector

MergeSemantics(
        child: RichText(
      text: TextSpan(
        style: const TextStyle(color: Colors.black, fontSize: 16.0),
        children: [
          const TextSpan(
            text:
                'Android is a mobile operating system based on a modified version of the ',
          ),
          TextSpan(
              text: 'Linux kernel',
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  print("Link 1 tapped!");
                }),
          const TextSpan(
            text: ' and other ',
          ),
          TextSpan(
              text: 'open-source',
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  print(" link 2 tapped!");
                }),
          const TextSpan(
            text: ' software.',
          ),
        ],
      ),
    ));
  }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745576176a4633974.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信