feat: search user by uid

This commit is contained in:
guozhigq
2024-10-26 16:31:01 +08:00
parent baa6a644e3
commit 4f6ac4aff4
4 changed files with 83 additions and 40 deletions

View File

@ -26,7 +26,6 @@ class SSearchController extends GetxController {
Box setting = GStrorage.setting;
bool enableHotKey = true;
bool enableSearchSuggest = true;
late StreamController<bool> clearStream = StreamController<bool>.broadcast();
@override
void onInit() {
@ -42,7 +41,6 @@ class SSearchController extends GetxController {
final hint = parameters['hintText'];
if (hint != null) {
hintText = hint;
searchKeyWord.value = hintText;
}
}
historyCacheList = GlobalDataCache().historyCacheList;
@ -55,10 +53,8 @@ class SSearchController extends GetxController {
searchKeyWord.value = value;
if (value == '') {
searchSuggestList.value = [];
clearStream.add(false);
return;
}
clearStream.add(true);
if (enableSearchSuggest) {
_debouncer.call(() => querySearchSuggest(value));
}
@ -68,23 +64,20 @@ class SSearchController extends GetxController {
controller.value.clear();
searchKeyWord.value = '';
searchSuggestList.value = [];
clearStream.add(false);
}
// 搜索
void submit() {
if (searchKeyWord.value == '') {
if (searchKeyWord.value == '' && hintText.isNotEmpty && hintText == '搜索') {
return;
} else {
if (searchKeyWord.value == '' && hintText != '搜索') {
searchKeyWord.value = hintText;
controller.value.text = hintText;
}
}
List arr = historyCacheList.where((e) => e != searchKeyWord.value).toList();
arr.insert(0, searchKeyWord.value);
historyCacheList = arr;
historyList.value = historyCacheList;
// 手动刷新
historyList.refresh();
localCache.put('cacheList', historyCacheList);
searchFocusNode.unfocus();
hintText = '搜索';
cacheHistory();
Get.toNamed('/searchResult', parameters: {'keyword': searchKeyWord.value});
}
@ -135,6 +128,18 @@ class SSearchController extends GetxController {
historyCacheList = [];
historyList.refresh();
localCache.put('cacheList', []);
GlobalDataCache().historyCacheList = [];
SmartDialog.showToast('搜索历史已清空');
}
cacheHistory() {
List arr = historyCacheList.where((e) => e != searchKeyWord.value).toList();
arr.insert(0, searchKeyWord.value);
historyCacheList = arr;
historyList.value = historyCacheList;
historyList.refresh();
localCache.put('cacheList', historyCacheList);
GlobalDataCache().historyCacheList = historyCacheList;
searchFocusNode.unfocus();
}
}

View File

@ -63,24 +63,35 @@ class _SearchPageState extends State<SearchPage> with RouteAware {
focusNode: _searchController.searchFocusNode,
controller: _searchController.controller.value,
textInputAction: TextInputAction.search,
onChanged: (value) => _searchController.onChange(value),
onChanged: _searchController.onChange,
decoration: InputDecoration(
hintText: _searchController.hintText,
border: InputBorder.none,
suffixIcon: StreamBuilder(
initialData: false,
stream: _searchController.clearStream.stream,
builder: (_, snapshot) {
if (snapshot.data == true) {
return IconButton(
suffix: Obx(() {
RxString searchKeyWord = _searchController.searchKeyWord;
if (searchKeyWord.value.isEmpty) {
return const SizedBox();
}
return Row(
mainAxisSize: MainAxisSize.min,
children: [
if (RegExp(r'^\d+$').hasMatch(searchKeyWord.value))
IconButton(
tooltip: '直达up主页',
icon: const Icon(Icons.person_outline, size: 22),
onPressed: () {
_searchController.cacheHistory();
Get.toNamed('/member?mid=${searchKeyWord.value}',
arguments: {'face': null});
},
),
IconButton(
icon: const Icon(Icons.clear, size: 22),
onPressed: () => _searchController.onClear(),
);
} else {
return const SizedBox();
}
},
),
),
],
);
}),
),
onSubmitted: (String value) => _searchController.submit(),
),

View File

@ -1,7 +1,9 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pilipala/http/member.dart';
import 'package:pilipala/http/search.dart';
import 'package:pilipala/models/common/search_type.dart';
import 'package:pilipala/models/search/result.dart';
import 'package:pilipala/utils/id_utils.dart';
import 'package:pilipala/utils/utils.dart';
@ -39,6 +41,30 @@ class SearchPanelController extends GetxController {
page.value++;
onPushDetail(keyword, resultList);
}
if (RegExp(r'^\d+$').hasMatch(keyword!) &&
searchType == SearchType.bili_user) {
var res = await MemberHttp.memberInfo(mid: int.parse(keyword!));
if (res['status']) {
try {
final user = SearchUserItemModel(
mid: res['data'].mid,
uname: res['data'].name,
upic: res['data'].face,
level: res['data'].level,
fans: null,
videos: null,
officialVerify: res['data'].official,
);
if (resultList.isEmpty) {
resultList = [user].obs;
} else {
resultList.insert(0, user);
}
} catch (err) {
debugPrint('搜索用户信息失败: $err');
}
}
}
return result;
}

View File

@ -13,10 +13,10 @@ Widget searchUserPanel(BuildContext context, ctr, list) {
controller: ctr!.scrollController,
addAutomaticKeepAlives: false,
addRepaintBoundaries: false,
itemCount: list!.length,
itemCount: list.length,
itemBuilder: (context, index) {
var i = list![index];
String heroTag = Utils.makeHeroTag(i!.mid);
var i = list[index];
String heroTag = Utils.makeHeroTag(i.mid);
return InkWell(
onTap: () => Get.toNamed('/member?mid=${i.mid}',
arguments: {'heroTag': heroTag, 'face': i.upic}),
@ -43,7 +43,7 @@ Widget searchUserPanel(BuildContext context, ctr, list) {
Row(
children: [
Text(
i!.uname,
i.uname!,
style: const TextStyle(
fontSize: 14,
),
@ -55,15 +55,16 @@ Widget searchUserPanel(BuildContext context, ctr, list) {
),
],
),
Row(
children: [
Text('粉丝:${i.fans} ', style: style),
Text(' 视频${i.videos}', style: style)
],
),
if (i.officialVerify['desc'] != '')
if (i.fans != null && i.videos != null)
Row(
children: [
Text('粉丝${i.fans} ', style: style),
Text(' 视频:${i.videos}', style: style)
],
),
if (i.officialVerify!['desc'] != '')
Text(
i.officialVerify['desc'],
i.officialVerify!['desc'],
style: style,
),
],