Flutter RichText 未显示

mak*_*ako 6 flutter

我在支架内的https://api.flutter.dev/flutter/widgets/RichText-class.html的 Centered 中有一个常规的普通富文本小部件。我看不到它。我什么也看不见。调试工具显示富文本确实存在于小部件层次结构中(尽管我看不到其中的 TextSpan,大概它们就在那里。)

Scaffold(body: Center(child:
  RichText(
    text: TextSpan(
      text: 'Hello ',
      style: DefaultTextStyle.of(context).style,
      children: <TextSpan>[
        TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
        TextSpan(text: ' world!'),
      ],
    ),
  ),
));
Run Code Online (Sandbox Code Playgroud)

mak*_*ako 10

我尝试了 Android 版本,看看这是否是 Linux 桌面版本特有的错误。事实并非如此!这里也发生了这种事。然而,我的手机的对比度足够高,我几乎看不到里面的文字,“White on Off White”。事实证明,与 Text 小部件不同,RichText 文本的默认颜色是白色,与主题的背景相同,因此文本不可见。

(该应用程序是否有可能响应系统范围的暗模式主题?不。切换主题不会影响任何内容。)

那么,为什么这是默认颜色?DefaultTextStyle我猜因为是坏的和错误的?使用Theme.of(context).textTheme.bodyText1您将得到您期望的结果

  • 更好的解决方案是将 `RichText(...)` 替换为 `Text.rich(...)`,您可以保留您的跨度:) (2认同)

小智 5

RichText是一种基本的、较低级别的文本渲染方法,并且有意不使用主题样式。

然而,Text.rich确实:

Scaffold(body: Center(
  child: Text.rich(
    TextSpan(
      text: 'Hello ',
      children: <TextSpan>[
        TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
        TextSpan(text: ' world!'),
      ],
    ),
  ),
));
Run Code Online (Sandbox Code Playgroud)