extended_rich_text

EasyRichText 小部件提供了一种使用 RichText 的便捷方式,当您想为特定单词使用特定样式时。

该小部件通过定义一个 <pattern, TextStyle> Map 来将字符串拆分成多个 TextSpan;

入门

安装


dependencies:
  easy_rich_text: '^0.1.0'

用法

String str = "This is a EasyRichText example. I want blue font. I want bold font. I want italic font. I want whole sentence bold." ;

简单示例

simple


EasyRichText(
  text: str,
  patternMap: {
    ' bold ': TextStyle(fontWeight: FontWeight.bold),
    'I want whole sentence bold.': TextStyle(fontWeight: FontWeight.bold),
    ' blue ': TextStyle(fontWeight: FontWeight.bold, color: Colors.blue)
  },
)

默认样式

default-style


EasyRichText(
  text: str,
  defaultStyle: TextStyle(color: Colors.grey),
  patternMap: {
    ' bold ': TextStyle(fontWeight: FontWeight.bold),
    ' blue ': TextStyle(fontWeight: FontWeight.bold, color: Colors.blue),
    ' italic ': TextStyle(fontStyle: FontStyle.italic,),
  },
),

所有 RichText 属性均可访问:textAlign, maxLines, overflow 等。

rich-text-overflow


EasyRichText(
  text: str,
  defaultStyle:  TextStyle(
    color:  Colors.grey,
  ),
  patternMap: {
    ' bold ':  TextStyle(fontWeight:  FontWeight.bold),
    'I want whole sentence bold.': TextStyle(fontWeight:  FontWeight.bold),
    ' blue ':  TextStyle(fontWeight:  FontWeight.bold, color:  Colors.blue),
    ' italic ':  TextStyle(fontStyle:  FontStyle.italic,),
  },
  textAlign:  TextAlign.justify,
  maxLines:  1,
  overflow:  TextOverflow.ellipsis,
),

GitHub

https://github.com/2000calories/flutter_easy_rich_text