From 9bac8553a59eb477257887f8ee483205d0bcb8bd Mon Sep 17 00:00:00 2001 From: guozhigq Date: Wed, 19 Apr 2023 10:51:00 +0800 Subject: [PATCH] =?UTF-8?q?mod:=20=E7=82=B9=E5=87=BB=E5=BA=95=E6=A0=8F?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E9=A1=B6=E9=83=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pages/home/controller.dart | 12 ++++++++++++ lib/pages/main/view.dart | 25 ++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) 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