redirect.dart 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. import 'package:vitalapp/routes/nav_ids.dart';
  5. import 'package:vitalapp/routes/routes.dart';
  6. import 'package:vitalapp/store/store.dart';
  7. class RedirectPage extends StatefulWidget {
  8. static bool _isBusy = false;
  9. static int count = 0;
  10. static bool get isBusy => _isBusy;
  11. static set isBusy(bool val) {
  12. print("isBusy -> $val");
  13. _isBusy = val;
  14. }
  15. const RedirectPage({super.key});
  16. @override
  17. State<StatefulWidget> createState() => _RedirectPageState();
  18. }
  19. class _RedirectPageState extends State<RedirectPage> {
  20. late final String targetName;
  21. late final String? targetPatientInfo;
  22. bool isLoading = true;
  23. @override
  24. void initState() {
  25. RedirectPage.isBusy = true;
  26. RedirectPage.count++;
  27. targetName = Routes.parameters["name"]!;
  28. super.initState();
  29. WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
  30. wait();
  31. });
  32. }
  33. @override
  34. void dispose() {
  35. RedirectPage.count--;
  36. if (RedirectPage.count == 0) {
  37. RedirectPage.isBusy = false;
  38. }
  39. super.dispose();
  40. }
  41. void wait() {
  42. Future.delayed(const Duration(milliseconds: 300), () {
  43. setState(() {
  44. isLoading = false;
  45. });
  46. redirectTo();
  47. });
  48. }
  49. void redirectTo() {
  50. Future.delayed(const Duration(milliseconds: 100), () {
  51. Get.offAllNamed(targetName, id: NavIds.HOME);
  52. // Routes.parameters = {};
  53. RedirectPage.isBusy = false;
  54. });
  55. }
  56. @override
  57. Widget build(BuildContext context) {
  58. if (isLoading) {
  59. Store.app.setBusy("加载中...");
  60. } else {
  61. Store.app.busy = false;
  62. }
  63. return const SizedBox();
  64. }
  65. }