mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
feat: search trending page
Closes #684 Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
@@ -156,13 +156,7 @@ class _SearchPageState extends State<SearchPage> with RouteAware {
|
||||
GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onTap: () {
|
||||
Get.toNamed(
|
||||
'/webview',
|
||||
parameters: {
|
||||
'url':
|
||||
'https://www.bilibili.com/blackboard/activity-trending-topic.html?navhide=1&native.theme=1&night=${Get.isDarkMode ? 1 : 0}'
|
||||
},
|
||||
);
|
||||
Get.toNamed('/searchTrending');
|
||||
},
|
||||
child: Padding(
|
||||
padding:
|
||||
|
||||
@@ -64,7 +64,7 @@ class _SearchResultPageState extends State<SearchResultPage>
|
||||
),
|
||||
title: GestureDetector(
|
||||
onTap: () {
|
||||
if (Get.previousRoute.startsWith('/search')) {
|
||||
if (Get.previousRoute.startsWith('/search?')) {
|
||||
Get.back();
|
||||
} else {
|
||||
Get.offNamed(
|
||||
|
||||
27
lib/pages/search_trending/controller.dart
Normal file
27
lib/pages/search_trending/controller.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
import 'package:PiliPlus/http/loading_state.dart';
|
||||
import 'package:PiliPlus/http/search.dart';
|
||||
import 'package:PiliPlus/models/search/search_trending/trending_data.dart';
|
||||
import 'package:PiliPlus/models/search/search_trending/trending_list.dart';
|
||||
import 'package:PiliPlus/pages/common/common_list_controller.dart';
|
||||
|
||||
class SearchTrendingController
|
||||
extends CommonListController<TrendingData, TrendingList> {
|
||||
int topCount = 0;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
queryData();
|
||||
}
|
||||
|
||||
@override
|
||||
List<TrendingList>? getDataList(TrendingData response) {
|
||||
List<TrendingList> topList = (response.topList ?? <TrendingList>[]);
|
||||
topCount = topList.length;
|
||||
return topList + (response.list ?? <TrendingList>[]);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<LoadingState<TrendingData>> customGetData() =>
|
||||
SearchHttp.searchTrending();
|
||||
}
|
||||
207
lib/pages/search_trending/view.dart
Normal file
207
lib/pages/search_trending/view.dart
Normal file
@@ -0,0 +1,207 @@
|
||||
import 'package:PiliPlus/common/widgets/http_error.dart';
|
||||
import 'package:PiliPlus/common/widgets/refresh_indicator.dart';
|
||||
import 'package:PiliPlus/http/loading_state.dart';
|
||||
import 'package:PiliPlus/models/search/search_trending/trending_list.dart';
|
||||
import 'package:PiliPlus/pages/search_trending/controller.dart';
|
||||
import 'package:PiliPlus/utils/extension.dart';
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class SearchTrendingPage extends StatefulWidget {
|
||||
const SearchTrendingPage({super.key});
|
||||
|
||||
@override
|
||||
State<SearchTrendingPage> createState() => _SearchTrendingPageState();
|
||||
}
|
||||
|
||||
class _SearchTrendingPageState extends State<SearchTrendingPage> {
|
||||
final _controller = Get.put(SearchTrendingController());
|
||||
|
||||
late double _offset;
|
||||
final RxDouble _scrollRatio = 0.0.obs;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller.scrollController.addListener(listener);
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
_offset = Get.width * 528 / 1125 - 56 - Get.mediaQuery.padding.top;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.scrollController.removeListener(listener);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void listener() {
|
||||
_scrollRatio.value = clampDouble(
|
||||
_controller.scrollController.position.pixels / _offset,
|
||||
0.0,
|
||||
1.0,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
extendBody: true,
|
||||
extendBodyBehindAppBar: true,
|
||||
appBar: PreferredSize(
|
||||
preferredSize: Size.fromHeight(56),
|
||||
child: Obx(
|
||||
() {
|
||||
final half = _scrollRatio.value >= 0.5;
|
||||
return AppBar(
|
||||
title: Opacity(
|
||||
opacity: _scrollRatio.value,
|
||||
child: Text(
|
||||
'B站热搜',
|
||||
style: TextStyle(
|
||||
color: half ? null : Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
backgroundColor: Theme.of(context)
|
||||
.colorScheme
|
||||
.surface
|
||||
.withOpacity(_scrollRatio.value),
|
||||
foregroundColor: half ? null : Colors.white,
|
||||
systemOverlayStyle: half
|
||||
? null
|
||||
: SystemUiOverlayStyle(
|
||||
statusBarBrightness: Brightness.dark,
|
||||
statusBarIconBrightness: Brightness.light,
|
||||
),
|
||||
bottom: _scrollRatio.value == 1
|
||||
? PreferredSize(
|
||||
preferredSize: Size.fromHeight(1),
|
||||
child: Divider(
|
||||
height: 1,
|
||||
color: Theme.of(context)
|
||||
.colorScheme
|
||||
.outline
|
||||
.withOpacity(0.1),
|
||||
),
|
||||
)
|
||||
: null,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
body: refreshIndicator(
|
||||
onRefresh: () async {
|
||||
await _controller.onRefresh();
|
||||
},
|
||||
child: CustomScrollView(
|
||||
controller: _controller.scrollController,
|
||||
slivers: [
|
||||
SliverToBoxAdapter(
|
||||
child: CachedNetworkImage(
|
||||
fit: BoxFit.fitWidth,
|
||||
imageUrl:
|
||||
'https://activity.hdslb.com/blackboard/activity59158/img/hot_banner.fbb081df.png',
|
||||
),
|
||||
),
|
||||
Obx(() => _buildBody(_controller.loadingState.value)),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBody(LoadingState<List<TrendingList>?> loadingState) {
|
||||
return switch (loadingState) {
|
||||
Loading() => SliverToBoxAdapter(child: LinearProgressIndicator()),
|
||||
Success() => loadingState.response?.isNotEmpty == true
|
||||
? SliverPadding(
|
||||
padding: EdgeInsets.only(
|
||||
bottom: MediaQuery.paddingOf(context).bottom + 100),
|
||||
sliver: SliverList.separated(
|
||||
itemCount: loadingState.response!.length,
|
||||
itemBuilder: (context, index) {
|
||||
final item = loadingState.response![index];
|
||||
return ListTile(
|
||||
dense: true,
|
||||
onTap: () {
|
||||
Get.toNamed(
|
||||
'/searchResult',
|
||||
parameters: {
|
||||
'keyword': item.keyword!,
|
||||
},
|
||||
);
|
||||
},
|
||||
leading: index < _controller.topCount
|
||||
? Icon(
|
||||
size: 16,
|
||||
Icons.vertical_align_top_outlined,
|
||||
color: const Color(0xFFd1403e),
|
||||
)
|
||||
: Text(
|
||||
'${index + 1 - _controller.topCount}',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: switch (index - _controller.topCount) {
|
||||
0 => const Color(0xFFfdad13),
|
||||
1 => const Color(0xFF8aace1),
|
||||
2 => const Color(0xFFdfa777),
|
||||
_ => Theme.of(context).colorScheme.outline,
|
||||
},
|
||||
fontSize: 16,
|
||||
fontStyle: FontStyle.italic,
|
||||
),
|
||||
),
|
||||
title: Row(
|
||||
children: [
|
||||
Flexible(
|
||||
child: Text(
|
||||
item.keyword!,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
strutStyle: StrutStyle(height: 1, leading: 0),
|
||||
style: TextStyle(height: 1, fontSize: 14),
|
||||
),
|
||||
),
|
||||
if (item.icon?.isNotEmpty == true) ...[
|
||||
const SizedBox(width: 4),
|
||||
CachedNetworkImage(
|
||||
imageUrl: item.icon!.http2https,
|
||||
height: 15,
|
||||
),
|
||||
] else if (item.showLiveIcon == true) ...[
|
||||
const SizedBox(width: 4),
|
||||
Image.asset(
|
||||
'assets/images/live/live_@28h.gif',
|
||||
width: 48,
|
||||
height: 15,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
separatorBuilder: (context, index) => Divider(
|
||||
height: 1,
|
||||
indent: 48,
|
||||
color: Theme.of(context).colorScheme.outline.withOpacity(0.1),
|
||||
),
|
||||
),
|
||||
)
|
||||
: HttpError(
|
||||
callback: _controller.onReload,
|
||||
),
|
||||
Error() => HttpError(
|
||||
errMsg: loadingState.errMsg,
|
||||
callback: _controller.onReload,
|
||||
),
|
||||
_ => throw UnimplementedError(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -196,20 +196,6 @@ class _WebviewPageNewState extends State<WebviewPageNew> {
|
||||
}
|
||||
},
|
||||
);
|
||||
controller.addJavaScriptHandler(
|
||||
handlerName: "onSearch",
|
||||
callback: (args) {
|
||||
dynamic title = args.firstOrNull;
|
||||
if (title != null) {
|
||||
Get.toNamed(
|
||||
'/searchResult',
|
||||
parameters: {
|
||||
'keyword': title,
|
||||
},
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
onProgressChanged: (controller, progress) {
|
||||
this.progress.value = progress / 100;
|
||||
@@ -238,21 +224,6 @@ class _WebviewPageNewState extends State<WebviewPageNew> {
|
||||
document.styleSheets[0].insertRule('#app__display-area > div.control-panel {display:none;}', 0);
|
||||
''',
|
||||
);
|
||||
} else if (url.startsWith(
|
||||
'https://www.bilibili.com/blackboard/activity-trending-topic.html')) {
|
||||
controller.evaluateJavascript(source: '''
|
||||
document.addEventListener("click", function(e) {
|
||||
const parentElement = e.target.parentElement;
|
||||
if (parentElement) {
|
||||
const trendingTitleElement = parentElement.querySelector(".trending-title");
|
||||
if (trendingTitleElement) {
|
||||
const rankElement = trendingTitleElement.querySelector(".rank-index");
|
||||
const title = rankElement ? trendingTitleElement.innerText.replace(rankElement.innerText, "").trim() : trendingTitleElement.innerText;
|
||||
window.flutter_inappwebview.callHandler("onSearch", title);
|
||||
}
|
||||
}
|
||||
});
|
||||
''');
|
||||
}
|
||||
// _webViewController?.evaluateJavascript(
|
||||
// source: '''
|
||||
@@ -263,11 +234,6 @@ class _WebviewPageNewState extends State<WebviewPageNew> {
|
||||
},
|
||||
onDownloadStartRequest: Platform.isAndroid
|
||||
? (controller, request) {
|
||||
if (_url.startsWith(
|
||||
'https://www.bilibili.com/blackboard/activity-trending-topic.html')) {
|
||||
progress.value = 1;
|
||||
return;
|
||||
}
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
@@ -340,9 +306,6 @@ class _WebviewPageNewState extends State<WebviewPageNew> {
|
||||
return NavigationActionPolicy.CANCEL;
|
||||
} else if (RegExp(r'^(?!(https?://))\S+://', caseSensitive: false)
|
||||
.hasMatch(url)) {
|
||||
if (url.startsWith('bilibili://browser')) {
|
||||
return NavigationActionPolicy.CANCEL;
|
||||
}
|
||||
if (context.mounted) {
|
||||
SnackBar snackBar = SnackBar(
|
||||
content: const Text('当前网页将要打开外部链接,是否打开'),
|
||||
|
||||
Reference in New Issue
Block a user