bottom_navigation.dart 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import 'dart:io';
  2. import 'package:eitc_erm_app/utils/Component.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'home.dart';
  6. import 'mine.dart';
  7. void main() {
  8. WidgetsFlutterBinding.ensureInitialized();
  9. runApp(BottomNavigationWidget());
  10. }
  11. class BottomNavigationWidget extends StatefulWidget {
  12. @override
  13. _BottomNavigationWidgetState createState() => _BottomNavigationWidgetState();
  14. }
  15. class _BottomNavigationWidgetState extends State<BottomNavigationWidget> {
  16. final bottomNavigationColor = Colors.blue;
  17. int curIndex = 0;
  18. List<Widget> pages = [];
  19. int lastExitTime = 0;
  20. @override
  21. void initState() {
  22. super.initState();
  23. }
  24. @override
  25. Widget build(BuildContext context) {
  26. pages
  27. ..add(Home(context))
  28. ..add(Mine());
  29. return PopScope(
  30. canPop: !Platform.isAndroid,
  31. onPopInvokedWithResult: _onPopInvoke,
  32. child: Scaffold(
  33. body: pages[curIndex],
  34. bottomNavigationBar: BottomNavigationBar(
  35. items: [
  36. BottomNavigationBarItem(
  37. icon: Icon(Icons.home, color: bottomNavigationColor),
  38. label: '首页',
  39. // title: Text('home1', style: TextStyle(color: bottomNavigationColor))
  40. ),
  41. BottomNavigationBarItem(
  42. icon: Icon(Icons.person, color: bottomNavigationColor),
  43. label: '我的',
  44. // title: Text('home', style: TextStyle(color: bottomNavigationColor))
  45. )
  46. ],
  47. currentIndex: curIndex,
  48. onTap: (int index) {
  49. setState(() {
  50. curIndex = index;
  51. });
  52. },
  53. fixedColor: Colors.blue,
  54. type: BottomNavigationBarType.fixed,
  55. ),
  56. ));
  57. }
  58. void _onPopInvoke(bool didPop, dynamic result) {
  59. if (didPop) {
  60. return;
  61. }
  62. int now = DateTime.now().millisecondsSinceEpoch;
  63. if (lastExitTime == 0 || now - lastExitTime > 2000) {
  64. Component.toast("再次点击退出应用", 2);
  65. lastExitTime = now;
  66. return;
  67. }
  68. SystemNavigator.pop(animated: true);
  69. }
  70. }