redirect.dart 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. class RedirectPage extends StatefulWidget {
  7. static bool _isBusy = false;
  8. static int count = 0;
  9. static bool get isBusy => _isBusy;
  10. static set isBusy(bool val) {
  11. print("isBusy -> $val");
  12. _isBusy = val;
  13. }
  14. const RedirectPage({super.key});
  15. @override
  16. State<StatefulWidget> createState() => _RedirectPageState();
  17. }
  18. class _RedirectPageState extends State<RedirectPage> {
  19. late final String targetName;
  20. bool isLoading = true;
  21. @override
  22. void initState() {
  23. RedirectPage.isBusy = true;
  24. RedirectPage.count++;
  25. targetName = Routes.parameters["name"]!;
  26. super.initState();
  27. WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
  28. wait();
  29. });
  30. }
  31. @override
  32. void dispose() {
  33. RedirectPage.count--;
  34. if (RedirectPage.count == 0) {
  35. RedirectPage.isBusy = false;
  36. }
  37. super.dispose();
  38. }
  39. void wait() {
  40. Future.delayed(const Duration(milliseconds: 900), () {
  41. setState(() {
  42. isLoading = false;
  43. });
  44. redirectTo();
  45. });
  46. }
  47. void redirectTo() {
  48. Future.delayed(const Duration(milliseconds: 100), () {
  49. print('🍤');
  50. print(targetName);
  51. print('🍇');
  52. Get.offAllNamed(targetName, id: NavIds.HOME);
  53. RedirectPage.isBusy = false;
  54. });
  55. }
  56. @override
  57. Widget build(BuildContext context) {
  58. if (isLoading) {
  59. return const Center(
  60. child: SizedBox(
  61. width: 50,
  62. height: 50,
  63. child: CircularProgressIndicator(),
  64. ),
  65. );
  66. }
  67. return const SizedBox();
  68. }
  69. }