pikapika/lib/screens/AppScreen.dart

130 lines
3.3 KiB
Dart
Raw Normal View History

2022-06-29 19:02:01 +00:00
import 'dart:async';
import 'dart:io';
2021-09-29 23:57:09 +00:00
import 'package:flutter/material.dart';
2022-08-11 03:21:55 +00:00
import 'package:flutter_styled_toast/flutter_styled_toast.dart';
2021-11-11 09:05:48 +00:00
import 'package:pikapika/basic/config/Version.dart';
2022-08-11 03:21:55 +00:00
import 'package:pikapika/basic/config/WillPopNotice.dart';
2021-11-11 09:05:48 +00:00
import 'package:pikapika/screens/components/Badge.dart';
2022-06-30 16:31:15 +00:00
import 'package:uni_links/uni_links.dart';
2022-06-29 19:02:01 +00:00
import 'package:uri_to_file/uri_to_file.dart';
2021-09-29 23:57:09 +00:00
2022-07-01 05:39:08 +00:00
import '../basic/Common.dart';
2021-09-29 23:57:09 +00:00
import 'CategoriesScreen.dart';
2022-06-29 19:02:01 +00:00
import 'PkzArchiveScreen.dart';
2021-09-29 23:57:09 +00:00
import 'SpaceScreen.dart';
// MAIN UI 底部导航栏
class AppScreen extends StatefulWidget {
const AppScreen({Key? key}) : super(key: key);
@override
State<AppScreen> createState() => _AppScreenState();
}
class _AppScreenState extends State<AppScreen> {
2022-06-30 16:31:15 +00:00
late StreamSubscription<String?> _linkSubscription;
2022-06-29 19:02:01 +00:00
2021-11-11 09:05:48 +00:00
@override
void initState() {
versionEvent.subscribe(_onVersion);
2022-07-01 05:39:08 +00:00
_linkSubscription = linkSubscript(context);
2021-11-11 09:05:48 +00:00
super.initState();
2022-10-14 17:54:50 +00:00
Future.delayed(Duration.zero, () async {
versionPop(context);
versionEvent.subscribe(_versionSub);
});
2021-11-11 09:05:48 +00:00
}
@override
void dispose() {
versionEvent.unsubscribe(_onVersion);
2022-06-29 19:02:01 +00:00
_linkSubscription.cancel();
2022-09-05 03:05:46 +00:00
versionEvent.unsubscribe(_versionSub);
2021-11-11 09:05:48 +00:00
super.dispose();
}
2022-09-05 03:05:46 +00:00
_versionSub(_) {
versionPop(context);
}
2021-11-11 09:05:48 +00:00
void _onVersion(dynamic a) {
setState(() {});
}
2021-11-29 03:26:30 +00:00
static const List<Widget> _widgetOptions = <Widget>[
2022-03-17 03:31:25 +00:00
CategoriesScreen(),
SpaceScreen(),
2021-09-29 23:57:09 +00:00
];
late int _selectedIndex = 0;
void _onItemTapped(int index) {
setState(() {
2021-11-29 03:26:30 +00:00
_selectedIndex = index;
2021-09-29 23:57:09 +00:00
});
}
@override
Widget build(BuildContext context) {
2022-08-11 03:21:55 +00:00
final body = Scaffold(
2021-11-29 03:26:30 +00:00
body: IndexedStack(
index: _selectedIndex,
2021-09-29 23:57:09 +00:00
children: _widgetOptions,
),
bottomNavigationBar: BottomNavigationBar(
2021-11-11 09:05:48 +00:00
items: <BottomNavigationBarItem>[
2022-03-19 04:12:27 +00:00
const BottomNavigationBarItem(
2021-11-11 09:05:48 +00:00
icon: Icon(Icons.public),
label: '浏览',
),
BottomNavigationBarItem(
icon: Badged(
2022-03-19 04:12:27 +00:00
child: const Icon(Icons.face),
2021-11-11 09:05:48 +00:00
badge: latestVersion() == null ? null : "1",
),
label: '我的',
),
],
2021-09-29 23:57:09 +00:00
currentIndex: _selectedIndex,
iconSize: 20,
selectedFontSize: 12,
unselectedFontSize: 12,
onTap: _onItemTapped,
),
);
2022-08-11 03:21:55 +00:00
return willPop(body);
}
int _noticeTime = 0;
Widget willPop(Scaffold body) {
return WillPopScope(
child: body,
onWillPop: () async {
if (willPopNotice()) {
final now = DateTime.now().millisecondsSinceEpoch;
if (_noticeTime + 3000 > now) {
return true;
} else {
_noticeTime = now;
showToast(
"再次返回将会退出应用程序",
context: context,
position: StyledToastPosition.center,
animation: StyledToastAnimation.scale,
reverseAnimation: StyledToastAnimation.fade,
duration: const Duration(seconds: 3),
animDuration: const Duration(milliseconds: 300),
curve: Curves.elasticOut,
reverseCurve: Curves.linear,
);
return false;
}
}
return true;
},
);
2021-09-29 23:57:09 +00:00
}
}