Flutter 仿寫微信搜索頁

如下圖所示,我們用 Flutter 來仿寫搜索頁面,這里聊天首頁點擊搜索欄會跳轉(zhuǎn)到搜索頁,搜索頁面包含頂部搜索框跟底部 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),分為左邊搜索框及右邊取消按鈕。

左邊搜索框?qū)崿F(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ù)組,但是當字符串的開頭或者結(jié)尾有跟 _searchStr 相同的字符的時候在 searchStrs 數(shù)組中的元素就是空字符串,所以我們就需要進行特別判 str == ''。我們循環(huán)遍歷 searchStrs 數(shù)組來創(chuàng)建 TextSpan 部件,并且根據(jù)內(nèi)容是否跟搜索內(nèi)容一樣來分別指定樣式 _normalStyle_hightlightedStyle。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容