diff --git a/lib/pages/home/controller.dart b/lib/pages/home/controller.dart index a8c5b6797..32aa81116 100644 --- a/lib/pages/home/controller.dart +++ b/lib/pages/home/controller.dart @@ -11,6 +11,7 @@ class HomeController extends GetxController { int crossAxisCount = 2; RxList videoList = [RecVideoItemModel()].obs; bool isLoadingMore = false; + bool flag = false; @override void onInit() { @@ -49,4 +50,15 @@ class HomeController extends GetxController { await Future.delayed(const Duration(milliseconds: 500)); queryRcmdFeed('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); + } + } } diff --git a/lib/pages/main/view.dart b/lib/pages/main/view.dart index 1aef13566..0ecb95f4e 100644 --- a/lib/pages/main/view.dart +++ b/lib/pages/main/view.dart @@ -1,5 +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 './controller.dart'; class MainApp extends StatefulWidget { @@ -11,10 +13,13 @@ class MainApp extends StatefulWidget { class _MainAppState extends State with SingleTickerProviderStateMixin { final MainController _mainController = Get.put(MainController()); + final HomeController _homeController = Get.put(HomeController()); + late AnimationController? _animationController; late Animation? _fadeAnimation; late Animation? _slideAnimation; int selectedIndex = 0; + int? _lastSelectTime; //上次点击时间 @override void initState() { @@ -29,9 +34,10 @@ class _MainAppState extends State with SingleTickerProviderStateMixin { Tween(begin: 0.8, end: 1.0).animate(_animationController!); _slideAnimation = Tween(begin: 0.8, end: 1.0).animate(_animationController!); + _lastSelectTime = DateTime.now().millisecondsSinceEpoch; } - void setIndex(int value) { + void setIndex(int value) async { if (selectedIndex != value) { selectedIndex = value; _animationController!.reverse().then((_) { @@ -40,6 +46,23 @@ class _MainAppState extends State with SingleTickerProviderStateMixin { }); setState(() {}); } + + var currentPage = _mainController.pages[value]; + if (currentPage is HomePage) { + if (_homeController.flag) { + // 单击返回顶部 双击并刷新 + if (DateTime.now().millisecondsSinceEpoch - _lastSelectTime! < 500) { + _homeController.onRefresh(); + } else { + await Future.delayed(const Duration(microseconds: 300)); + _homeController.animateToTop(); + } + _lastSelectTime = DateTime.now().millisecondsSinceEpoch; + } + _homeController.flag = true; + } else { + _homeController.flag = false; + } } @override