flutter - 检查文本小部件是否有样式

标签 flutter dart

我正在尝试将默认样式添加到 Text Widget,但我需要能够覆盖它。

这就是我现在正在尝试的。

var newTitle = title;
    if (title is Text) {
      Text titleText = title as Text;
      newTitle = Text(titleText.data, style: TextStyle == null ? TextStyle() : TextStyle(fontWeight: FontWeight.bold));
    }

所以我想检查它是否有样式,如果没有给它添加默认样式,否则使用覆盖

最佳答案

Flutter 通过提供 DefaultTextStyle 小部件提供了一种简单的方法来执行此操作,该小部件可用于为子树指定默认的文本样式。如果子 Text 小部件已经定义了样式,则将使用特定的 Text 样式。

DefaultTextStyle(
  child: title,
  style: TextStyle(fontWeight: FontWeight.bold),
),

关于flutter - 检查文本小部件是否有样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57473269/

相关文章:

flutter - 有没有办法让 Flutter Timer.periodic 在进入下一个周期之前等待函数返回加上固定的持续时间?

flutter - 在 Flutter 的列中插入一行

postgresql - 如果我需要等待连接,我应该做主异步吗?

flutter - 如果在底页上按确认,如何重建屏幕?

flutter - PageView 内部的 GridView 抖动

Flutter Googleapis_auth 从刷新 token 中获取新的访问 token

Flutter的Webview-如何清除 session 状态?

json - 如何将复杂(嵌套)对象解析为 JSON 并在 flutter 中使用 HTTP 将其发送到服务器?

flutter - 电子邮件验证不随电子邮件文本字段更改而更新

mobile - Chrome Dev Editor中的Mobile Dart工作流程