LoadingPage.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_habit/common/BaseArchitectural.dart';
  4. import 'package:flutter_habit/common/I18N.dart';
  5. import 'package:flutter_habit/common/LocalData.dart';
  6. import 'package:flutter_habit/common/SqfliteDataBase.dart';
  7. import 'package:flutter_habit/common/provider/ConfigProvider.dart';
  8. import 'package:flutter_habit/common/provider/DataProvider.dart';
  9. import 'package:flutter_habit/common/provider/NotificationProvider.dart';
  10. import 'package:flutter_habit/common/provider/UserProvider.dart';
  11. import 'package:flutter_habit/common/provider/ThemeProvider.dart';
  12. import 'package:flutter_habit/view/HomePage.dart';
  13. import 'package:provider/provider.dart';
  14. class LoadingPage extends StatelessWidget {
  15. @override
  16. Widget build(BuildContext context) {
  17. return MultiProvider(
  18. providers: [
  19. ChangeNotifierProvider<LoadingPageService>(
  20. create: (_) => LoadingPageService(context)),
  21. ],
  22. child: _LoadingPageView(),
  23. );
  24. }
  25. }
  26. // service
  27. class LoadingPageService extends BaseService {
  28. LoadingPageService(BuildContext context) : super(context);
  29. Timer timer;
  30. bool isLoaded;
  31. Future<void> loadData(BuildContext context) async {
  32. // ================================== init ==================================
  33. await LocalData.init();
  34. // LocalData.getInstance().clear();
  35. I18N.init();
  36. Provider.of<ThemeProvider>(context, listen: false).init();
  37. Provider.of<UserProvider>(context, listen: false).init();
  38. Provider.of<ConfigProvider>(context, listen: false).init();
  39. await Provider.of<NotificationProvider>(context, listen: false).init();
  40. await SqfliteDataBase.init();
  41. // await SqfliteDataBase.resetTables();
  42. await Provider.of<DataProvider>(context, listen: false).init();
  43. isLoaded = true;
  44. // ================================== over ==================================
  45. }
  46. @override
  47. void init(BuildContext context) {
  48. super.init(context);
  49. isLoaded = false;
  50. timer = null;
  51. startCountDown(context);
  52. loadData(context);
  53. }
  54. void startCountDown(BuildContext context) {
  55. if (timer == null) {
  56. int minDuration = 3;
  57. timer = Timer.periodic(Duration(seconds: 1), (t) {
  58. minDuration--;
  59. if (isLoaded && minDuration <= 0) {
  60. timer.cancel();
  61. scalaJumpToHomePage(context);
  62. }
  63. });
  64. }
  65. }
  66. void scalaJumpToHomePage(BuildContext context) {
  67. Navigator.of(context).pushReplacement(PageRouteBuilder(
  68. transitionDuration: Duration(milliseconds: 600),
  69. pageBuilder: (context, animation, secondaryAnimation) =>
  70. // FadeTransition(opacity: animation, child: HomePage())));
  71. ScaleTransition(scale: animation, child: HomePage())));
  72. }
  73. @override
  74. void dispose() {
  75. super.dispose();
  76. if (timer != null) {
  77. timer.cancel();
  78. }
  79. }
  80. }
  81. class _LoadingPageView extends StatelessWidget {
  82. @override
  83. Widget build(BuildContext context) {
  84. LoadingPageService service =
  85. Provider.of<LoadingPageService>(context, listen: false);
  86. return ConstrainedBox(
  87. constraints: BoxConstraints.expand(),
  88. child: Image.asset(
  89. "res/welcome.png",
  90. fit: BoxFit.fitWidth,
  91. ),
  92. );
  93. }
  94. }