From 48c6c27a243cd07cfff6c26b07da94a5ae603d59 Mon Sep 17 00:00:00 2001 From: guozhigq Date: Thu, 22 Jun 2023 22:41:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=90=9C=E7=B4=A2=E5=8E=86=E5=8F=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/http/init.dart | 4 +- lib/http/search.dart | 4 +- lib/pages/search/controller.dart | 37 +++++++++- lib/pages/search/view.dart | 90 ++++++++++++++++++----- lib/pages/search/widgets/search_text.dart | 34 +++++++++ lib/utils/storage.dart | 3 + 6 files changed, 145 insertions(+), 27 deletions(-) create mode 100644 lib/pages/search/widgets/search_text.dart diff --git a/lib/http/init.dart b/lib/http/init.dart index 240251dd..b75d0dc3 100644 --- a/lib/http/init.dart +++ b/lib/http/init.dart @@ -91,8 +91,8 @@ class Request { ..add(ApiInterceptor()) // 日志拦截器 输出请求、响应内容 ..add(LogInterceptor( - request: false, - requestHeader: false, + request: true, + requestHeader: true, responseHeader: false, )); dio.transformer = BackgroundTransformer(); diff --git a/lib/http/search.dart b/lib/http/search.dart index 2afa59df..953af190 100644 --- a/lib/http/search.dart +++ b/lib/http/search.dart @@ -51,8 +51,8 @@ class SearchHttp { var res = await Request().get(Api.searchByType, data: { 'search_type': searchType.type, 'keyword': keyword, - 'order_sort': 0, - 'user_type': 0, + // 'order_sort': 0, + // 'user_type': 0, 'page': page }); if (res.data['code'] == 0 && res.data['data']['numPages'] > 0) { diff --git a/lib/pages/search/controller.dart b/lib/pages/search/controller.dart index 7e4e0772..2f825209 100644 --- a/lib/pages/search/controller.dart +++ b/lib/pages/search/controller.dart @@ -5,7 +5,6 @@ import 'package:hive/hive.dart'; import 'package:pilipala/http/search.dart'; import 'package:pilipala/models/search/hot.dart'; import 'package:pilipala/models/search/suggest.dart'; -import 'package:pilipala/utils/id_utils.dart'; import 'package:pilipala/utils/storage.dart'; class SearchController extends GetxController { @@ -14,6 +13,9 @@ class SearchController extends GetxController { Rx controller = TextEditingController().obs; List hotSearchList = []; Box hotKeyword = GStrorage.hotKeyword; + Box histiryWord = GStrorage.historyword; + List historyCacheList = []; + RxList historyList = [].obs; RxList searchSuggestList = [SearchSuggestItem()].obs; final _debouncer = Debouncer(delay: const Duration(milliseconds: 200)); // 设置延迟时间 @@ -33,6 +35,8 @@ class SearchController extends GetxController { if (Get.parameters.keys.isNotEmpty) { onClickKeyword(Get.parameters['keyword']!); } + historyCacheList = histiryWord.get('cacheList') ?? []; + historyList.value = historyCacheList; } void onChange(value) { @@ -45,9 +49,13 @@ class SearchController extends GetxController { } void onClear() { - controller.value.clear(); - searchKeyWord.value = ''; - searchSuggestList.value = []; + if (searchKeyWord.value.isNotEmpty) { + controller.value.clear(); + searchKeyWord.value = ''; + searchSuggestList.value = []; + } else { + Get.back(); + } } // 搜索 @@ -56,6 +64,14 @@ class SearchController extends GetxController { if (searchKeyWord == '') { return; } + List arr = historyCacheList.where((e) => e != searchKeyWord.value).toList(); + arr.insert(0, searchKeyWord.value); + historyCacheList = arr; + + historyList.value = historyCacheList; + // 手动刷新 + historyList.refresh(); + histiryWord.put('cacheList', historyCacheList); Get.toNamed('/searchResult', parameters: {'keyword': searchKeyWord.value}); } @@ -84,4 +100,17 @@ class SearchController extends GetxController { searchSuggestList.value = result['data'].tag; } } + + onSelect(word) { + searchKeyWord.value = word; + controller.value.text = word; + submit(); + } + + onClearHis() { + historyList.value = []; + historyCacheList = []; + historyList.refresh(); + histiryWord.put('cacheList', []); + } } diff --git a/lib/pages/search/view.dart b/lib/pages/search/view.dart index 38277285..1e6f80fd 100644 --- a/lib/pages/search/view.dart +++ b/lib/pages/search/view.dart @@ -5,6 +5,7 @@ import 'package:get/get.dart'; import 'package:pilipala/common/widgets/http_error.dart'; import 'controller.dart'; import 'widgets/hotKeyword.dart'; +import 'widgets/search_text.dart'; class SearchPage extends StatefulWidget { const SearchPage({super.key}); @@ -63,28 +64,31 @@ class _SearchPageState extends State with RouteAware { decoration: InputDecoration( hintText: '搜索', border: InputBorder.none, - suffixIcon: _searchController.searchKeyWord.value.isNotEmpty - ? IconButton( - icon: Icon( - Icons.clear, - size: 22, - color: Theme.of(context).colorScheme.outline, - ), - onPressed: () => _searchController.onClear()) - : null, + suffixIcon: IconButton( + icon: Icon( + Icons.clear, + size: 22, + color: Theme.of(context).colorScheme.outline, + ), + onPressed: () => _searchController.onClear(), + ), ), onSubmitted: (String value) => _searchController.submit(), ), ), ), - body: Column( - children: [ - const SizedBox(height: 12), - // 搜索建议 - _searchSuggest(), - // 热搜 - hotSearch(), - ], + body: SingleChildScrollView( + child: Column( + children: [ + const SizedBox(height: 12), + // 搜索建议 + _searchSuggest(), + // 热搜 + hotSearch(), + // 搜索历史 + _history() + ], + ), ), ); } @@ -143,7 +147,7 @@ class _SearchPageState extends State with RouteAware { crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( - padding: const EdgeInsets.fromLTRB(6, 0, 0, 0), + padding: const EdgeInsets.fromLTRB(6, 0, 0, 6), child: Text( '大家都在搜', style: Theme.of(context) @@ -152,7 +156,6 @@ class _SearchPageState extends State with RouteAware { .copyWith(fontWeight: FontWeight.bold), ), ), - const SizedBox(height: 6), LayoutBuilder( builder: (context, boxConstraints) { final double width = boxConstraints.maxWidth; @@ -197,4 +200,53 @@ class _SearchPageState extends State with RouteAware { ), ); } + + Widget _history() { + return Obx( + () => Container( + width: double.infinity, + padding: const EdgeInsets.fromLTRB(10, 25, 4, 0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + if (_searchController.historyList.isNotEmpty) + Padding( + padding: const EdgeInsets.fromLTRB(6, 0, 1, 2), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + '搜索历史', + style: Theme.of(context) + .textTheme + .titleMedium! + .copyWith(fontWeight: FontWeight.bold), + ), + TextButton( + onPressed: () => _searchController.onClearHis(), + child: const Text('清空'), + ) + ], + ), + ), + // if (_searchController.historyList.isNotEmpty) + Wrap( + spacing: 8, + runSpacing: 8, + direction: Axis.horizontal, + textDirection: TextDirection.ltr, + children: [ + for (int i = 0; i < _searchController.historyList.length; i++) + SearchText( + searchText: _searchController.historyList[i], + searchTextIdx: i, + onSelect: (value) => _searchController.onSelect(value), + ) + ], + ), + ], + ), + ), + ); + } } diff --git a/lib/pages/search/widgets/search_text.dart b/lib/pages/search/widgets/search_text.dart new file mode 100644 index 00000000..4739d49c --- /dev/null +++ b/lib/pages/search/widgets/search_text.dart @@ -0,0 +1,34 @@ +import 'package:flutter/material.dart'; + +class SearchText extends StatelessWidget { + String? searchText; + Function? onSelect; + int? searchTextIdx; + SearchText({super.key, this.searchText, this.onSelect, this.searchTextIdx}); + + @override + Widget build(BuildContext context) { + return Material( + color: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.5), + borderRadius: BorderRadius.circular(6), + child: Padding( + padding: EdgeInsets.zero, + child: InkWell( + onTap: () { + onSelect!(searchText); + }, + borderRadius: BorderRadius.circular(6), + child: Padding( + padding: + const EdgeInsets.only(top: 5, bottom: 5, left: 11, right: 11), + child: Text( + searchText!, + style: TextStyle( + color: Theme.of(context).colorScheme.onSurfaceVariant), + ), + ), + ), + ), + ); + } +} diff --git a/lib/utils/storage.dart b/lib/utils/storage.dart index 089eada7..0bbd4136 100644 --- a/lib/utils/storage.dart +++ b/lib/utils/storage.dart @@ -11,6 +11,7 @@ class GStrorage { static late final Box recVideo; static late final Box userInfo; static late final Box hotKeyword; + static late final Box historyword; static Future init() async { final dir = await getApplicationDocumentsDirectory(); @@ -25,6 +26,8 @@ class GStrorage { userInfo = await Hive.openBox('userInfo'); // 热搜关键词 hotKeyword = await Hive.openBox('hotKeyword'); + // 搜索历史 + historyword = await Hive.openBox('historyWord'); } static regAdapter() {