AppScreen.dart 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_styled_toast/flutter_styled_toast.dart';
  5. import 'package:prime_chat/screens/ContactsScreen.dart';
  6. import 'package:prime_chat/screens/SpaceScreen.dart';
  7. // MAIN UI 底部导航栏
  8. class AppScreen extends StatefulWidget {
  9. const AppScreen({Key? key}) : super(key: key);
  10. @override
  11. State<AppScreen> createState() => _AppScreenState();
  12. }
  13. class _AppScreenState extends State<AppScreen> {
  14. late StreamSubscription<String?> _linkSubscription;
  15. static const List<Widget> _widgetOptions = <Widget>[
  16. ContactsScreen(),
  17. SpaceScreen(),
  18. ];
  19. late int _selectedIndex = 0;
  20. void _onItemTapped(int index) {
  21. setState(() {
  22. _selectedIndex = index;
  23. });
  24. }
  25. @override
  26. Widget build(BuildContext context) {
  27. final body = Scaffold(
  28. body: IndexedStack(
  29. index: _selectedIndex,
  30. children: _widgetOptions,
  31. ),
  32. bottomNavigationBar: BottomNavigationBar(
  33. items: const <BottomNavigationBarItem>[
  34. BottomNavigationBarItem(
  35. icon: Icon(Icons.people),
  36. label: '通讯录',
  37. ),
  38. BottomNavigationBarItem(
  39. icon: Icon(Icons.face),
  40. label: '我的',
  41. ),
  42. ],
  43. currentIndex: _selectedIndex,
  44. iconSize: 20,
  45. selectedFontSize: 12,
  46. unselectedFontSize: 12,
  47. onTap: _onItemTapped,
  48. ),
  49. );
  50. return willPop(body);
  51. }
  52. int _noticeTime = 0;
  53. Widget willPop(Scaffold body) {
  54. return WillPopScope(
  55. child: body,
  56. onWillPop: () async {
  57. final now = DateTime.now().millisecondsSinceEpoch;
  58. if (_noticeTime + 3000 > now) {
  59. return true;
  60. } else {
  61. _noticeTime = now;
  62. showToast(
  63. "再次返回将会退出应用程序",
  64. context: context,
  65. position: StyledToastPosition.center,
  66. animation: StyledToastAnimation.scale,
  67. reverseAnimation: StyledToastAnimation.fade,
  68. duration: const Duration(seconds: 3),
  69. animDuration: const Duration(milliseconds: 300),
  70. curve: Curves.elasticOut,
  71. reverseCurve: Curves.linear,
  72. );
  73. return false;
  74. }
  75. },
  76. );
  77. }
  78. }