Merge branch 'fix'
This commit is contained in:
@ -1,3 +1,6 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:get/get.dart';
|
||||||
|
|
||||||
class SearchSuggestModel {
|
class SearchSuggestModel {
|
||||||
SearchSuggestModel({
|
SearchSuggestModel({
|
||||||
this.tag,
|
this.tag,
|
||||||
@ -19,32 +22,74 @@ class SearchSuggestItem {
|
|||||||
SearchSuggestItem({
|
SearchSuggestItem({
|
||||||
this.value,
|
this.value,
|
||||||
this.term,
|
this.term,
|
||||||
this.name,
|
|
||||||
this.spid,
|
this.spid,
|
||||||
|
this.textRich,
|
||||||
});
|
});
|
||||||
|
|
||||||
String? value;
|
String? value;
|
||||||
String? term;
|
String? term;
|
||||||
List? name;
|
|
||||||
int? spid;
|
int? spid;
|
||||||
|
Widget? textRich;
|
||||||
|
|
||||||
SearchSuggestItem.fromJson(Map<String, dynamic> json, String inputTerm) {
|
SearchSuggestItem.fromJson(Map<String, dynamic> json, String inputTerm) {
|
||||||
value = json['value'];
|
value = json['value'];
|
||||||
term = json['term'];
|
term = json['term'];
|
||||||
String reg = '<em class="suggest_high_light">$inputTerm</em>';
|
textRich = highlightText(json['name']);
|
||||||
try {
|
|
||||||
if (json['name'].indexOf(inputTerm) != -1) {
|
|
||||||
String str = json['name'].replaceAll(reg, '^');
|
|
||||||
List arr = str.split('^');
|
|
||||||
arr.insert(arr.length - 1, inputTerm);
|
|
||||||
name = arr;
|
|
||||||
} else {
|
|
||||||
name = ['', '', json['term']];
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
name = ['', '', json['term']];
|
|
||||||
}
|
|
||||||
|
|
||||||
spid = json['spid'];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget highlightText(String str) {
|
||||||
|
// 创建正则表达式,匹配 <em class="suggest_high_light">...</em> 格式的文本
|
||||||
|
RegExp regex = RegExp(r'<em class="suggest_high_light">(.*?)<\/em>');
|
||||||
|
|
||||||
|
// 用于存储每个匹配项的列表
|
||||||
|
List<InlineSpan> children = [];
|
||||||
|
|
||||||
|
// 获取所有匹配项
|
||||||
|
Iterable<Match> matches = regex.allMatches(str);
|
||||||
|
|
||||||
|
// 当前索引位置
|
||||||
|
int currentIndex = 0;
|
||||||
|
|
||||||
|
// 遍历每个匹配项
|
||||||
|
for (var match in matches) {
|
||||||
|
// 获取当前匹配项之前的普通文本部分
|
||||||
|
String normalText = str.substring(currentIndex, match.start);
|
||||||
|
|
||||||
|
// 获取需要高亮显示的文本部分
|
||||||
|
String highlightedText = match.group(1)!;
|
||||||
|
|
||||||
|
// 如果普通文本部分不为空,则将其添加到 children 列表中
|
||||||
|
if (normalText.isNotEmpty) {
|
||||||
|
children.add(TextSpan(
|
||||||
|
text: normalText,
|
||||||
|
style: DefaultTextStyle.of(Get.context!).style,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将需要高亮显示的文本部分添加到 children 列表中,并设置相应样式
|
||||||
|
children.add(TextSpan(
|
||||||
|
text: highlightedText,
|
||||||
|
style: TextStyle(
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
color: Theme.of(Get.context!).colorScheme.primary),
|
||||||
|
));
|
||||||
|
|
||||||
|
// 更新当前索引位置
|
||||||
|
currentIndex = match.end;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果当前索引位置小于文本长度,表示还有剩余的普通文本部分
|
||||||
|
if (currentIndex < str.length) {
|
||||||
|
String remainingText = str.substring(currentIndex);
|
||||||
|
|
||||||
|
// 将剩余的普通文本部分添加到 children 列表中
|
||||||
|
children.add(TextSpan(
|
||||||
|
text: remainingText,
|
||||||
|
style: DefaultTextStyle.of(Get.context!).style,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用 Text.rich 创建包含高亮显示的富文本小部件,并返回
|
||||||
|
return Text.rich(TextSpan(children: children));
|
||||||
|
}
|
||||||
|
@ -45,6 +45,10 @@ class _SearchPageState extends State<SearchPage> with RouteAware {
|
|||||||
return OpenContainer(
|
return OpenContainer(
|
||||||
closedElevation: 0,
|
closedElevation: 0,
|
||||||
openElevation: 0,
|
openElevation: 0,
|
||||||
|
onClosed: (_) {
|
||||||
|
// 在 openBuilder 关闭时触发的回调函数
|
||||||
|
_searchController.onClear();
|
||||||
|
},
|
||||||
openColor: Theme.of(context).colorScheme.background,
|
openColor: Theme.of(context).colorScheme.background,
|
||||||
middleColor: Theme.of(context).colorScheme.background,
|
middleColor: Theme.of(context).colorScheme.background,
|
||||||
closedColor: Theme.of(context).colorScheme.background,
|
closedColor: Theme.of(context).colorScheme.background,
|
||||||
@ -176,25 +180,7 @@ class _SearchPageState extends State<SearchPage> with RouteAware {
|
|||||||
// child: Text(
|
// child: Text(
|
||||||
// _searchController.searchSuggestList[index].term!,
|
// _searchController.searchSuggestList[index].term!,
|
||||||
// ),
|
// ),
|
||||||
child: Text.rich(
|
child: _searchController.searchSuggestList[index].textRich,
|
||||||
TextSpan(
|
|
||||||
children: [
|
|
||||||
TextSpan(
|
|
||||||
text: _searchController
|
|
||||||
.searchSuggestList[index].name![0]),
|
|
||||||
TextSpan(
|
|
||||||
text: _searchController
|
|
||||||
.searchSuggestList[index].name![1],
|
|
||||||
style: TextStyle(
|
|
||||||
color: Theme.of(context).colorScheme.primary,
|
|
||||||
fontWeight: FontWeight.bold),
|
|
||||||
),
|
|
||||||
TextSpan(
|
|
||||||
text: _searchController
|
|
||||||
.searchSuggestList[index].name![2]),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user