mod: hotPage开发
This commit is contained in:
@ -0,0 +1,59 @@
|
||||
import 'package:flutter/animation.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:pilipala/http/api.dart';
|
||||
import 'package:pilipala/http/init.dart';
|
||||
|
||||
class HotController extends GetxController {
|
||||
final ScrollController scrollController = ScrollController();
|
||||
final int _count = 20;
|
||||
int _currentPage = 1;
|
||||
RxList videoList = [].obs;
|
||||
bool isLoadingMore = false;
|
||||
bool flag = false;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
queryHotFeed('init');
|
||||
}
|
||||
|
||||
// 获取推荐
|
||||
Future queryHotFeed(type) async {
|
||||
var res = await Request().get(
|
||||
Api.hotList,
|
||||
data: {'pn': _currentPage, 'ps': _count},
|
||||
);
|
||||
var data = res.data['data']['list'];
|
||||
if (type == 'init') {
|
||||
videoList.value = data;
|
||||
} else if (type == 'onRefresh') {
|
||||
videoList.insertAll(0, data);
|
||||
} else if (type == 'onLoad') {
|
||||
videoList.addAll(data);
|
||||
}
|
||||
_currentPage += 1;
|
||||
isLoadingMore = false;
|
||||
}
|
||||
|
||||
// 下拉刷新
|
||||
Future onRefresh() async {
|
||||
queryHotFeed('onRefresh');
|
||||
}
|
||||
|
||||
// 上拉加载
|
||||
Future onLoad() async {
|
||||
queryHotFeed('onLoad');
|
||||
}
|
||||
|
||||
// 返回顶部并刷新
|
||||
void animateToTop() async {
|
||||
if (scrollController.offset >=
|
||||
MediaQuery.of(Get.context!).size.height * 5) {
|
||||
scrollController.jumpTo(0);
|
||||
} else {
|
||||
await scrollController.animateTo(0,
|
||||
duration: const Duration(milliseconds: 500), curve: Curves.easeInOut);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,70 @@
|
||||
import "package:flutter/material.dart";
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:pilipala/common/widgets/video_card_h.dart';
|
||||
import 'package:pilipala/pages/hot/controller.dart';
|
||||
import 'package:pilipala/pages/home/widgets/app_bar.dart';
|
||||
|
||||
class HotPage extends StatefulWidget {
|
||||
const HotPage({super.key});
|
||||
const HotPage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<HotPage> createState() => _HotPageState();
|
||||
}
|
||||
|
||||
class _HotPageState extends State<HotPage> {
|
||||
class _HotPageState extends State<HotPage> with AutomaticKeepAliveClientMixin {
|
||||
final HotController _hotController = Get.put(HotController());
|
||||
List videoList = [];
|
||||
|
||||
@override
|
||||
bool get wantKeepAlive => true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_hotController.videoList.listen((value) {
|
||||
videoList = value;
|
||||
setState(() {});
|
||||
});
|
||||
|
||||
_hotController.scrollController.addListener(
|
||||
() {
|
||||
if (_hotController.scrollController.position.pixels >=
|
||||
_hotController.scrollController.position.maxScrollExtent - 200) {
|
||||
if (!_hotController.isLoadingMore) {
|
||||
_hotController.isLoadingMore = true;
|
||||
_hotController.onLoad();
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
super.build(context);
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('热门'),
|
||||
body: RefreshIndicator(
|
||||
displacement: kToolbarHeight + MediaQuery.of(context).padding.top,
|
||||
onRefresh: () async {
|
||||
return await _hotController.onRefresh();
|
||||
},
|
||||
child: CustomScrollView(
|
||||
controller: _hotController.scrollController,
|
||||
slivers: [
|
||||
const HomeAppBar(),
|
||||
SliverList(
|
||||
delegate: SliverChildBuilderDelegate((context, index) {
|
||||
return VideoCardH(
|
||||
videoItem: videoList[index],
|
||||
);
|
||||
}, childCount: videoList.length)),
|
||||
SliverToBoxAdapter(
|
||||
child: SizedBox(
|
||||
height: MediaQuery.of(context).padding.bottom + 10,
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
// import 'package:pilipala/pages/home/controller.dart';
|
||||
import 'package:pilipala/pages/home/index.dart';
|
||||
import 'package:pilipala/pages/hot/index.dart';
|
||||
import './controller.dart';
|
||||
|
||||
class MainApp extends StatefulWidget {
|
||||
@ -14,6 +14,7 @@ class MainApp extends StatefulWidget {
|
||||
class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
|
||||
final MainController _mainController = Get.put(MainController());
|
||||
final HomeController _homeController = Get.put(HomeController());
|
||||
final HotController _hotController = Get.put(HotController());
|
||||
|
||||
late AnimationController? _animationController;
|
||||
late Animation<double>? _fadeAnimation;
|
||||
@ -63,6 +64,22 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
|
||||
} else {
|
||||
_homeController.flag = false;
|
||||
}
|
||||
|
||||
if (currentPage is HotPage) {
|
||||
if (_hotController.flag) {
|
||||
// 单击返回顶部 双击并刷新
|
||||
if (DateTime.now().millisecondsSinceEpoch - _lastSelectTime! < 500) {
|
||||
_hotController.onRefresh();
|
||||
} else {
|
||||
await Future.delayed(const Duration(microseconds: 300));
|
||||
_hotController.animateToTop();
|
||||
}
|
||||
_lastSelectTime = DateTime.now().millisecondsSinceEpoch;
|
||||
}
|
||||
_hotController.flag = true;
|
||||
} else {
|
||||
_hotController.flag = false;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
Reference in New Issue
Block a user