最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Flutter實現(xiàn)簡單的內(nèi)容高亮效果

 更新時間:2023年08月15日 08:38:11   作者:程序員一鳴  
內(nèi)容高亮并不陌生,特別是在搜索內(nèi)容頁面,可以說四處可見,這篇文章主要為大家介紹了如何使用Flutter實現(xiàn)簡單的內(nèi)容高亮效果,需要的可以參考下

內(nèi)容高亮并不陌生,特別是在搜索內(nèi)容頁面,可以說四處可見,就拿掘金這個應(yīng)用而言,針對某一個關(guān)鍵字,我們搜索之后,與關(guān)鍵字相同的內(nèi)容,則會高亮展示,如下圖所示:

如上的效果,在Flutter當(dāng)中,實現(xiàn)起來可以說是無比的簡單,畢竟原生的組件都給我們提供了,那就是富文本組件RichText。

針對今天的內(nèi)容,簡單的列一個大綱,主要內(nèi)容如下:

  • 1、案例簡單效果
  • 2、認識RichText
  • 3、文本的高亮實現(xiàn)邏輯
  • 4、高亮組件源碼

一、案例簡單效果

1、簡單的內(nèi)容高亮展示

2、列表形式內(nèi)容展示

二、認識RichText

要實現(xiàn)高亮效果,那么我們必須了解富文本組件RichText,話又說回來,什么是富文本呢?簡單來說,它是一種特殊的文本格式,比普通文本更加豐富多彩,可以包含各種字體、顏色、大小等元素,使文本更加生動、有趣,比如我們常見的閱讀協(xié)議等場景,均可采用富文本形式,這是原生的文本無法實現(xiàn)的效果。

初識構(gòu)造

構(gòu)造屬性需要注意的是,這里的text,和文本Text中的text是不一樣的,文本Text指的是字符串,這里的text指的是InlineSpan,當(dāng)然了InlineSpan是抽象基類,一般我們使用TextSpan。

RichText({
    super.key,
    required this.text,
    this.textAlign = TextAlign.start,
    this.textDirection,
    this.softWrap = true,
    this.overflow = TextOverflow.clip,
    this.textScaleFactor = 1.0,
    this.maxLines,
    this.locale,
    this.strutStyle,
    this.textWidthBasis = TextWidthBasis.parent,
    this.textHeightBehavior,
    this.selectionRegistrar,
    this.selectionColor,
  }) : assert(text != null),
       assert(textAlign != null),
       assert(softWrap != null),
       assert(overflow != null),
       assert(textScaleFactor != null),
       assert(maxLines == null || maxLines > 0),
       assert(textWidthBasis != null),
       assert(selectionRegistrar == null || selectionColor != null),
       super(children: _extractChildren(text));

常見構(gòu)造屬性概述

const TextSpan({
    this.text,
    this.children,
    super.style,
    this.recognizer,
    MouseCursor? mouseCursor,
    this.onEnter,
    this.onExit,
    this.semanticsLabel,
    this.locale,
    this.spellOut,
  }) : mouseCursor = mouseCursor ??
         (recognizer == null ? MouseCursor.defer : SystemMouseCursors.click),
       assert(!(text == null && semanticsLabel != null));
屬性類型概述
textAlignTextAlign文本對齊方式TextAlign.left TextAlign.right TextAlign.cente TextAlign.justify TextAlign.start TextAlign.end
textDirectionTextDirection文本的方向TextDirection.ltr TextDirection.rtl
overflowTextOverflow文字溢出的處理方式 TextOverflow.clip:剪切溢出的文本填滿容器。 TextOverflow.fade:將溢出的文本淡化為透明。 TextOverflow.ellipsis:使用省略號表示文本已溢出。 TextOverflow.visible:呈現(xiàn)容器外溢出的文本
maxLinesint最大行數(shù)
textWidthBasisTextWidthBasis文本的寬度TextWidthBasis.parentTextWidthBasis.longestLine

TextSpan常見屬性

屬性說明
textString類型的文本
children子組件
styleTextStyle類型的文本樣式可以設(shè)置文字的大小、顏色、樣式等
recognizer指定手勢交互 recognizer: TapGestureRecognizer()..onTap = () {},可以監(jiān)聽點擊事件

簡單案例

