如何利用Flutter仿写微信搜索页效果

目录
  • 效果图
  • 顶部搜索栏
    • searchbar 实现细节
      • 左边搜索框实现
      • 右边取消按钮实现
  • 内容的检索
    • 内容的传递
    • 内容的检索
      • 搜索列表实现
        • 总结

          效果图

          如上图所示,我们用 flutter 来仿写搜索页面,这里聊天首页点击搜索栏会跳转到搜索页,搜索页面包含顶部搜索框跟底部 listview,在搜索框内我们输入搜索词会检索聊天列表模型中 name 属性中包含搜索词的模型,并在底部列表中展示,且搜索词高亮显示。下面我们分别来介绍下这些功能的实现。

          顶部搜索栏

          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 的类来实现,底部搜索列表我们用 expanded 部件进行了包装。

          searchbar 实现细节

          针对 searchbar 我们这里整体高度设置为了 84,这里最好高度定义为一个变量,根据机型来设置。针对搜索栏我们分为上下两部分,顶部留白及内容部分,顶部留白用 sizedbox 部件实现,底部内容用 row 部件来实现,分为左边搜索框及右边取消按钮。

          左边搜索框实现

          搜索框整体部分我们用 container 部件来实现,可以设置宽、高及圆角属性。child 属性我们也是用 row 部件来实现,分为左边搜索图标、中间 textfield 及右边 关闭按钮。

          textfield 实现细节

          textfield 部件有两个比较重要的属性,onchangedcontrolleronchanged 我们传入了一个外部方法 _onchange,可以监听输入框文字的变化,当有文字时展示关闭按钮,没有文字时隐藏关闭按钮。controller 我们传入了一个外部定义的属性 _editingcontroller,可以用来控制 textfield 的编辑。

          关闭按钮的实现

          关闭按钮我们根据 _showclose 来判断是否展示,且通过 gesturedetector 部件进行包装,点击的时候清空输入框及调用 _onchange 方法,参数传入空字符串,_onchange 方法中字符串为空的时候就会隐藏关闭按钮。

          右边取消按钮实现

          取消按钮点击的时候就是返回到上一个页面。

          内容的检索

          我们监听到文字输入框的变化后,就需要进行内容的检索,并且在底部列表中进行展示。

          内容的传递

          class searchcell extends statelesswidget {
            final list<chatmodel>? datas;
            const searchcell({this.datas});
          }
          class searchpage extends statefulwidget {
            final list<chatmodel>? datas;
            const searchpage({this.datas});
          }

          这里我们是基于聊天页面列表数据模型进行检索,找到名称中包含搜索词的模型进行展示。所以我们在 searchcell 中定义了 datas 属性,并且在 searchpage 中也定义了 datas 属性,分别在初始化的时候进行传递,这样我们在搜索页面就可以拿到了聊天列表的模型数据。

          内容的检索

          //满足查找条件的数据数组
            list<chatmodel> _models = [];
            //正在搜索的内容
            string _searchstr = '';
            // 搜索数据查找
            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 方法中,输入内容变化的时候调用 _searchdata 方法。这里我们定义了一个 _models 数组,专门存放检索数据,我们遍历 datas,把检索到的模型添加到 _models 中。

          搜索列表实现

          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,但是需要变化的是搜索内容的高亮显示,所以 listtile 部件的 title 属性我们用 richtext 来实现,实现内容这里抽取到了 _searchtitle 方法中。name.split(_searchstr) 会返回一个根据搜索词 _searchstr 进行分割的数组,但是当字符串的开头或者结尾有跟 _searchstr 相同的字符的时候在 searchstrs 数组中的元素就是空字符串,所以我们就需要进行特别判 str == ”。我们循环遍历 searchstrs 数组来创建 textspan 部件,并且根据内容是否跟搜索内容一样来分别指定样式 _normalstyle 与 _hightlightedstyle。

          总结

          到此这篇关于如何利用flutter仿写微信搜索页效果的文章就介绍到这了,更多相关flutter仿写微信搜索页内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

          (0)
          上一篇 2022年3月22日
          下一篇 2022年3月22日

          相关推荐