Skip to content

Commit

Permalink
feat: 退出 app 拦截处理
Browse files Browse the repository at this point in the history
  • Loading branch information
wkl007 committed Jun 20, 2021
1 parent 71cb1a1 commit 7a62290
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ android {
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
ndk {
// abiFilters "armeabi-v7a", "arm64-v8a", "x86_64", "x86"
abiFilters "armeabi-v7a", "arm64-v8a", "x86_64", "x86"
// TODO 只打包flutter所支持的架构,flutter没有armeabi架构的so,加x86的原因是为了能够兼容模拟器
abiFilters "armeabi-v7a" // release 时只打"armeabi-v7包
// abiFilters "armeabi-v7a" // release 时只打"armeabi-v7包
}
}

Expand Down
29 changes: 24 additions & 5 deletions lib/navigator/bottom_navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter_bilibili/pages/home_page.dart';
import 'package:flutter_bilibili/pages/profile_page.dart';
import 'package:flutter_bilibili/pages/ranking_page.dart';
import 'package:flutter_bilibili/util/color.dart';
import 'package:flutter_bilibili/util/toast.dart';

class BottomNavigator extends StatefulWidget {
const BottomNavigator({Key? key}) : super(key: key);
Expand All @@ -26,6 +27,9 @@ class _BottomNavigatorState extends State<BottomNavigator> {
/// 标签页面
List<Widget> _pages = [];

///上次点击时间
DateTime? _lastPressedAt;

static int initialPage = 0;
final PageController _controller = PageController(initialPage: initialPage);

Expand All @@ -45,6 +49,18 @@ class _BottomNavigatorState extends State<BottomNavigator> {
});
}

/// 退出 app
Future<bool> exitApp() async {
if (_lastPressedAt == null ||
DateTime.now().difference(_lastPressedAt!) > Duration(seconds: 2)) {
showToast('再按一次退出应用');
// 两次点击间隔超过2秒则重新计时
_lastPressedAt = DateTime.now();
return Future.value(false);
}
return Future.value(true);
}

/// 底部 Item
BottomNavigationBarItem _bottomItem(String label, IconData icon, int index) {
return BottomNavigationBarItem(
Expand All @@ -71,11 +87,14 @@ class _BottomNavigatorState extends State<BottomNavigator> {
}

return Scaffold(
body: PageView(
controller: _controller,
children: _pages,
onPageChanged: (index) => _onJumpTo(index, pageChange: true),
physics: NeverScrollableScrollPhysics(),
body: WillPopScope(
onWillPop: exitApp,
child: PageView(
controller: _controller,
children: _pages,
onPageChanged: (index) => _onJumpTo(index, pageChange: true),
physics: NeverScrollableScrollPhysics(),
),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/home_tab_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class _HomeTabPageState
),
],
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2, childAspectRatio: 0.95),
crossAxisCount: 2, childAspectRatio: 0.82),
itemBuilder: (BuildContext context, int index) {
return VideoCard(videoInfo: dataList[index]);
},
Expand Down

0 comments on commit 7a62290

Please sign in to comment.