mod: 使用PageView替换IndexedStack
This commit is contained in:
@ -11,12 +11,6 @@ class HotController extends GetxController {
|
|||||||
bool isLoadingMore = false;
|
bool isLoadingMore = false;
|
||||||
bool flag = false;
|
bool flag = false;
|
||||||
|
|
||||||
@override
|
|
||||||
void onInit() {
|
|
||||||
super.onInit();
|
|
||||||
queryHotFeed('init');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取推荐
|
// 获取推荐
|
||||||
Future queryHotFeed(type) async {
|
Future queryHotFeed(type) async {
|
||||||
var res = await VideoHttp.hotVideoList(
|
var res = await VideoHttp.hotVideoList(
|
||||||
@ -34,6 +28,7 @@ class HotController extends GetxController {
|
|||||||
_currentPage += 1;
|
_currentPage += 1;
|
||||||
}
|
}
|
||||||
isLoadingMore = false;
|
isLoadingMore = false;
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 下拉刷新
|
// 下拉刷新
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
|
import 'package:pilipala/common/skeleton/video_card_h.dart';
|
||||||
|
import 'package:pilipala/common/widgets/http_error.dart';
|
||||||
import 'package:pilipala/common/widgets/video_card_h.dart';
|
import 'package:pilipala/common/widgets/video_card_h.dart';
|
||||||
import 'package:pilipala/pages/hot/controller.dart';
|
import 'package:pilipala/pages/hot/controller.dart';
|
||||||
import 'package:pilipala/pages/home/widgets/app_bar.dart';
|
import 'package:pilipala/pages/home/widgets/app_bar.dart';
|
||||||
@ -14,6 +16,7 @@ class HotPage extends StatefulWidget {
|
|||||||
class _HotPageState extends State<HotPage> with AutomaticKeepAliveClientMixin {
|
class _HotPageState extends State<HotPage> with AutomaticKeepAliveClientMixin {
|
||||||
final HotController _hotController = Get.put(HotController());
|
final HotController _hotController = Get.put(HotController());
|
||||||
List videoList = [];
|
List videoList = [];
|
||||||
|
Future? _futureBuilderFuture;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool get wantKeepAlive => true;
|
bool get wantKeepAlive => true;
|
||||||
@ -21,11 +24,7 @@ class _HotPageState extends State<HotPage> with AutomaticKeepAliveClientMixin {
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_hotController.videoList.listen((value) {
|
_futureBuilderFuture = _hotController!.queryHotFeed('init');
|
||||||
videoList = value;
|
|
||||||
setState(() {});
|
|
||||||
});
|
|
||||||
|
|
||||||
_hotController.scrollController.addListener(
|
_hotController.scrollController.addListener(
|
||||||
() {
|
() {
|
||||||
if (_hotController.scrollController.position.pixels >=
|
if (_hotController.scrollController.position.pixels >=
|
||||||
@ -52,12 +51,37 @@ class _HotPageState extends State<HotPage> with AutomaticKeepAliveClientMixin {
|
|||||||
controller: _hotController.scrollController,
|
controller: _hotController.scrollController,
|
||||||
slivers: [
|
slivers: [
|
||||||
const HomeAppBar(),
|
const HomeAppBar(),
|
||||||
SliverList(
|
FutureBuilder(
|
||||||
delegate: SliverChildBuilderDelegate((context, index) {
|
future: _futureBuilderFuture,
|
||||||
return VideoCardH(
|
builder: (context, snapshot) {
|
||||||
videoItem: videoList[index],
|
if (snapshot.connectionState == ConnectionState.done) {
|
||||||
);
|
Map data = snapshot.data as Map;
|
||||||
}, childCount: videoList.length)),
|
if (data['status']) {
|
||||||
|
return Obx(
|
||||||
|
() => SliverList(
|
||||||
|
delegate: SliverChildBuilderDelegate((context, index) {
|
||||||
|
return VideoCardH(
|
||||||
|
videoItem: _hotController.videoList[index],
|
||||||
|
);
|
||||||
|
}, childCount: _hotController.videoList.length),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return HttpError(
|
||||||
|
errMsg: data['msg'],
|
||||||
|
fn: () => setState(() {}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 骨架屏
|
||||||
|
return SliverList(
|
||||||
|
delegate: SliverChildBuilderDelegate((context, index) {
|
||||||
|
return const VideoCardHSkeleton();
|
||||||
|
}, childCount: 5),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
SliverToBoxAdapter(
|
SliverToBoxAdapter(
|
||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
height: MediaQuery.of(context).padding.bottom + 10,
|
height: MediaQuery.of(context).padding.bottom + 10,
|
||||||
|
|||||||
@ -15,11 +15,12 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
|
|||||||
final MainController _mainController = Get.put(MainController());
|
final MainController _mainController = Get.put(MainController());
|
||||||
final HomeController _homeController = Get.put(HomeController());
|
final HomeController _homeController = Get.put(HomeController());
|
||||||
final HotController _hotController = Get.put(HotController());
|
final HotController _hotController = Get.put(HotController());
|
||||||
|
PageController? _pageController;
|
||||||
|
|
||||||
late AnimationController? _animationController;
|
late AnimationController? _animationController;
|
||||||
late Animation<double>? _fadeAnimation;
|
late Animation<double>? _fadeAnimation;
|
||||||
late Animation<double>? _slideAnimation;
|
late Animation<double>? _slideAnimation;
|
||||||
int selectedIndex = 2;
|
int selectedIndex = 0;
|
||||||
int? _lastSelectTime; //上次点击时间
|
int? _lastSelectTime; //上次点击时间
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -36,6 +37,7 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
|
|||||||
_slideAnimation =
|
_slideAnimation =
|
||||||
Tween(begin: 0.8, end: 1.0).animate(_animationController!);
|
Tween(begin: 0.8, end: 1.0).animate(_animationController!);
|
||||||
_lastSelectTime = DateTime.now().millisecondsSinceEpoch;
|
_lastSelectTime = DateTime.now().millisecondsSinceEpoch;
|
||||||
|
_pageController = PageController(initialPage: selectedIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setIndex(int value) async {
|
void setIndex(int value) async {
|
||||||
@ -47,7 +49,7 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
|
|||||||
});
|
});
|
||||||
setState(() {});
|
setState(() {});
|
||||||
}
|
}
|
||||||
|
_pageController!.jumpToPage(value);
|
||||||
var currentPage = _mainController.pages[value];
|
var currentPage = _mainController.pages[value];
|
||||||
if (currentPage is HomePage) {
|
if (currentPage is HomePage) {
|
||||||
if (_homeController.flag) {
|
if (_homeController.flag) {
|
||||||
@ -98,8 +100,13 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
|
|||||||
reverseCurve: Curves.linear,
|
reverseCurve: Curves.linear,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
child: IndexedStack(
|
child: PageView(
|
||||||
index: selectedIndex,
|
controller: _pageController,
|
||||||
|
physics: const NeverScrollableScrollPhysics(),
|
||||||
|
onPageChanged: (index) {
|
||||||
|
selectedIndex = index;
|
||||||
|
setState(() {});
|
||||||
|
},
|
||||||
children: _mainController.pages,
|
children: _mainController.pages,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user