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

想象一下我有这么长的一段文字: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'),
    ],
  ),
)

谢谢!

最佳答案

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;
}

https://stackoverflow.com/questions/46224355/

相关文章:

flutter - 它们在 flutter 中是什么意思 'cross axis' ?

android - 将数据广播到 Flutter 中的多个小部件

dart - 如何在 Widget 构建方法中调用异步属性

dart - 构造函数可选参数

flutter - 无法在 flutter 中同步 build.gradle 中的依赖项

dart - 我们如何在flutter中更改appbar背景颜色

android-studio - 如何使 Flutter 应用的 Navigation Drawer

android - Flutter 中的 ListView 居中

user-interface - Flutter slideTransition没有动画

dart - 如何知道用户是否看到了 flutter 图像