158

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

例如:

你好世界

4

6 回答 6

322

您应该使用RichText小部件。

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

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

这是要呈现的示例代码:Hello World

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)),
    ],
  ),
);
于 2017-01-09T21:20:01.590 回答
43

[更新]

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

[旧答案]

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

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

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

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

于 2018-12-02T15:13:21.017 回答
17
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?'),
    ],
  ),
);
于 2019-05-17T06:32:04.230 回答
4

您应该在这里使用类中的Text.rich构造函数。Text

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

为什么我推荐它而不是RichText因为使用RichText你需要定义父级TextStyleRichText但使用你的构造rich函数Text不需要显式定义父级TextStyleText.rich

这是示例如何使用它并获得相同的结果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)),
    ],
  ),
 );

使用rich构造函数Text

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)),
    ],
  ),
)
于 2021-07-07T12:27:32.910 回答
2

正则表达式

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

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

于 2021-03-09T01:21:55.170 回答
-1

另一种方法:

Row(
      children: [
        Text("Hello "),
        Text("World",
            style: TextStyle(
                fontWeight: FontWeight.bold, color: Colors.blueAccent)),
      ],
);
于 2021-07-14T04:41:57.720 回答