From 4e15422d2d0a0164cca69dbd8643a1956e499a2e Mon Sep 17 00:00:00 2001 From: My-Responsitories <107370289+My-Responsitories@users.noreply.github.com> Date: Wed, 1 Oct 2025 21:29:08 +0800 Subject: [PATCH] fix: search (#1412) --- lib/pages/search/controller.dart | 58 ++++++++++++++++--------- lib/pages/search/view.dart | 5 +-- lib/pages/search_result/controller.dart | 2 +- lib/pages/search_trending/view.dart | 5 +-- 4 files changed, 41 insertions(+), 29 deletions(-) diff --git a/lib/pages/search/controller.dart b/lib/pages/search/controller.dart index f223be39..7415388d 100644 --- a/lib/pages/search/controller.dart +++ b/lib/pages/search/controller.dart @@ -50,6 +50,34 @@ abstract class DebounceStreamState extends State } } +class BaseSearchController extends GetxController { + final historyList = List.from( + GStorage.historyWord.get('cacheList') ?? [], + ).obs; + + late final Rx> trendingState; + + final recordSearchHistory = Pref.recordSearchHistory.obs; + final searchSuggestion = Pref.searchSuggestion; + final enableTrending = Pref.enableTrending; + final enableSearchRcmd = Pref.enableSearchRcmd; + + @override + void onInit() { + super.onInit(); + + if (enableTrending) { + trendingState = LoadingState.loading().obs; + queryTrendingList(); + } + } + + // 获取热搜关键词 + Future queryTrendingList() async { + trendingState.value = await SearchHttp.searchTrending(limit: 10); + } +} + class SSearchController extends GetxController with DebounceStreamMixin { SSearchController(this.tag); @@ -57,6 +85,7 @@ class SSearchController extends GetxController final searchFocusNode = FocusNode(); final controller = TextEditingController(); + final _baseCtr = Get.putOrFind(BaseSearchController.new); String? hintText; @@ -66,21 +95,24 @@ class SSearchController extends GetxController final RxBool showUidBtn = false.obs; // history - final RxBool recordSearchHistory = Pref.recordSearchHistory.obs; - late final RxList historyList; + RxBool get recordSearchHistory => _baseCtr.recordSearchHistory; + RxList get historyList => _baseCtr.historyList; // suggestion - final bool searchSuggestion = Pref.searchSuggestion; + bool get searchSuggestion => _baseCtr.searchSuggestion; late final RxList searchSuggestList; // trending - final bool enableTrending = Pref.enableTrending; - late final Rx> trendingState; + bool get enableTrending => _baseCtr.enableTrending; + Rx> get trendingState => + _baseCtr.trendingState; // rcmd - final bool enableSearchRcmd = Pref.enableSearchRcmd; + bool get enableSearchRcmd => _baseCtr.enableSearchRcmd; late final Rx> recommendData; + Future Function() get queryTrendingList => _baseCtr.queryTrendingList; + @override void onInit() { super.onInit(); @@ -91,20 +123,11 @@ class SSearchController extends GetxController controller.text = text; } - historyList = List.from( - GStorage.historyWord.get('cacheList') ?? [], - ).obs; - if (searchSuggestion) { subInit(); searchSuggestList = [].obs; } - if (enableTrending) { - trendingState = LoadingState.loading().obs; - queryTrendingList(); - } - if (enableSearchRcmd) { recommendData = LoadingState.loading().obs; queryRecommendList(); @@ -176,11 +199,6 @@ class SSearchController extends GetxController } } - // 获取热搜关键词 - Future queryTrendingList() async { - trendingState.value = await SearchHttp.searchTrending(limit: 10); - } - Future queryRecommendList() async { recommendData.value = await SearchHttp.searchRecommend(); } diff --git a/lib/pages/search/view.dart b/lib/pages/search/view.dart index c51584d7..62baec83 100644 --- a/lib/pages/search/view.dart +++ b/lib/pages/search/view.dart @@ -203,10 +203,7 @@ class _SearchPageState extends State { SizedBox( height: 34, child: TextButton( - onPressed: () => Get.toNamed( - '/searchTrending', - parameters: {'tag': _tag}, - ), + onPressed: () => Get.toNamed('/searchTrending'), child: Row( children: [ Text( diff --git a/lib/pages/search_result/controller.dart b/lib/pages/search_result/controller.dart index 3bf67368..41860ff3 100644 --- a/lib/pages/search_result/controller.dart +++ b/lib/pages/search_result/controller.dart @@ -4,7 +4,7 @@ import 'package:get/get.dart'; class SearchResultController extends GetxController { String keyword = Get.parameters['keyword'] ?? ''; - RxList count = List.generate(SearchType.values.length, (_) => -1).obs; + RxList count = List.filled(SearchType.values.length, -1).obs; RxInt toTopIndex = (-1).obs; diff --git a/lib/pages/search_trending/view.dart b/lib/pages/search_trending/view.dart index ca8e1cbe..447ff4b0 100644 --- a/lib/pages/search_trending/view.dart +++ b/lib/pages/search_trending/view.dart @@ -24,10 +24,7 @@ class SearchTrendingPage extends StatefulWidget { } class _SearchTrendingPageState extends State { - final _controller = Get.put( - SearchTrendingController(), - tag: Get.parameters['tag'], - ); + final _controller = Get.putOrFind(SearchTrendingController.new); late double _offset; final RxDouble _scrollRatio = 0.0.obs;