【问题标题】:How to highlight every word in a text in a squence in Flutter?如何在 Flutter 中突出显示序列中文本中的每个单词?
【发布时间】:2021-09-14 13:40:05
【问题描述】:

我想在 Flutter 中突出显示文本,但我想按顺序突出显示文本中的每个单词。因此,第一个单词被突出显示(并且前一个恢复正常,即未突出显示),然后是第二个单词,然后是第三个单词..等等。 我怎么能在 Flutter 中做这样的事情。

#编辑 我想对其进行动画处理,以便高光按顺序移动。例如,第一个单词用黄色突出显示,然后恢复正常,然后第二个单词突出显示,然后恢复正常,依此类推...... 我希望你明白了。

【问题讨论】:

  • 这段代码有帮助吗?
  • RichText( text: TextSpan( children: [ TextSpan(text: 'highlighted',style: TextStyle(color: Colors.black, backgroundColor: Colors.yellow)), TextSpan(text: ' the rest',style: TextStyle(color: Colors.black)), ] ), )

标签: android ios flutter dart mobile


【解决方案1】:

你可以试试:

class HighlightedTextWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    String text = "Hello, this is a highlighted text in Flutter";
    List<String> textList = text.split(" ");
    return Scaffold(
      body: Center(
        child: Text.rich(
          TextSpan(
            children: textList.map((text)  {
              var index = textList.indexOf(text);
              if (index % 2 == 0) {
                return TextSpan(text: text, style: TextStyle(background: Paint()..color = Colors.cyan));
              }
              return TextSpan(text: text, style: TextStyle(background: Paint()..color = Colors.limeAccent));
            }).toList() ,
          ),
        ),
      ),
    );
  }
}

这会给你:

【讨论】:

  • 我想对其进行动画处理,以便高光按顺序移动。例如,第一个单词以黄色突出显示,然后恢复正常,然后第二个单词突出显示,然后恢复正常。我希望你明白了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-05
  • 2013-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多