dart - 当我点击它时如何制作 TextSpan 波纹?

标签 dart flutter

想象一下我有这么长的一段文字:HELLO THIS IS MY LONG SENTENCE。当我点击它时,我希望 LONG 这个词产生波纹(墨水溅起)。

假设我有这个代码:

new RichText(
  text: new TextSpan(
    text: 'HELLO THIS IS MY ',
    style: DefaultTextStyle.of(context).style,
    children: <TextSpan>[
      new TextSpan(text: 'LONG', style: new TextStyle(fontWeight: FontWeight.bold)),
      new TextSpan(text: ' SENTENCE'),
    ],
  ),
)

谢谢!

最佳答案

example

If you want a generic solution to place widgets over portions of text, see this gist.

您可以使用以下代码将波纹限制在文本的特定部分:

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

import 'dart:ui' show TextBox;
import 'dart:math';

void main() {
  runApp(new MaterialApp(
    home: new Material(
      child: new Center(
        child: new Demo(),
      ),
    ),
  ));
}

class Demo extends StatelessWidget {
  final TextSelection textSelection =
      const TextSelection(baseOffset: 17, extentOffset: 21);

  final GlobalKey _textKey = new GlobalKey();

  @override
  Widget build(context) => new Stack(
        children: <Widget>[
          new RichText(
            key: _textKey,
            text: new TextSpan(
              text: 'HELLO THIS IS MY ',
              style: DefaultTextStyle.of(context).style,
              children: <TextSpan>[
                new TextSpan(
                    text: 'LONG',
                    style: new TextStyle(fontWeight: FontWeight.bold)),
                new TextSpan(text: ' SENTENCE'),
              ],
            ),
          ),
          new Positioned.fill(
            child: new LayoutBuilder(
              builder: (context, _) => new Stack(
                    children: <Widget>[
                      new Positioned.fromRect(
                        rect: _getSelectionRect(),
                        child: new InkWell(
                          onTap: () => {}, // needed to show the ripple
                        ),
                      ),
                    ],
                  ),
            ),
          ),
        ],
      );

  Rect _getSelectionRect() =>
      (_textKey.currentContext.findRenderObject() as RenderParagraph)
          .getBoxesForSelection(textSelection)
          .fold(
            null,
            (Rect previous, TextBox textBox) => new Rect.fromLTRB(
                  min(previous?.left ?? textBox.left, textBox.left),
                  min(previous?.top ?? textBox.top, textBox.top),
                  max(previous?.right ?? textBox.right, textBox.right),
                  max(previous?.bottom ?? textBox.bottom, textBox.bottom),
                ),
          ) ??
      Rect.zero;
}

关于dart - 当我点击它时如何制作 TextSpan 波纹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46224355/

相关文章:

flutter - 如何在 flutter 中水平扩展抽屉导航内的折叠项目

flutter - 当 child 等待 parent 的设置状态完成时,如何在两个有状态小部件之间使用 FutureBuilder?

ios - 如何从 Flutter 代码在 Swift 中打开相机 View 以访问自动对焦等相机功能

selenium-webdriver - 在要检查的属性异步的流中查找元素

android - Flutter setState 改变,但不重新渲染

flutter - 如何在 DropdownMenuItem 上使用 onClick 监听器

android - Flutter UI 损坏了第一个屏幕

flutter - Flutter 中的 "dirty"是什么以及导致此 "dirty"状态的原因是什么?

android - 尝试获取位置时避免 Google 提示

java - Dart/Flutter UTF-16 编码