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

如何利用Flutter仿寫微信搜索頁效果

 更新時間:2022年02月10日 15:48:47   作者:晨曦_iOS  
這篇文章主要給大家介紹了關于如何利用Flutter仿寫微信搜索頁效果的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

效果圖

如上圖所示,我們用 Flutter 來仿寫搜索頁面,這里聊天首頁點擊搜索欄會跳轉到搜索頁,搜索頁面包含頂部搜索框跟底部 ListView,在搜索框內(nèi)我們輸入搜索詞會檢索聊天列表模型中 name 屬性中包含搜索詞的模型,并在底部列表中展示,且搜索詞高亮顯示。下面我們分別來介紹下這些功能的實現(xiàn)。

頂部搜索欄

class SearchBar extends StatefulWidget {
  final ValueChanged<String>? onChanged;
  const SearchBar({this.onChanged});

  @override
  _SearchBarState createState() => _SearchBarState();
}

class _SearchBarState extends State<SearchBar> {
  final TextEditingController _editingController = TextEditingController();
  bool _showClose = false;
  void _onChange(text) {
    if (null != widget.onChanged) {
      widget.onChanged!(text);
    }
    setState(() {
      _showClose = ((text as String).length > 0);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 84,
      color: CahtThemColor,
      child: Column(
        children: [
          SizedBox(height: 40,), //上半部分留空
          Container(
            height: 44,
            child: Row(
              children: [
                Container(
                  width: screenWidth(context) - 50,
                  height: 34,
                  margin: EdgeInsets.only(left: 5, right: 10),
                  padding: EdgeInsets.only(left: 5, right: 5),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(6.0),
                  ),
                  child: Row(
                    children: [
                      Image(image: AssetImage('images/放大鏡b.png'), width: 20, color: Colors.grey,), //放大鏡
                      Expanded(child: TextField(
                        controller: _editingController,
                        onChanged: _onChange,
                        autofocus: true,
                        cursorColor: Colors.green,
                        style: TextStyle(
                          fontSize: 16.0,
                          color: Colors.black87,
                          fontWeight: FontWeight.w400,
                        ),
                        decoration: InputDecoration(
                          contentPadding: EdgeInsets.only(left: 5, right: 5, bottom: 12,),
                          border: InputBorder.none,
                          hintText: '搜索',
                          hintStyle: TextStyle(
                            fontSize: 16.0,
                            color: Colors.grey,
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                      ),),
                      if (_showClose) GestureDetector(
                        onTap: () {
                          //清空搜索框
                          _editingController.clear();
                          setState(() {
                            _onChange('');
                          });
                        },
                        child: Icon(
                          Icons.cancel,
                          color: Colors.grey,
                          size: 20,
                        ),
                      ),
                    ],
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  ),
                ), //左邊白色背景
                GestureDetector (
                  onTap: () {
                    Navigator.pop(context);
                  },
                  child: Text('取消'),
                ), //右邊取消按鈕
              ],
            ),
          ), //搜索條部分
        ],
      ),
    );
  }
}

針對搜索頁的整體布局我們可以分為頂部的搜索欄跟底部的搜索列表兩部分,針對搜索欄我們單獨定義了一個 SearchBar 的類來實現(xiàn),底部搜索列表我們用 Expanded 部件進行了包裝。

SearchBar 實現(xiàn)細節(jié)

針對 SearchBar 我們這里整體高度設置為了 84,這里最好高度定義為一個變量,根據(jù)機型來設置。針對搜索欄我們分為上下兩部分,頂部留白及內(nèi)容部分,頂部留白用 SizedBox 部件實現(xiàn),底部內(nèi)容用 Row 部件來實現(xiàn),分為左邊搜索框及右邊取消按鈕。

左邊搜索框實現(xiàn)

搜索框整體部分我們用 Container 部件來實現(xiàn),可以設置寬、高及圓角屬性。child 屬性我們也是用 Row 部件來實現(xiàn),分為左邊搜索圖標、中間 TextField 及右邊 關閉按鈕。

TextField 實現(xiàn)細節(jié)

TextField 部件有兩個比較重要的屬性,onChangedcontroller。onChanged 我們傳入了一個外部方法 _onChange,可以監(jiān)聽輸入框文字的變化,當有文字時展示關閉按鈕,沒有文字時隱藏關閉按鈕。controller 我們傳入了一個外部定義的屬性 _editingController,可以用來控制 TextField 的編輯。

關閉按鈕的實現(xiàn)

關閉按鈕我們根據(jù) _showClose 來判斷是否展示,且通過 GestureDetector 部件進行包裝,點擊的時候清空輸入框及調(diào)用 _onChange 方法,參數(shù)傳入空字符串,_onChange 方法中字符串為空的時候就會隱藏關閉按鈕。

右邊取消按鈕實現(xiàn)

取消按鈕點擊的時候就是返回到上一個頁面。

內(nèi)容的檢索

我們監(jiān)聽到文字輸入框的變化后,就需要進行內(nèi)容的檢索,并且在底部列表中進行展示。

內(nèi)容的傳遞

class SearchCell extends StatelessWidget {
  final List<ChatModel>? datas;
  const SearchCell({this.datas});
}
class SearchPage extends StatefulWidget {
  final List<ChatModel>? datas;
  const SearchPage({this.datas});
}

這里我們是基于聊天頁面列表數(shù)據(jù)模型進行檢索,找到名稱中包含搜索詞的模型進行展示。所以我們在 SearchCell 中定義了 datas 屬性,并且在 SearchPage 中也定義了 datas 屬性,分別在初始化的時候進行傳遞,這樣我們在搜索頁面就可以拿到了聊天列表的模型數(shù)據(jù)。

內(nèi)容的檢索

//滿足查找條件的數(shù)據(jù)數(shù)組
  List<ChatModel> _models = [];
  //正在搜索的內(nèi)容
  String _searchStr = '';
  // 搜索數(shù)據(jù)查找
  void _searchData(String text) {
    //每次搜索前先清空
    _models.clear();
    _searchStr = text;
    if (text.length < 1) {
      setState(() {});
      return;
    }
    for (int i = 0; i < datas.length; i++) {
      String name = widget.datas?[i].name as String;
      if(name.contains(text)) {
        _models.add(widget.datas?[i] as ChatModel);
      }
    }
    setState(() {});
  }

我們把檢索邏輯都抽取到了 _searchData 方法中,輸入內(nèi)容變化的時候調(diào)用 _searchData 方法。這里我們定義了一個 _models 數(shù)組,專門存放檢索數(shù)據(jù),我們遍歷 datas,把檢索到的模型添加到 _models 中。

搜索列表實現(xiàn)

TextStyle _normalStyle = TextStyle(
    fontSize: 16,
    color: Colors.black,
  );
  TextStyle _hightlightedStyle = TextStyle(
    fontSize: 16,
    color: Colors.green,
  );

  Widget _searchTitle(String name) {
    List<TextSpan> textSpans = [];

    List<String> searchStrs = name.split(_searchStr);
    for (int i = 0; i < searchStrs.length; i++) {
      String str = searchStrs[i];
      if (str == '' && i < searchStrs.length - 1) {
        textSpans.add(TextSpan(text: _searchStr, style: _hightlightedStyle));
      } else {
        textSpans.add(TextSpan(text: str, style: _normalStyle));
        if (i < searchStrs.length - 1) {
          textSpans.add(TextSpan(text: _searchStr, style: _hightlightedStyle));
        }
      }
    }
    return RichText(text: TextSpan(children: textSpans));
  }

搜索列表的 cell 就是復用聊天列表的 cell,但是需要變化的是搜索內(nèi)容的高亮顯示,所以 ListTile 部件的 title 屬性我們用 RichText 來實現(xiàn),實現(xiàn)內(nèi)容這里抽取到了 _searchTitle 方法中。name.split(_searchStr) 會返回一個根據(jù)搜索詞 _searchStr 進行分割的數(shù)組,但是當字符串的開頭或者結尾有跟 _searchStr 相同的字符的時候在 searchStrs 數(shù)組中的元素就是空字符串,所以我們就需要進行特別判 str == ''。我們循環(huán)遍歷 searchStrs 數(shù)組來創(chuàng)建 TextSpan 部件,并且根據(jù)內(nèi)容是否跟搜索內(nèi)容一樣來分別指定樣式 _normalStyle 與 _hightlightedStyle。

總結

到此這篇關于如何利用Flutter仿寫微信搜索頁效果的文章就介紹到這了,更多相關Flutter仿寫微信搜索頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Android獲取手機位置的實現(xiàn)代碼

    Android獲取手機位置的實現(xiàn)代碼

    這篇文章主要為大家詳細介紹了Android獲取手機位置的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android 實現(xiàn)手機接通電話后振動提示的功能

    Android 實現(xiàn)手機接通電話后振動提示的功能

    本文主要介紹Android 實現(xiàn)手機接通電話后振動提示的功能,這里整理了詳細的相關資料,并附有示例代碼,有需要的朋友可以參考下
    2016-08-08
  • Android SwipeRefreshLayout超詳細講解

    Android SwipeRefreshLayout超詳細講解

    在android開發(fā)中,使用最多的數(shù)據(jù)刷新方式就是下拉刷新了,而完成此功能我們使用最多的就是第三方的開源庫PullToRefresh?,F(xiàn)如今,google也忍不住推出了自己的下拉組件SwipeRefreshLayout,下面我們通過api文檔和源碼來分析學習如何使用SwipeRefreshLayout
    2022-11-11
  • android開發(fā)之Json文件的讀寫的示例代碼

    android開發(fā)之Json文件的讀寫的示例代碼

    這篇文章主要介紹了android開發(fā)之Json文件的讀寫的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Android中Hilt的使用詳解

    Android中Hilt的使用詳解

    Hilt?是?Android?的依賴項注入庫,可減少在項目中執(zhí)行手動依賴項注入的樣板代碼,本文就來為大家介紹一下Hilt的具體使用吧,希望對大家有所幫助
    2023-06-06
  • Android編程設計模式之單例模式實例詳解

    Android編程設計模式之單例模式實例詳解

    這篇文章主要介紹了Android編程設計模式之單例模式,結合實例形式詳細分析了Android開發(fā)設計模式中單例模式的概念、功能、實現(xiàn)、使用方法及相關注意事項,需要的朋友可以參考下
    2017-12-12
  • 基于Android中獲取資源的id和url方法總結

    基于Android中獲取資源的id和url方法總結

    下面小編就為大家分享一篇基于Android中獲取資源的id和url方法總結,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • 如何使用Flutter發(fā)布安卓應用

    如何使用Flutter發(fā)布安卓應用

    應用開發(fā)完了,當然需要發(fā)布了,下面來看看用 Flutter 開發(fā)的應用如何發(fā)布,本文只關注 Android 版本的發(fā)布
    2021-05-05
  • android實現(xiàn)查詢公交車還有幾站的功能

    android實現(xiàn)查詢公交車還有幾站的功能

    這篇文章主要為大家詳細介紹了android實現(xiàn)查詢公交車還有幾站的功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Android開發(fā)之ImageLoader本地緩存

    Android開發(fā)之ImageLoader本地緩存

    ImageLoader是一個圖片緩存的開源庫,提供了強大的圖片緩存機制,很多開發(fā)者都在使用,今天給大家介紹Android開發(fā)之ImageLoader本地緩存。對imageloader本地緩存相關知識感興趣的朋友一起學習吧
    2016-01-01

最新評論

侯马市| 巢湖市| 馆陶县| 新丰县| 四子王旗| 泾阳县| 乳山市| 崇明县| 新邵县| 德庆县| 静宁县| 山东| 班玛县| 旬阳县| 梨树县| 平湖市| 罗城| 喜德县| 威信县| 兴山县| 新巴尔虎右旗| 大方县| 大邑县| 育儿| 和林格尔县| 洛隆县| 故城县| 祁连县| 安仁县| 雷波县| 基隆市| 二连浩特市| 丹巴县| 洱源县| 石林| 明星| 斗六市| 五常市| 安丘市| 高碑店市| 海丰县|