From fceb55aaa325d1a42e33462bb8712d4878d678a4 Mon Sep 17 00:00:00 2001 From: guozhigq Date: Wed, 30 Aug 2023 09:18:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=A4=E6=AC=A1=E9=80=80=E5=87=BA?= =?UTF-8?q?=E7=A1=AE=E8=AE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pages/main/controller.dart | 14 +++++ lib/pages/main/view.dart | 95 ++++++++++++++++++---------------- 2 files changed, 63 insertions(+), 46 deletions(-) diff --git a/lib/pages/main/controller.dart b/lib/pages/main/controller.dart index 85150349..1d296105 100644 --- a/lib/pages/main/controller.dart +++ b/lib/pages/main/controller.dart @@ -1,5 +1,6 @@ import 'dart:async'; +import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get/get.dart'; import 'package:flutter/material.dart'; import 'package:hive/hive.dart'; @@ -53,6 +54,7 @@ class MainController extends GetxController { final StreamController bottomBarStream = StreamController.broadcast(); Box setting = GStrorage.setting; + DateTime? _lastPressedAt; @override void onInit() { @@ -61,4 +63,16 @@ class MainController extends GetxController { Utils.checkUpdata(); } } + + Future onBackPressed(BuildContext context) { + if (_lastPressedAt == null || + DateTime.now().difference(_lastPressedAt!) > + const Duration(seconds: 2)) { + // 两次点击时间间隔超过2秒,重新记录时间戳 + _lastPressedAt = DateTime.now(); + SmartDialog.showToast("再按一次退出Pili"); + return Future.value(false); // 不退出应用 + } + return Future.value(true); // 退出应用 + } } diff --git a/lib/pages/main/view.dart b/lib/pages/main/view.dart index b771ab0f..c744098e 100644 --- a/lib/pages/main/view.dart +++ b/lib/pages/main/view.dart @@ -110,55 +110,58 @@ class _MainAppState extends State with SingleTickerProviderStateMixin { MediaQuery.of(context).size.width * 9 / 16; localCache.put('sheetHeight', sheetHeight); localCache.put('statusBarHeight', statusBarHeight); - return Scaffold( - extendBody: true, - body: FadeTransition( - opacity: _fadeAnimation!, - child: SlideTransition( - position: Tween( - begin: const Offset(0, 0.5), - end: Offset.zero, - ).animate( - CurvedAnimation( - parent: _slideAnimation!, - curve: Curves.fastOutSlowIn, - reverseCurve: Curves.linear, + return WillPopScope( + onWillPop: () => _mainController.onBackPressed(context), + child: Scaffold( + extendBody: true, + body: FadeTransition( + opacity: _fadeAnimation!, + child: SlideTransition( + position: Tween( + begin: const Offset(0, 0.5), + end: Offset.zero, + ).animate( + CurvedAnimation( + parent: _slideAnimation!, + curve: Curves.fastOutSlowIn, + reverseCurve: Curves.linear, + ), + ), + child: PageView( + physics: const NeverScrollableScrollPhysics(), + controller: _pageController, + onPageChanged: (index) { + selectedIndex = index; + setState(() {}); + }, + children: _mainController.pages, ), - ), - child: PageView( - physics: const NeverScrollableScrollPhysics(), - controller: _pageController, - onPageChanged: (index) { - selectedIndex = index; - setState(() {}); - }, - children: _mainController.pages, ), ), - ), - bottomNavigationBar: StreamBuilder( - stream: _mainController.bottomBarStream.stream, - initialData: true, - builder: (context, AsyncSnapshot snapshot) { - return AnimatedSlide( - curve: Curves.easeInOutCubicEmphasized, - duration: const Duration(milliseconds: 1000), - offset: Offset(0, snapshot.data ? 0 : 1), - child: NavigationBar( - onDestinationSelected: (value) => setIndex(value), - selectedIndex: selectedIndex, - destinations: [ - ..._mainController.navigationBars.map((e) { - return NavigationDestination( - icon: e['icon'], - selectedIcon: e['selectIcon'], - label: e['label'], - ); - }).toList(), - ], - ), - ); - }, + bottomNavigationBar: StreamBuilder( + stream: _mainController.bottomBarStream.stream, + initialData: true, + builder: (context, AsyncSnapshot snapshot) { + return AnimatedSlide( + curve: Curves.easeInOutCubicEmphasized, + duration: const Duration(milliseconds: 1000), + offset: Offset(0, snapshot.data ? 0 : 1), + child: NavigationBar( + onDestinationSelected: (value) => setIndex(value), + selectedIndex: selectedIndex, + destinations: [ + ..._mainController.navigationBars.map((e) { + return NavigationDestination( + icon: e['icon'], + selectedIcon: e['selectIcon'], + label: e['label'], + ); + }).toList(), + ], + ), + ); + }, + ), ), ); }