opt: search page

This commit is contained in:
guozhigq
2024-10-19 13:29:18 +08:00
parent fa79b92f82
commit c755e8a60f
6 changed files with 138 additions and 95 deletions

View File

@ -1,10 +1,13 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/src/rx_workers/utils/debouncer.dart';
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/global_data_cache.dart';
import 'package:pilipala/utils/storage.dart';
class SSearchController extends GetxController {
@ -12,7 +15,7 @@ class SSearchController extends GetxController {
RxString searchKeyWord = ''.obs;
Rx<TextEditingController> controller = TextEditingController().obs;
RxList<HotSearchItem> hotSearchList = <HotSearchItem>[].obs;
Box histiryWord = GStrorage.historyword;
Box localCache = GStrorage.localCache;
List historyCacheList = [];
RxList historyList = [].obs;
RxList<SearchSuggestItem> searchSuggestList = <SearchSuggestItem>[].obs;
@ -22,21 +25,26 @@ class SSearchController extends GetxController {
RxString defaultSearch = ''.obs;
Box setting = GStrorage.setting;
bool enableHotKey = true;
late StreamController<bool> clearStream = StreamController<bool>.broadcast();
@override
void onInit() {
super.onInit();
// 其他页面跳转过来
if (Get.parameters.keys.isNotEmpty) {
if (Get.parameters['keyword'] != null) {
onClickKeyword(Get.parameters['keyword']!);
final parameters = Get.parameters;
if (parameters.keys.isNotEmpty) {
final keyword = parameters['keyword'];
if (keyword != null) {
onClickKeyword(keyword);
}
if (Get.parameters['hintText'] != null) {
hintText = Get.parameters['hintText']!;
final hint = parameters['hintText'];
if (hint != null) {
hintText = hint;
searchKeyWord.value = hintText;
}
}
historyCacheList = histiryWord.get('cacheList') ?? [];
historyCacheList = GlobalDataCache().historyCacheList;
historyList.value = historyCacheList;
enableHotKey = setting.get(SettingBoxKey.enableHotKey, defaultValue: true);
}
@ -45,8 +53,10 @@ class SSearchController extends GetxController {
searchKeyWord.value = value;
if (value == '') {
searchSuggestList.value = [];
clearStream.add(false);
return;
}
clearStream.add(true);
_debouncer.call(() => querySearchSuggest(value));
}
@ -55,6 +65,7 @@ class SSearchController extends GetxController {
controller.value.clear();
searchKeyWord.value = '';
searchSuggestList.value = [];
clearStream.add(false);
} else {
Get.back();
}
@ -62,8 +73,7 @@ class SSearchController extends GetxController {
// 搜索
void submit() {
// ignore: unrelated_type_equality_checks
if (searchKeyWord == '') {
if (searchKeyWord.value == '') {
return;
}
List arr = historyCacheList.where((e) => e != searchKeyWord.value).toList();
@ -73,7 +83,7 @@ class SSearchController extends GetxController {
historyList.value = historyCacheList;
// 手动刷新
historyList.refresh();
histiryWord.put('cacheList', historyCacheList);
localCache.put('cacheList', historyCacheList);
searchFocusNode.unfocus();
Get.toNamed('/searchResult', parameters: {'keyword': searchKeyWord.value});
}
@ -117,13 +127,14 @@ class SSearchController extends GetxController {
int index = historyList.indexOf(word);
historyList.removeAt(index);
historyList.refresh();
histiryWord.put('cacheList', historyList);
localCache.put('cacheList', historyList);
}
onClearHis() {
historyList.value = [];
historyCacheList = [];
historyList.refresh();
histiryWord.put('cacheList', []);
localCache.put('cacheList', []);
SmartDialog.showToast('搜索历史已清空');
}
}

View File

@ -1,4 +1,3 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pilipala/common/widgets/http_error.dart';
@ -54,7 +53,7 @@ class _SearchPageState extends State<SearchPage> with RouteAware {
actions: [
IconButton(
onPressed: () => _searchController.submit(),
icon: const Icon(CupertinoIcons.search, size: 22),
icon: const Icon(Icons.search),
),
const SizedBox(width: 10)
],
@ -68,13 +67,19 @@ class _SearchPageState extends State<SearchPage> with RouteAware {
decoration: InputDecoration(
hintText: _searchController.hintText,
border: InputBorder.none,
suffixIcon: IconButton(
icon: Icon(
Icons.clear,
size: 22,
color: Theme.of(context).colorScheme.outline,
),
onPressed: () => _searchController.onClear(),
suffixIcon: StreamBuilder(
initialData: false,
stream: _searchController.clearStream.stream,
builder: (_, snapshot) {
if (snapshot.data == true) {
return IconButton(
icon: const Icon(Icons.clear, size: 22),
onPressed: () => _searchController.onClear(),
);
} else {
return const SizedBox();
}
},
),
),
onSubmitted: (String value) => _searchController.submit(),
@ -84,7 +89,7 @@ class _SearchPageState extends State<SearchPage> with RouteAware {
body: SingleChildScrollView(
child: Column(
children: [
const SizedBox(height: 12),
const SizedBox(height: 6),
// 搜索建议
_searchSuggest(),
// 热搜
@ -135,7 +140,7 @@ class _SearchPageState extends State<SearchPage> with RouteAware {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(6, 0, 6, 6),
padding: const EdgeInsets.fromLTRB(6, 0, 6, 4),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
@ -153,7 +158,7 @@ class _SearchPageState extends State<SearchPage> with RouteAware {
padding: MaterialStateProperty.all(const EdgeInsets.only(
left: 10, top: 6, bottom: 6, right: 10)),
),
onPressed: () => ctr.queryHotSearchList(),
onPressed: ctr.queryHotSearchList,
icon: const Icon(Icons.refresh_outlined, size: 18),
label: const Text('刷新'),
),
@ -199,6 +204,7 @@ class _SearchPageState extends State<SearchPage> with RouteAware {
return HotKeyword(
width: width,
hotSearchList: _searchController.hotSearchList,
onClick: () {},
);
} else {
return const SizedBox();
@ -217,13 +223,13 @@ class _SearchPageState extends State<SearchPage> with RouteAware {
return Obx(
() => Container(
width: double.infinity,
padding: const EdgeInsets.fromLTRB(10, 25, 6, 0),
padding: const EdgeInsets.fromLTRB(10, 20, 4, 0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (_searchController.historyList.isNotEmpty)
Padding(
padding: const EdgeInsets.fromLTRB(6, 0, 0, 2),
padding: const EdgeInsets.fromLTRB(6, 0, 6, 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
@ -234,10 +240,19 @@ class _SearchPageState extends State<SearchPage> with RouteAware {
.titleMedium!
.copyWith(fontWeight: FontWeight.bold),
),
TextButton(
onPressed: () => _searchController.onClearHis(),
child: const Text('清空'),
)
SizedBox(
height: 34,
child: TextButton.icon(
style: ButtonStyle(
padding: MaterialStateProperty.all(
const EdgeInsets.only(
left: 10, top: 6, bottom: 6, right: 10)),
),
onPressed: _searchController.onClearHis,
icon: const Icon(Icons.clear_all_outlined, size: 18),
label: const Text('清空'),
),
),
],
),
),

View File

@ -1,15 +1,15 @@
// ignore: file_names
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
class HotKeyword extends StatelessWidget {
final double? width;
final List? hotSearchList;
final Function? onClick;
final double width;
final List hotSearchList;
final Function onClick;
const HotKeyword({
this.width,
this.hotSearchList,
this.onClick,
required this.width,
required this.hotSearchList,
required this.onClick,
super.key,
});
@ -18,45 +18,67 @@ class HotKeyword extends StatelessWidget {
return Wrap(
runSpacing: 0.4,
spacing: 5.0,
children: [
for (var i in hotSearchList!)
SizedBox(
width: width! / 2 - 4,
child: Material(
borderRadius: BorderRadius.circular(3),
clipBehavior: Clip.hardEdge,
child: InkWell(
onTap: () => onClick!(i.keyword),
child: Padding(
padding: EdgeInsets.only(
left: 2,
right: hotSearchList!.indexOf(i) % 2 == 1 ? 10 : 0),
child: Row(
children: [
Flexible(
child: Padding(
padding: const EdgeInsets.fromLTRB(6, 5, 4, 5),
child: Text(
i.keyword!,
overflow: TextOverflow.ellipsis,
maxLines: 1,
style: const TextStyle(fontSize: 14),
),
),
),
if (i.icon != null && i.icon != '')
SizedBox(
height: 15,
child: CachedNetworkImage(
imageUrl: i.icon!, height: 15.0),
),
],
),
),
),
),
),
],
children: hotSearchList.map((item) {
return HotKeywordItem(
width: width,
item: item,
onClick: onClick,
isRightPadding: hotSearchList.indexOf(item) % 2 == 1,
);
}).toList(),
);
}
}
class HotKeywordItem extends StatelessWidget {
final double width;
final dynamic item;
final Function onClick;
final bool isRightPadding;
const HotKeywordItem({
required this.width,
required this.item,
required this.onClick,
required this.isRightPadding,
super.key,
});
@override
Widget build(BuildContext context) {
return SizedBox(
width: width / 2 - 4,
child: Material(
borderRadius: BorderRadius.circular(4),
clipBehavior: Clip.hardEdge,
child: InkWell(
onTap: () => onClick.call(item.keyword),
child: Padding(
padding: EdgeInsets.only(left: 2, right: isRightPadding ? 10 : 0),
child: Row(
children: [
Flexible(
child: Padding(
padding: const EdgeInsets.fromLTRB(6, 5, 4, 5),
child: Text(
item.keyword,
overflow: TextOverflow.ellipsis,
maxLines: 1,
style: const TextStyle(fontSize: 14),
),
),
),
if (item.icon != null && item.icon != '')
SizedBox(
height: 15,
child:
CachedNetworkImage(imageUrl: item.icon!, height: 15.0),
),
],
),
),
),
),
);
}
}

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:pilipala/utils/feed_back.dart';
class SearchText extends StatelessWidget {
final String? searchText;
@ -17,30 +18,31 @@ class SearchText extends StatelessWidget {
@override
Widget build(BuildContext context) {
final ColorScheme colorScheme = Theme.of(context).colorScheme;
return Material(
color: isSelect
? Theme.of(context).colorScheme.primaryContainer
: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.5),
? colorScheme.primaryContainer
: colorScheme.surfaceVariant.withOpacity(0.5),
borderRadius: BorderRadius.circular(6),
child: Padding(
padding: EdgeInsets.zero,
child: InkWell(
onTap: () {
onSelect!(searchText);
onSelect?.call(searchText);
},
onLongPress: () {
onLongSelect!(searchText);
feedBack();
onLongSelect?.call(searchText);
},
borderRadius: BorderRadius.circular(6),
child: Padding(
padding:
const EdgeInsets.only(top: 5, bottom: 5, left: 11, right: 11),
padding: const EdgeInsets.symmetric(vertical: 5, horizontal: 11),
child: Text(
searchText!,
style: TextStyle(
color: isSelect
? Theme.of(context).colorScheme.primary
: Theme.of(context).colorScheme.onSurfaceVariant,
? colorScheme.primary
: colorScheme.onSurfaceVariant,
),
),
),

View File

@ -45,6 +45,8 @@ class GlobalDataCache {
late List<double> speedsList;
// 用户信息
UserInfoData? userInfo;
// 搜索历史
late List historyCacheList;
// 私有构造函数
GlobalDataCache._();
@ -105,5 +107,6 @@ class GlobalDataCache {
userInfo = userInfoCache.get('userInfoCache');
sheetHeight = localCache.get('sheetHeight');
historyCacheList = localCache.get('cacheList', defaultValue: []);
}
}

View File

@ -5,7 +5,6 @@ import 'package:pilipala/models/user/info.dart';
class GStrorage {
static late final Box<dynamic> userInfo;
static late final Box<dynamic> historyword;
static late final Box<dynamic> localCache;
static late final Box<dynamic> setting;
static late final Box<dynamic> video;
@ -26,18 +25,11 @@ class GStrorage {
localCache = await Hive.openBox(
'localCache',
compactionStrategy: (int entries, int deletedEntries) {
return deletedEntries > 4;
return deletedEntries > 10;
},
);
// 设置
setting = await Hive.openBox('setting');
// 搜索历史
historyword = await Hive.openBox(
'historyWord',
compactionStrategy: (int entries, int deletedEntries) {
return deletedEntries > 10;
},
);
// 视频设置
video = await Hive.openBox('video');
}
@ -52,8 +44,6 @@ class GStrorage {
// user.close();
userInfo.compact();
userInfo.close();
historyword.compact();
historyword.close();
localCache.compact();
localCache.close();
setting.compact();