contant_item_page.dart 8.24 KB
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:one_key/models/user_history_entity.dart';
import 'package:one_key/provider/contant_show_model.dart';
import 'package:one_key/res/colors.dart';
import 'package:one_key/utils/utils.dart';
import 'package:one_key/widgets/MyDivider.dart';
import 'package:one_key/widgets/v_empty_view.dart';

class ContantItemPage extends StatefulWidget {
  final int index;
  final ContantShowModel model;
  final GestureTapCallback onTap;
  final List<UserHistoryEntity> data;
  ContantItemPage({Key key, this.index, this.model, this.onTap, this.data})
      : super(key: key);

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

class _ContantItemPageState extends State<ContantItemPage> {
  _getWidget(String title, String key, {fontSize = 32}) {
    return RichText(
        text: TextSpan(
            children: getTextSpanList(title,
                searchContent: key, fontSize: fontSize)));
  }

  ///关键字在前面布局
  _startPlaceWidget(
      String title, String key, UserHistoryEntity userHistoryEntity,
      {fontSize = 32}) {
    String keyTemp = '';
    if (Utils.isChinese(key)) {
      keyTemp = key;
    } else {
      String name =
          userHistoryEntity.rname == null || userHistoryEntity.rname == ''
              ? ''
              : userHistoryEntity.rname;
      String pinyinFirst = userHistoryEntity.pinyinFirst;
      List<String> pinyinList = userHistoryEntity.pinyinList;
      String pinyin = userHistoryEntity.pinyin;
      // if (pinyinList.indexOf(key) != -1) {
      //   //全拼音检索一个字
      //   keyTemp = name.substring(
      //       pinyinList.indexOf(key), pinyinList.indexOf(key) + 1);
      // } else
      if (pinyinFirst.contains(key)) {
        keyTemp = name.substring(
            pinyinFirst.indexOf(key), pinyinFirst.indexOf(key) + key.length);
      } else if (pinyin.indexOf(key) != -1) {
        if (pinyinList.indexOf(key) != -1) {
//全拼音检索  lilao
          keyTemp = name.substring(0, pinyinList.indexOf(key) + 1);
        } else if (pinyinFirst.contains(key)) {
          keyTemp = name.substring(
              pinyinFirst.indexOf(key), pinyinFirst.indexOf(key) + key.length);
        }
      }

      print(keyTemp);
    }
    return _getWidget(title, keyTemp, fontSize: fontSize);
  }

  List<TextSpan> getTextSpanList(String textContent,
      {String searchContent = '',
      String frontContent = '',
      int fontSize = 32,
      Color fontColor = MyColours.color_333333,
      Color selectFontColor = MyColours.color_199095}) {
    List<TextSpan> textSpanList = [];

    if (frontContent.isEmpty == false) {
      textSpanList.add(TextSpan(
          text: frontContent,
          style: TextStyle(
              fontSize: ScreenUtil().setSp(fontSize), color: fontColor)));
    }

    ///搜索内容不为空并且 显示内容中存在与搜索内容相同的文字
    if (searchContent != null &&
        searchContent.isEmpty == false &&
        textContent.contains(searchContent)) {
      List<Map> _strMapList = [];
      bool _isContains = true;
      while (_isContains) {
        int startIndex = textContent.indexOf(searchContent);
        String showStr = textContent.substring(
            startIndex, startIndex + searchContent.length);
        Map _strMap;
        if (startIndex > 0) {
          String normalStr = textContent.substring(0, startIndex);
          _strMap = Map();
          _strMap['content'] = normalStr;
          _strMap['isHighlight'] = false;
          _strMapList.add(_strMap);
        }
        _strMap = Map();
        _strMap['content'] = showStr;
        _strMap['isHighlight'] = true;
        _strMapList.add(_strMap);
        textContent = textContent.substring(
            startIndex + searchContent.length, textContent.length);

        _isContains = textContent.contains(searchContent);
        if (!_isContains && textContent != '') {
          _strMap = Map();
          _strMap['content'] = textContent;
          _strMap['isHighlight'] = false;
          _strMapList.add(_strMap);
        }
      }
      _strMapList.forEach((map) {
        textSpanList.add(TextSpan(
            text: map['content'],
            style: TextStyle(
                fontSize: ScreenUtil().setSp(fontSize),
                color: map['isHighlight'] ? selectFontColor : fontColor)));
      });
    } else {
      ///正常显示所有文字
      textSpanList.add(TextSpan(
        text: textContent,
        style:
            TextStyle(fontSize: ScreenUtil().setSp(fontSize), color: fontColor),
      ));
    }
    return textSpanList;
  }

