mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
refa: search panel
Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
@@ -1,134 +1,106 @@
|
||||
import 'package:PiliPlus/common/widgets/refresh_indicator.dart';
|
||||
import 'package:PiliPlus/http/loading_state.dart';
|
||||
import 'package:PiliPlus/pages/search_panel/widgets/video_panel.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:PiliPlus/common/constants.dart';
|
||||
import 'package:PiliPlus/common/skeleton/media_bangumi.dart';
|
||||
import 'package:PiliPlus/common/skeleton/video_card_h.dart';
|
||||
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/result.dart';
|
||||
import 'package:PiliPlus/utils/grid.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:PiliPlus/models/common/search_type.dart';
|
||||
|
||||
import '../../common/constants.dart';
|
||||
import '../../utils/grid.dart';
|
||||
import 'controller.dart';
|
||||
import 'widgets/article_panel.dart';
|
||||
import 'widgets/live_panel.dart';
|
||||
import 'widgets/media_bangumi_panel.dart';
|
||||
import 'widgets/user_panel.dart';
|
||||
|
||||
class SearchPanel extends StatefulWidget {
|
||||
final String keyword;
|
||||
final SearchType searchType;
|
||||
final String tag;
|
||||
const SearchPanel({
|
||||
abstract class CommonSearchPanel extends StatefulWidget {
|
||||
const CommonSearchPanel({
|
||||
super.key,
|
||||
required this.keyword,
|
||||
required this.searchType,
|
||||
required this.tag,
|
||||
this.hasHeader = false,
|
||||
});
|
||||
|
||||
@override
|
||||
State<SearchPanel> createState() => _SearchPanelState();
|
||||
final String keyword;
|
||||
final SearchType searchType;
|
||||
final String tag;
|
||||
final bool hasHeader;
|
||||
}
|
||||
|
||||
class _SearchPanelState extends State<SearchPanel>
|
||||
with AutomaticKeepAliveClientMixin {
|
||||
late SearchPanelController _searchPanelController;
|
||||
abstract class CommonSearchPanelState<
|
||||
S extends CommonSearchPanel,
|
||||
R extends SearchNumData<T>,
|
||||
T> extends State<S> with AutomaticKeepAliveClientMixin {
|
||||
SearchPanelController<R, T> get controller;
|
||||
|
||||
@override
|
||||
bool get wantKeepAlive => true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_searchPanelController = Get.put(
|
||||
SearchPanelController(
|
||||
keyword: widget.keyword,
|
||||
searchType: widget.searchType,
|
||||
tag: widget.tag,
|
||||
),
|
||||
tag: widget.searchType.name + widget.tag,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
super.build(context);
|
||||
return refreshIndicator(
|
||||
onRefresh: () async {
|
||||
await _searchPanelController.onRefresh();
|
||||
await controller.onRefresh();
|
||||
},
|
||||
child: Obx(() => _buildBody(_searchPanelController.loadingState.value)),
|
||||
child: SafeArea(
|
||||
bottom: false,
|
||||
child: CustomScrollView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
slivers: [
|
||||
if (widget.hasHeader)
|
||||
Obx(() => buildHeader(controller.loadingState.value)),
|
||||
Obx(() => _buildBody(controller.loadingState.value)),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBody(LoadingState<List<dynamic>?> loadingState) {
|
||||
if (loadingState is Loading) {
|
||||
return CustomScrollView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
slivers: [
|
||||
SliverGrid(
|
||||
gridDelegate: widget.searchType == SearchType.media_bangumi ||
|
||||
widget.searchType == SearchType.media_ft
|
||||
? SliverGridDelegateWithExtentAndRatio(
|
||||
mainAxisSpacing: 2,
|
||||
maxCrossAxisExtent: Grid.smallCardWidth * 2,
|
||||
childAspectRatio: StyleString.aspectRatio * 1.5,
|
||||
minHeight: MediaQuery.textScalerOf(context).scale(155),
|
||||
)
|
||||
: Grid.videoCardHDelegate(context),
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
(context, index) {
|
||||
switch (widget.searchType) {
|
||||
case SearchType.video:
|
||||
return const VideoCardHSkeleton();
|
||||
case SearchType.media_bangumi || SearchType.media_ft:
|
||||
return const MediaBangumiSkeleton();
|
||||
case SearchType.bili_user:
|
||||
return const VideoCardHSkeleton();
|
||||
case SearchType.live_room:
|
||||
return const VideoCardHSkeleton();
|
||||
default:
|
||||
return const VideoCardHSkeleton();
|
||||
}
|
||||
},
|
||||
childCount: 15,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
} else {
|
||||
switch (widget.searchType) {
|
||||
case SearchType.video:
|
||||
return searchVideoPanel(
|
||||
context,
|
||||
_searchPanelController,
|
||||
loadingState,
|
||||
);
|
||||
case SearchType.media_bangumi || SearchType.media_ft:
|
||||
return searchBangumiPanel(
|
||||
context,
|
||||
_searchPanelController,
|
||||
loadingState,
|
||||
);
|
||||
case SearchType.bili_user:
|
||||
return searchUserPanel(
|
||||
context,
|
||||
_searchPanelController,
|
||||
loadingState,
|
||||
);
|
||||
case SearchType.live_room:
|
||||
return searchLivePanel(
|
||||
context,
|
||||
_searchPanelController,
|
||||
loadingState,
|
||||
);
|
||||
case SearchType.article:
|
||||
return searchArticlePanel(
|
||||
context,
|
||||
_searchPanelController,
|
||||
loadingState,
|
||||
);
|
||||
}
|
||||
}
|
||||
Widget buildHeader(LoadingState<List<T>?> loadingState) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
Widget get _builLoading {
|
||||
return SliverGrid(
|
||||
gridDelegate: widget.searchType == SearchType.media_bangumi ||
|
||||
widget.searchType == SearchType.media_ft
|
||||
? SliverGridDelegateWithExtentAndRatio(
|
||||
mainAxisSpacing: 2,
|
||||
maxCrossAxisExtent: Grid.smallCardWidth * 2,
|
||||
childAspectRatio: StyleString.aspectRatio * 1.5,
|
||||
minHeight: MediaQuery.textScalerOf(context).scale(155),
|
||||
)
|
||||
: Grid.videoCardHDelegate(context),
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
(context, index) {
|
||||
switch (widget.searchType) {
|
||||
case SearchType.media_bangumi || SearchType.media_ft:
|
||||
return const MediaBangumiSkeleton();
|
||||
default:
|
||||
return const VideoCardHSkeleton();
|
||||
}
|
||||
},
|
||||
childCount: 15,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBody(LoadingState<List<T>?> loadingState) {
|
||||
return switch (loadingState) {
|
||||
Loading() => _builLoading,
|
||||
Success() => loadingState.response?.isNotEmpty == true
|
||||
? buildList(loadingState.response!)
|
||||
: HttpError(
|
||||
callback: controller.onReload,
|
||||
),
|
||||
Error() => HttpError(
|
||||
errMsg: loadingState.errMsg,
|
||||
callback: controller.onReload,
|
||||
),
|
||||
_ => throw UnimplementedError(),
|
||||
};
|
||||
}
|
||||
|
||||
Widget buildList(List<T> list);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user