【问题标题】:Flutter: Enable text selection with clickable TextSpan inside RichTextFlutter:在 RichText 中使用可点击的 TextSpan 启用文本选择
【发布时间】:2020-02-25 13:40:12
【问题描述】:

我需要在 RichText 中启用文本选择。当用户点击特定的 TextSpan 时,我还需要调用一些代码。

我厌倦了运行下面的代码。但我注意到onTap 仅适用于_nonSelectableRichText(),而不适用于_selectableRichText

我不知道这是否是一个错误……但我想问是否有人有结合这两个功能的解决方案?

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
                }),
        ],
      ),
    );
  }
}

【问题讨论】:

标签: flutter richtext textspan


【解决方案1】:

这个问题是fixed。我尝试运行您的 sn-p 并验证它可以在 Flutter 2.2 上运行

【讨论】:

    猜你喜欢
    • 2021-08-07
    • 2021-08-21
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 2020-04-20
    • 1970-01-01
    • 2017-11-12
    相关资源
    最近更新 更多