flutter 仿微信通讯录页面

具体实现效果:

呵呵.gif

使用的插件:

//获取汉字拼音
lpinyin: ^2.0.3

主要注意点:
1、在从接口获取好友列表后,需要保存每个好友名字的拼音首字母

单独写了一个对象用来保存每个好友名字的拼音首字母:

class FriendVo {
  V2TimFriendInfo info;
//名字拼音首字母
  String pin;
  FriendVo({
    this.info,
    this.pin,
  });
}

从接口获取数据源之后的处理

 List<FriendVo> _friendList = [];
//_resultList是接口返回的列表  _friendList为最终的好友列表数据源
_resultList.forEach((element) {
        String now = '#';
        String nowPin = PinyinHelper.getFirstWordPinyin(_getShowName(element));
        if (nowPin.length > 0) {
          now = indexList.contains(nowPin.substring(0, 1).toUpperCase())
              ? nowPin.substring(0, 1)
              : '#';
        }
        FriendVo vo = FriendVo(info: element,pin: now.toUpperCase());
        _friendList.add(vo);
      });

//处理好的数据源按照首字母进行重新排序
_friendList.sort((FriendVo a, FriendVo b) {
        if (a.pin == '#'){
          return 1;
        }
        if (b.pin == '#'){
          return 0;
        }
        return a.pin.compareTo(b.pin);
      });

默认创建多个GlobalKey对象,用来获取每个item距离顶部的距离

_keyList = List.generate(_friendList.length, (index) => GlobalKey());

UI数据源处理

List<Widget> itemList = [];
//顶部菜单
if (_menuList.length > 0){
      itemList.addAll(_menuList.map((e){
        int index = _menuList.indexOf(e);
        return MenuItem(
          menuVo: e,
          isHiddenLine: index == _menuList.length - 1,
          onSelect: (int value){
            if (value == 1){
              Navigator.push(context, MaterialPageRoute(builder: (context){
                return GroupListPage();
              }));
            }
          },
        );
      }).toList()
      );
    }
itemList.addAll(_friendList.map((e) {
      int index = _friendList.indexOf(e);
     //此处判断是都显示字母分组,判断逻辑为,每个item只需跟上一个item的首字母对比,如果相同就不显示,否则显示
      FriendVo vo = e;
      bool isShow = true;
      if (nextVo != null){
        if (vo.pin == nextVo.pin) {
          isShow = false;
        }
      }
      nextVo = vo;
      return FriendItem(
        key: _keyList[index],
        vo: vo,
        isShowPin: isShow,
        isHiddenLine: index == _friendList.length - 1,
      );
    }).toList());

2、由于需要做到左边好友列表跟右边字母列表联动滚动,所以需要获取item的位置。整个页面刷新完成即可通过key获取每个item距离顶部的距离,然后保存

//保存的当前数据源包含的字母(不重复)
List<String> _filterList = [];
//保存每个显示字母的item距离顶部的距离
List<double> _locList = [];
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
        _subInitState();
      });
 _subInitState() {
    FriendVo _nextVo;
    for (int i = 0; i < _friendList.length; i++) {
      FriendVo vo = _friendList[i];
      bool isShow = true;
      String now = vo.pin;
      if (_nextVo != null){
        String back = _nextVo.pin;
        if (back == now) {
          isShow = false;
        }
      }
      _nextVo = vo;
      if (isShow) {
        var globalKey = _keyList[i];
        var offsetY = getY(globalKey.currentContext)-_top;
        print('now:${now}');
        print('offsetY:${offsetY}');
        if (!_filterList.contains(vo.pin)){
          _filterList.add(vo.pin);
          _locList.add(offsetY);
        }
      }
    }
    _indexKey.currentState.updateIndex(_filterList);
  }

3、选中字母列表某个item

