contant_item_page.dart
8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/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])
],
),
)
]));
}
}