RichText(
            text: const TextSpan(children: [
              TextSpan(text: "床前明月光,", style: TextStyle(color: Colors.black)),
              TextSpan(text: "疑是地上霜。", style: TextStyle(color: Colors.red)),
              TextSpan(text: "舉頭望明月,", style: TextStyle(color: Colors.blueAccent)),
              TextSpan(text: "低頭思故鄉(xiāng)。", style: TextStyle(color: Colors.tealAccent))
            ])

效果

當(dāng)然了,除了上述寫法之外,也可以使用Text.rich來實現(xiàn),代碼如下:

 const Text.rich(TextSpan(children: [
            TextSpan(text: "床前明月光,", style: TextStyle(color: Colors.black)),
            TextSpan(text: "疑是地上霜。", style: TextStyle(color: Colors.red)),
            TextSpan(text: "舉頭望明月,", style: TextStyle(color: Colors.blueAccent)),
            TextSpan(text: "低頭思故鄉(xiāng)。", style: TextStyle(color: Colors.tealAccent))
          ]))

三、文本的高亮實現(xiàn)邏輯

RichText可以實現(xiàn)一個富文本展示,那么如何利用這個組件實現(xiàn)某個內(nèi)容高亮展示呢?首先,我們要明白,高亮的內(nèi)容是不固定的,一段內(nèi)容的每個字符都有可能會高亮,所以針對TextSpan,我們就需要動態(tài)的創(chuàng)建,然后動態(tài)的改變其樣式。

這里的動態(tài)也是十分的簡單,無非就是字符串的截取,分別是開頭、結(jié)尾、和中間三種情況進行截取,如下圖所示。

當(dāng)然了,需要注意,有可能要搜索的這個內(nèi)容,在整個內(nèi)容中是多處存在的,這個時候,針對以上的邏輯,就需要遍歷循環(huán)了,直至找到最后一個搜索的內(nèi)容。

主要的邏輯如下

 //搜索內(nèi)容為空
    if (_searchContent == "") {
      return Text(
        _content,
        style: _ordinaryStyle,
      );
    }
    List<TextSpan> richList = [];
    int start = 0;
    int end;
    //遍歷,進行多處高亮
    while ((end = _content.indexOf(_searchContent, start)) != -1) {
      //如果搜索內(nèi)容在開頭位置,直接高亮,此處不執(zhí)行
      if (end != 0) {
        richList.add(TextSpan(
            text: _content.substring(start, end), style: _ordinaryStyle));
      }
      //高亮內(nèi)容
      richList.add(TextSpan(text: _searchContent, style: _highlightStyle));
      //賦值索引
      start = end + _searchContent.length;
    }
    //搜索內(nèi)容只有在開頭或者中間位置,才執(zhí)行
    if (start != _content.length) {
      richList.add(TextSpan(
          text: _content.substring(start, _content.length),
          style: _ordinaryStyle));
    }
    return RichText(
      text: TextSpan(children: richList),
    );

四、高亮組件源碼

源碼很簡單,可以結(jié)合列表組件或者單獨使用,當(dāng)然了,有一些特殊需求,文字加大或者改變背景等需求,都可以進行擴展。

class TextHighlight extends StatelessWidget {
  final TextStyle _ordinaryStyle; //普通的樣式
  final TextStyle _highlightStyle; //高亮的樣式
  final String _content; //文本內(nèi)容
  final String _searchContent; //搜索的內(nèi)容
  const TextHighlight(this._content, this._searchContent, this._ordinaryStyle,
      this._highlightStyle,
      {super.key});
  @override
  Widget build(BuildContext context) {
    //搜索內(nèi)容為空
    if (_searchContent == "") {
      return Text(
        _content,
        style: _ordinaryStyle,
      );
    }
    List<TextSpan> richList = [];
    int start = 0;
    int end;
    //遍歷,進行多處高亮
    while ((end = _content.indexOf(_searchContent, start)) != -1) {
      //如果搜索內(nèi)容在開頭位置,直接高亮,此處不執(zhí)行
      if (end != 0) {
        richList.add(TextSpan(
            text: _content.substring(start, end), style: _ordinaryStyle));
      }
      //高亮內(nèi)容
      richList.add(TextSpan(text: _searchContent, style: _highlightStyle));
      //賦值索引
      start = end + _searchContent.length;
    }
    //搜索內(nèi)容只有在開頭或者中間位置,才執(zhí)行
    if (start != _content.length) {
      richList.add(TextSpan(
          text: _content.substring(start, _content.length),
          style: _ordinaryStyle));
    }
    return RichText(
      text: TextSpan(children: richList),
    );
  }
}

案例Demo很是簡單,上邊是搜索框,下面是展示的內(nèi)容,這里就不貼了,高亮組件已經(jīng)給大家提供了,大家可以直接復(fù)制使用。

以上就是Flutter實現(xiàn)簡單的內(nèi)容高亮效果的詳細內(nèi)容,更多關(guān)于Flutter內(nèi)容高亮的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

确山县| 三门县| 牡丹江市| 乌苏市| 民乐县| 义乌市| 桑植县| 翁牛特旗| 深州市| 鱼台县| 中阳县| 新兴县| 龙里县| 外汇| 湟中县| 托克逊县| 吐鲁番市| 屯昌县| 永兴县| 开化县| 白水县| 蒲江县| 泌阳县| 图片| 通城县| 富平县| 高邮市| 通江县| 资源县| 万源市| 敖汉旗| 北碚区| 赤峰市| 长沙市| 阳曲县| 阿拉善左旗| 威海市| 巴彦淖尔市| 大同市| 六安市| 错那县|