【问题标题】:How do I bold (or format) a piece of text within a paragraph?如何在段落中加粗(或格式化)一段文本?
【发布时间】:2017-05-24 06:03:37
【问题描述】:

我怎样才能有不同格式的文本行?

例如:

你好世界

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    您应该使用RichText 小部件。

    RichText 小部件将采用TextSpan 小部件,该小部件还可以具有子 TextSpan 的列表。

    每个 TextSpan 小部件可以有不同的 TextStyle

    这里是要渲染的示例代码: 你好世界

    var text = RichText(
      text: TextSpan(
        // Note: Styles for TextSpans must be explicitly defined.
        // Child text spans will inherit styles from parent
        style: const TextStyle(
          fontSize: 14.0,
          color: Colors.black,
        ),
        children: <TextSpan>[
          TextSpan(text: 'Hello'),
          TextSpan(text: 'World', style: const TextStyle(fontWeight: FontWeight.bold)),
        ],
      ),
    );
    

    【讨论】:

    【解决方案2】:

    [更新]

    下面的答案最适合几个单词而不是一个段落,如果您有一个长句子或需要格式化特定文本的段落,则更喜欢使用上面答案中@DvdWasibi 建议的 RichText

    [旧答案]

    我喜欢保持我的代码简洁明了,这就是我将如何在一行中添加两个文本字段,一个是普通字体,另一个是粗体

    注意:这可能不适合长段落看起来适合标题等。

    Row(children: [
      Text("Hello"),
      Text("World", style: const TextStyle(fontWeight: FontWeight.bold))
    ]);
    

    你应该得到一个期望的输出为“Hello World

    【讨论】:

    • 这不是一个好主意,如果你打算有一段文字。行内的每个 Text() 都会创建自己的垂直/水平空间。
    • 那么使用 Row 的替代方法是什么?如果我想要不同格式的文本并排
    • 查看@Dvdwasibi 的上述答案,只需尝试大段落的实现,您就会发现两个不同的段落并排。你的答案对 2/3 个单词是正确的,但对一个段落来说是正确的。
    • 同意,顺便感谢我更新了我的答案。 :)
    【解决方案3】:
    return RichText(
      text: TextSpan(
        text: 'Can you ',
        style: TextStyle(color: Colors.black),
        children: <TextSpan>[
          TextSpan(
            text: 'find the',
            style: TextStyle(
              color: Colors.green,
              decoration: TextDecoration.underline,
              decorationStyle: TextDecorationStyle.wavy,
            ),
            recognizer: _longPressRecognizer,
          ),
          TextSpan(text: 'secret?'),
        ],
      ),
    );
    

    【讨论】:

      【解决方案4】:

      正则表达式

      您可以使用此小部件。下面的示例总是将数字加粗。

      import 'package:flutter/material.dart';
      
      class TextBold extends StatelessWidget{
        final String text;
        final String regex;
        static const _separator = " ";
      
        const TextBold({Key key, this.text, this.regex = r'\d+'}) : super(key: key);
      
        @override
        Widget build(BuildContext context) {
      
          final parts = splitJoin();
      
          return Text.rich(TextSpan(
              children: parts.map((e) => TextSpan(
                  text: e.text,
                  style: (e.isBold)
                      ? const TextStyle(fontFamily: 'bold')
                      : const TextStyle(fontFamily: 'light')))
                  .toList()));
        }
      
        // Splits text using separator, tag ones to be bold using regex
        // and rejoin equal parts back when possible
        List<TextPart> splitJoin(){
          assert(text!=null);
      
          final tmp = <TextPart>[];
      
          final parts = text.split(_separator);
      
          // Bold it
          for (final p in parts){
            tmp.add(TextPart(p + _separator,p.contains(RegExp(regex))));
          }
      
          final result = <TextPart>[tmp[0]];
          // Fold it
          if (tmp.length>1) {
            int resultIdx = 0;
            for (int i = 1; i < tmp.length; i++)
              if (tmp[i - 1].isBold != tmp[i].isBold) {
                result.add(tmp[i]);
                resultIdx++;
              }
              else
                result[resultIdx].text = result[resultIdx].text
                    + tmp[i].text;
          }
      
          return result;
        }
      }
      
      class TextPart{
        String text;
        bool isBold;
      
        TextPart(this.text, this.isBold);
      }
      
      

      【讨论】:

      • 不能开箱即用,对于未来的用户,将 fontFamily 替换为 fontWeight:bold/nomal 和 eureka 效果很好!!!!!!!。
      【解决方案5】:

      您应该使用来自TexthereText.rich 构造函数。

      通过使用rich 构造函数,您可以显示具有不同样式TextSpans 的段落。

      为什么我推荐它而不是 RichText 是因为通过使用 RichText 你需要在 RichText 中定义父 TextStyle 但使用 rich 的构造函数 Text 你不需要在Text.rich中显式定义了父TextStyle

      这是如何使用它并获得相同结果的示例 使用RichText

      const text = RichText(
        text: TextSpan( 
          // Here is the explicit parent TextStyle
          style: new TextStyle(
            fontSize: 16.0,
            color: Colors.black,
            fontFamily: 'Montserrat', 
          ),
          children: <TextSpan>[
            new TextSpan(text: 'Hello'),
            new TextSpan(text: 'World', style: new TextStyle(fontWeight: FontWeight.bold)),
          ],
        ),
       );
      

      使用Textrich构造函数

      const text = Text.rich(
        TextSpan(
          // with no TextStyle it will have default text style
          text: 'Hello',
          children: <TextSpan>[
            TextSpan(text: 'World', style: TextStyle(fontWeight: FontWeight.bold)),
          ],
        ),
      )
      

      【讨论】:

      • 这种方式比 RichText 好,因为它使用了正确的 TextStyle 和 textScaleFactor。
      【解决方案6】:

      另一种方法:

      Row(
            children: [
              Text("Hello "),
              Text("World",
                  style: TextStyle(
                      fontWeight: FontWeight.bold, color: Colors.blueAccent)),
            ],
      );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-16
        • 1970-01-01
        • 2019-09-27
        • 1970-01-01
        • 2016-09-13
        • 2013-08-19
        • 2017-06-12
        相关资源
        最近更新 更多