onVerticalDragUpdate: (DragUpdateDetails details) {
        print('onVerticalDragUpdate');
        print('globalPosition:${details.globalPosition}');
        _selectIndex = getIdex(context, details.globalPosition);
        double _bubbleY = details.globalPosition.dy;
        bool _bubbleHidden = false;
        if (widget.onSelect != null) {
          widget.onSelect(
              _itemList[_selectIndex < 0
                  ? 0
                  : (_selectIndex > (_itemList.length - 1)
                      ? _itemList.length - 1
                      : _selectIndex)],
              _bubbleY,
              _bubbleHidden);
        }
        setState(() {});
      },
      //按住屏幕移动手指实时更新触摸的位置坐标
      onVerticalDragDown: (DragDownDetails details) {
        print('onVerticalDragDown');
        _selectIndex = getIdex(context, details.globalPosition);
        double _bubbleY = details.globalPosition.dy;
        bool _bubbleHidden = false;
        if (widget.onSelect != null) {
          widget.onSelect(
              _itemList[_selectIndex < 0
                  ? 0
                  : (_selectIndex > (_itemList.length - 1)
                      ? _itemList.length - 1
                      : _selectIndex)],
              _bubbleY,
              _bubbleHidden);
        }
        print('现在点击的位置是${details.globalPosition}');
        setState(() {});
      },
      //触摸开始
      onVerticalDragEnd: (DragEndDetails details) {
        print('onVerticalDragEnd');
        double _bubbleY = 0;
        bool _bubbleHidden = true;
        if (widget.onSelect != null) {
          widget.onSelect(
              _itemList[_selectIndex < 0
                  ? 0
                  : (_selectIndex > (_itemList.length - 1)
                      ? _itemList.length - 1
                      : _selectIndex)],
              _bubbleY,
              _bubbleHidden);
        } //触摸结束
        setState(() {});
      },
      onTapDown: (TapDownDetails details) {
        print('onTapDown');
      },
      onTapUp: (TapUpDetails details) {
        print('onTapUp');
        double _bubbleY = 0;
        bool _bubbleHidden = true;
        if (widget.onSelect != null) {
          widget.onSelect(
              _itemList[_selectIndex < 0
                  ? 0
                  : (_selectIndex > (_itemList.length - 1)
                      ? _itemList.length - 1
                      : _selectIndex)],
              _bubbleY,
              _bubbleHidden);
        } //触摸结束
        setState(() {});
      },

4、选中字母列表某个item的回调:

int index = _filterList.indexWhere((element) => element == value);
                  if (index > -1){
                    double max =  _scrollController.position.maxScrollExtent;
                    double offset = _locList[index];
                    if (offset > max){
                      _scrollController.jumpTo(max);
                    }else{
                      _scrollController.jumpTo(offset);
                    }
                  }

5、整体页面结构

@override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    return Stack(
      children: [
        Positioned(
            left: 0,
            right: 0,
            top: 0,
            bottom: 0,
            child: SingleChildScrollView(
              controller: _scrollController,
              child: Column(
                children: _getItemList(),
              ),
            )
        ),
        Positioned(
            top: height/8,
            height: height/2,
            right: 0,
            child: IndexItem(
              key: _indexKey,
              onSelect: (String value,double offset,bool type) {
              
                if (type == false){
                  int index = _filterList.indexWhere((element) => element == value);
                  if (index > -1){
                    double max =  _scrollController.position.maxScrollExtent;
                    double offset = _locList[index];
                    if (offset > max){
                      _scrollController.jumpTo(max);
                    }else{
                      _scrollController.jumpTo(offset);
                    }
                  }
                }
              //悬浮气泡
                _bubbleKey.currentState.updateLoc(value,offset,type);
              },
            )),
        Bubble(
          key: _bubbleKey,
        )
      ],
    );
  }

注意:此处一定要用SingleChildScrollView,因为ListView会重新创建、布局、绘制可见区域内的item,一般会多绘制可见区域以外2-4个item,所以不在屏幕的item通过key获取的位置是0。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容