navigator_util.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import 'dart:convert';
  2. import 'package:fluro/fluro.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:sport/bean/forum.dart';
  5. import 'package:sport/bean/game.dart';
  6. import 'package:sport/bean/post.dart';
  7. import 'package:sport/bean/post_user.dart';
  8. import 'package:sport/bean/user.dart';
  9. import 'package:sport/pages/social/user_detail_page.dart';
  10. import '../application.dart';
  11. import 'routes.dart';
  12. typedef PageCreate = Future<List> Function(int pageNum);
  13. class NavigatorUtil {
  14. static Future<dynamic> _navigateTo(BuildContext context, String path,
  15. {bool replace = false,
  16. bool clearStack = false,
  17. Duration transitionDuration = const Duration(milliseconds: 250),
  18. RouteTransitionsBuilder? transitionBuilder}) {
  19. if (Application.router != null)
  20. return Application.router!.navigateTo(context, path,
  21. replace: replace,
  22. clearStack: clearStack,
  23. transitionDuration: transitionDuration,
  24. transitionBuilder: transitionBuilder,
  25. transition: TransitionType.material);
  26. return Future.value(false);
  27. }
  28. static String toJson(Object value) {
  29. if (value == null) return "";
  30. return Uri.encodeComponent(json.encode(value));
  31. }
  32. static Future goPage(BuildContext context, WidgetBuilder builder, {RouteSettings? settings}) {
  33. return Navigator.push(context, MaterialPageRoute(builder: builder, settings: settings));
  34. }
  35. static Future goPageRemoveUntil(BuildContext context, WidgetBuilder builder, {RouteSettings? settings}) {
  36. return Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: builder, settings: settings), (route) {
  37. if(route.settings.name == settings?.name)
  38. return false;
  39. return true;
  40. });
  41. // Navigator.replace(context, oldRoute: MaterialPageRoute(builder: builder, settings: settings), newRoute: MaterialPageRoute(builder: builder, settings: settings));
  42. }
  43. static Future goTransparentPage(BuildContext context, RoutePageBuilder pageBuilder, {RouteSettings? settings}) {
  44. return Navigator.push(context, PageRouteBuilder(pageBuilder: pageBuilder, settings: settings, opaque: false));
  45. }
  46. static Future pushAndRemoveUntil(BuildContext context, WidgetBuilder builder, RouteSettings? settings) {
  47. return Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: builder, settings: settings), (check) {
  48. if (check.settings.name == "/") return true;
  49. return false;
  50. });
  51. }
  52. ///跳转 uri: Routes.any
  53. static Future go(BuildContext context, String uri) {
  54. return _navigateTo(context, uri);
  55. }
  56. /// 首页
  57. static void goHomePage(BuildContext context) {
  58. _navigateTo(context, Routes.home, clearStack: true);
  59. }
  60. /// social detail
  61. static void goSocialDetail(BuildContext context, Forum forum) {
  62. _navigateTo(context, "${Routes.socialDetail}?data=${toJson(forum)}");
  63. }
  64. /// social post detail
  65. static void goSocialPostDetail(BuildContext context, Post post, {bool comment = false}) {
  66. _navigateTo(context, "${Routes.socialPostDetail}?data=${toJson(post)}&comment=$comment");
  67. }
  68. /// social user detail
  69. static Future goSocialUserDetail(BuildContext context, PostUser user) {
  70. return NavigatorUtil.goTransparentPage(context, (context,_,__) => UserDetailPage(user));
  71. // return _navigateTo(context, "${Routes.socialUserDetail}?data=${toJson(user)}");
  72. }
  73. static Future<dynamic> goGameDetails(BuildContext context, {required GameInfoData details}) {
  74. return _navigateTo(context, "${Routes.gameDetails}?details=${toJson(details)}");
  75. }
  76. static void goGameHistory(BuildContext context, GameInfoData details) {
  77. _navigateTo(context, "${Routes.sportHistory}?details=${toJson(details)}");
  78. }
  79. static void goRankPeopleDetails(BuildContext context, {required User details}) {
  80. _navigateTo(context, "${Routes.rankPeopleDetails}?details=${toJson(details)}");
  81. }
  82. static void goLogin(BuildContext context, {int type = 1}) {
  83. _navigateTo(context, "${Routes.login}?type=$type", clearStack: true);
  84. }
  85. static void goLicense(BuildContext context) {
  86. _navigateTo(context, "${Routes.license}");
  87. }
  88. static void goAuthCode(BuildContext context, {required String phone}) {
  89. _navigateTo(context, "${Routes.authCode}?phone=$phone");
  90. }
  91. static void goRegister(BuildContext context) {
  92. _navigateTo(context, "${Routes.register}");
  93. }
  94. static void goBasicInfo(BuildContext context, {required String username}) {
  95. _navigateTo(context, "${Routes.basicInfo}?username=${username.isNotEmpty == true ? Uri.encodeComponent(username) : ""}");
  96. }
  97. static void goFindPassword(BuildContext context) {
  98. _navigateTo(context, "${Routes.findPassword}");
  99. }
  100. static void goAchievementDetails(BuildContext context, {required int id, int userId = 0, bool replace = false}) {
  101. _navigateTo(context, "${Routes.achievementDetail}?id=$id&userId=$userId", replace: replace);
  102. }
  103. static void goLevel(BuildContext context) {
  104. _navigateTo(context, Routes.level);
  105. }
  106. // goback
  107. // static void goBack(){
  108. // _navigateTo.pop(context);
  109. // }
  110. }