feat: 搜索计数
This commit is contained in:
@ -1,8 +1,11 @@
|
||||
import 'package:get/get.dart';
|
||||
import 'package:pilipala/http/search.dart';
|
||||
import 'package:pilipala/models/common/search_type.dart';
|
||||
|
||||
class SearchResultController extends GetxController {
|
||||
String? keyword;
|
||||
int tabIndex = 0;
|
||||
RxList searchTabs = [].obs;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
@ -10,5 +13,21 @@ class SearchResultController extends GetxController {
|
||||
if (Get.parameters.keys.isNotEmpty) {
|
||||
keyword = Get.parameters['keyword'];
|
||||
}
|
||||
searchTabs.value = SearchType.values
|
||||
.map((type) => {'label': type.label, 'id': type.type})
|
||||
.toList();
|
||||
querySearchCount();
|
||||
}
|
||||
|
||||
Future querySearchCount() async {
|
||||
var result = await SearchHttp.searchCount(keyword: keyword!);
|
||||
if (result['status']) {
|
||||
for (var i in searchTabs) {
|
||||
final count = result['data'].topTList[i['id']];
|
||||
i['count'] = count > 99 ? '99+' : count.toString();
|
||||
}
|
||||
searchTabs.refresh();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,7 +13,7 @@ class SearchResultPage extends StatefulWidget {
|
||||
|
||||
class _SearchResultPageState extends State<SearchResultPage>
|
||||
with TickerProviderStateMixin {
|
||||
late SearchResultController? _searchResultController;
|
||||
late SearchResultController _searchResultController;
|
||||
late TabController? _tabController;
|
||||
|
||||
@override
|
||||
@ -25,7 +25,7 @@ class _SearchResultPageState extends State<SearchResultPage>
|
||||
_tabController = TabController(
|
||||
vsync: this,
|
||||
length: SearchType.values.length,
|
||||
initialIndex: _searchResultController!.tabIndex,
|
||||
initialIndex: _searchResultController.tabIndex,
|
||||
);
|
||||
}
|
||||
|
||||
@ -46,7 +46,7 @@ class _SearchResultPageState extends State<SearchResultPage>
|
||||
child: SizedBox(
|
||||
width: double.infinity,
|
||||
child: Text(
|
||||
'${_searchResultController!.keyword}',
|
||||
'${_searchResultController.keyword}',
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
),
|
||||
@ -64,35 +64,39 @@ class _SearchResultPageState extends State<SearchResultPage>
|
||||
splashColor: Colors.transparent, // 点击时的水波纹颜色设置为透明
|
||||
highlightColor: Colors.transparent, // 点击时的背景高亮颜色设置为透明
|
||||
),
|
||||
child: TabBar(
|
||||
controller: _tabController,
|
||||
tabs: [
|
||||
for (var i in SearchType.values) Tab(text: i.label),
|
||||
],
|
||||
isScrollable: true,
|
||||
indicatorWeight: 0,
|
||||
indicatorPadding:
|
||||
const EdgeInsets.symmetric(horizontal: 3, vertical: 8),
|
||||
indicator: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.secondaryContainer,
|
||||
borderRadius: const BorderRadius.all(Radius.circular(20)),
|
||||
),
|
||||
indicatorSize: TabBarIndicatorSize.tab,
|
||||
labelColor: Theme.of(context).colorScheme.onSecondaryContainer,
|
||||
labelStyle: const TextStyle(fontSize: 13),
|
||||
dividerColor: Colors.transparent,
|
||||
unselectedLabelColor: Theme.of(context).colorScheme.outline,
|
||||
tabAlignment: TabAlignment.start,
|
||||
onTap: (index) {
|
||||
if (index == _searchResultController!.tabIndex) {
|
||||
Get.find<SearchPanelController>(
|
||||
tag: SearchType.values[index].type +
|
||||
_searchResultController!.keyword!)
|
||||
.animateToTop();
|
||||
}
|
||||
child: Obx(
|
||||
() => (TabBar(
|
||||
controller: _tabController,
|
||||
tabs: [
|
||||
for (var i in _searchResultController.searchTabs)
|
||||
Tab(text: "${i['label']} ${i['count'] ?? ''}")
|
||||
],
|
||||
isScrollable: true,
|
||||
indicatorWeight: 0,
|
||||
indicatorPadding:
|
||||
const EdgeInsets.symmetric(horizontal: 3, vertical: 8),
|
||||
indicator: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.secondaryContainer,
|
||||
borderRadius: const BorderRadius.all(Radius.circular(20)),
|
||||
),
|
||||
indicatorSize: TabBarIndicatorSize.tab,
|
||||
labelColor:
|
||||
Theme.of(context).colorScheme.onSecondaryContainer,
|
||||
labelStyle: const TextStyle(fontSize: 13),
|
||||
dividerColor: Colors.transparent,
|
||||
unselectedLabelColor: Theme.of(context).colorScheme.outline,
|
||||
tabAlignment: TabAlignment.start,
|
||||
onTap: (index) {
|
||||
if (index == _searchResultController.tabIndex) {
|
||||
Get.find<SearchPanelController>(
|
||||
tag: SearchType.values[index].type +
|
||||
_searchResultController.keyword!)
|
||||
.animateToTop();
|
||||
}
|
||||
|
||||
_searchResultController!.tabIndex = index;
|
||||
},
|
||||
_searchResultController.tabIndex = index;
|
||||
},
|
||||
)),
|
||||
),
|
||||
),
|
||||
),
|
||||
@ -102,7 +106,7 @@ class _SearchResultPageState extends State<SearchResultPage>
|
||||
children: [
|
||||
for (var i in SearchType.values) ...{
|
||||
SearchPanel(
|
||||
keyword: _searchResultController!.keyword,
|
||||
keyword: _searchResultController.keyword,
|
||||
searchType: i,
|
||||
tag: DateTime.now().millisecondsSinceEpoch.toString(),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user