【问题标题】:Border around textSpan of RichTextRichText 的 textSpan 周围的边框
【发布时间】:2020-06-10 05:36:53
【问题描述】:

我有一个 RichText 小部件,其中包含一些 TextSpan.TextSpan 小部件周围,我想在它周围放置一个边框。另外,我希望它可以在网络上运行,但目前从我的方法来看,它仅适用于移动设备。

预期:我想实现以下目标。

使用此代码的结果:

    Paint paint = Paint()
  ..color = Colors.blue
  ..style = PaintingStyle.stroke
  ..strokeCap = StrokeCap.round
  ..strokeWidth = 2.0;

RichText(
   text: TextSpan(children: [
       TextSpan(text: text1),
       TextSpan(text: text2, style: TextStyle(background: paint)),
       TextSpan(text: text3, )
  ]))

请帮忙!!!

如果不清楚的话,简单来说,我只想在高亮部分周围有一个边框,并去掉下图中的高亮而不改变任何其他内容

【问题讨论】:

  • 用容器包装你的 RichText 并为你选择的文本添加边框:)
  • 我不想为整个 RichText 添加边框,而是在 RichText 中添加单独的 textSpan,以便我可以自定义同一段落之间的文本并获得如上所述的结果。跨度>
  • 嗨,Kuldip,我刚刚检查了这个飞镖垫,可以重现这个问题。看起来像 flutter-web 中的一个错误,因为您提到它可以在移动设备上运行。
  • 也许您可以使用此示例代码在 github 中提出和发布。
  • 我会记得这样做,但我仍然无法在 android 中获得所需的结果。

标签: flutter flutter-layout flutter-dependencies flutter-web


【解决方案1】:

你可以像这样在容器内使用文本

return Container(
        margin: const EdgeInsets.all(30.0),
        padding: const EdgeInsets.all(10.0),
        decoration: BoxDecoration(
    border: Border.all(),
  ), 
        child: Text(
          "text",
          style: TextStyle(fontSize: 30.0),
        ),
      );

【讨论】:

  • 我正在寻找一种方法来突出显示段落中间的随机文本,而不仅仅是单个文本小部件。因此,这种方法不适合我。不过感谢您的帮助。 :D
【解决方案2】:

由于操作的问题发生在 web 中,由于 Flutter-web 本身存在错误,目前无法解决。但是,以下答案包含一个通用解决方案,该解决方案在固定后应该可以在颤振网络中使用。

新答案:04.03.2020 为了达到您想要的效果,有一个名为blendMode 的选项,您必须在Paint 对象中使用BlendMode.difference。您可以使用它来获得类似于您想要的结果。

相同的代码

 TextSpan(
          style: TextStyle(
            background: Paint()
              ..color = Colors.blue
              ..style = PaintingStyle.stroke
              ..strokeWidth = 2.0
              ..strokeJoin = StrokeJoin.bevel
              ..blendMode = BlendMode.difference,
          ),
          text:
              'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
        ),

注意:我也在尝试另一种费力的手动绘制边界的方法。如果您遇到其他一些解决方案,请在此处分享。 :) 希望这对你有所帮助。

旧答案

所以根据运行时的上下文,我切换文本样式。对于网络,这会传递TextDecorations 的列表,例如underlineoverline。这不包括文本的结尾,但我相信比在结果中完全突出显示的文本更好。还有一些额外的选项,如 decorationThicknessdecorationStyle 可以修改。

这是解决方案在 Flutter Web 模式下的外观。

Paint paint = Paint()
      ..color = Colors.blue
      ..style = PaintingStyle.stroke
      ..strokeCap = StrokeCap.round
      ..strokeWidth = 1.0;

    return RichText(
      text: TextSpan(
        children: [
          TextSpan(text: 'bounded text1'),
          TextSpan(
            text: ' Will this work ',
            style: kIsWeb
                ? TextStyle(
                    decoration: TextDecoration.combine(
                        [TextDecoration.overline, TextDecoration.underline]),
                    decorationColor: Colors.blue,
                  )
                : TextStyle(
                    background: paint,
                  ),
          ),
          TextSpan(
            text: 'bounded text 3',
          )
        ],
      ),
    );

我会将这些样式提取为一个常量,并根据当前运行的上下文(如 android 或 web)动态应用它们。希望这会有所帮助。

【讨论】:

  • 感谢您的帮助,但让它适用于网络并不是我的首要问题,我的首要任务是让它看起来像问题中提到的第一个数字,至少在 android 上,如果可能的话以后上网。你有什么想法,我怎么能做到这一点?再次感谢!
  • 感谢您的帮助,还有一个小问题,它只适用于黑色背景,并且在这样做时仍然存在一些伪影。我想让它在任何情况下都能正常工作。感谢您的所有帮助,不胜感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-05
  • 2011-06-08
  • 1970-01-01
相关资源
最近更新 更多