  ///姓名widget
  Widget _getNameWidget(UserHistoryEntity userHistoryEntity) {
    String name =
        userHistoryEntity.rname == null || userHistoryEntity.rname == ''
            ? ''
            : userHistoryEntity.rname;

    String key = widget.model.controller.text;
    String pinyinFirst = userHistoryEntity.pinyinFirst;
    List<String> pinyinList = userHistoryEntity.pinyinList;
    if (key == '') {
      return Text(name,
          style: TextStyle(
              color: MyColours.color_333333, fontSize: ScreenUtil().setSp(32)));
    } else {
      return (name.contains(key) && name.length != key.length) ||
              (pinyinFirst.contains(key) && pinyinFirst.length != key.length) ||
              (pinyinList.contains(key) && pinyinList.length != key.length)
          ? _startPlaceWidget(name, key, userHistoryEntity)
          : Text(name,
              style: TextStyle(
                  color: ((name.contains(key) && name.length == key.length) ||
                          (pinyinFirst.contains(key) &&
                              pinyinFirst.length == key.length) ||
                          (pinyinList.contains(key) &&
                              pinyinList.length == key.length))
                      ? MyColours.color_199095
                      : MyColours.color_333333,
                  fontSize: ScreenUtil().setSp(32)));
    }
  }

  ///电话widget
  Widget _getPhoneWidget(UserHistoryEntity userHistoryEntity) {
    String phone =
        userHistoryEntity.phone == null || userHistoryEntity.phone == ''
            ? ''
            : userHistoryEntity.phone;

    String key = widget.model.controller.text;

    if (key == '') {
      return Text(phone,
          style: TextStyle(
              color: MyColours.color_333333, fontSize: ScreenUtil().setSp(32)));
    } else {
      return (phone.contains(key) && phone.length != key.length)
          ? _getWidget(phone, key, fontSize: 30)
          : Text(phone,
              style: TextStyle(
                  color: phone.contains(key) && phone.length == key.length
                      ? MyColours.color_199095
                      : MyColours.color_333333,
                  fontSize: ScreenUtil().setSp(30)));
    }
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
        onTap: widget.onTap,
        child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          widget.index == 0
              ? MyDivider(
                  double.infinity,
                  ScreenUtil().setWidth(20),
                  color: MyColours.color_f5f5f5,
                )
              : Container(),
          Container(
            width: double.infinity,
            padding: EdgeInsets.symmetric(
                horizontal: ScreenUtil().setWidth(40),
                vertical: ScreenUtil().setWidth(30)),
            // height: ScreenUtil().setWidth(76 * 2),
            decoration: BoxDecoration(
                border: Border(
                    top: widget.index == 0
                        ? BorderSide(
                            width: ScreenUtil().setWidth(2),
                            color: MyColours.color_E0E0E0)
                        : BorderSide(width: 0, color: Colors.transparent),
                    bottom: BorderSide(
                        width: ScreenUtil().setWidth(2),
                        color: MyColours.color_E0E0E0))),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _getNameWidget(widget.data[widget.index]),
                VEmptyView(10),
                _getPhoneWidget(widget.data[widget.index])
              ],
            ),
          )
        ]));
  }
}