navigator_util.dart 4.2 KB

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