import 'dart:convert'; import 'package:fluro/fluro.dart'; import 'package:flutter/material.dart'; import 'package:sport/bean/forum.dart'; import 'package:sport/bean/game.dart'; import 'package:sport/bean/post.dart'; import 'package:sport/bean/post_user.dart'; import 'package:sport/bean/user.dart'; import 'package:sport/pages/social/user_detail_page.dart'; import '../application.dart'; import 'routes.dart'; typedef PageCreate = Future Function(int pageNum); class NavigatorUtil { static Future _navigateTo(BuildContext context, String path, {bool replace = false, bool clearStack = false, Duration transitionDuration = const Duration(milliseconds: 250), RouteTransitionsBuilder? transitionBuilder}) { if (Application.router != null) return Application.router!.navigateTo(context, path, replace: replace, clearStack: clearStack, transitionDuration: transitionDuration, transitionBuilder: transitionBuilder, transition: TransitionType.material); return Future.value(false); } static String toJson(Object value) { if (value == null) return ""; return Uri.encodeComponent(json.encode(value)); } static Future goPage(BuildContext context, WidgetBuilder builder, {RouteSettings? settings}) { return Navigator.push(context, MaterialPageRoute(builder: builder, settings: settings)); } static Future goPageRemoveUntil(BuildContext context, WidgetBuilder builder, {RouteSettings? settings}) { return Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: builder, settings: settings), (route) { if(route.settings.name == settings?.name) return false; return true; }); // Navigator.replace(context, oldRoute: MaterialPageRoute(builder: builder, settings: settings), newRoute: MaterialPageRoute(builder: builder, settings: settings)); } static Future goTransparentPage(BuildContext context, RoutePageBuilder pageBuilder, {RouteSettings? settings}) { return Navigator.push(context, PageRouteBuilder(pageBuilder: pageBuilder, settings: settings, opaque: false)); } static Future pushAndRemoveUntil(BuildContext context, WidgetBuilder builder, RouteSettings? settings) { return Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: builder, settings: settings), (check) { if (check.settings.name == "/") return true; return false; }); } ///跳转 uri: Routes.any static Future go(BuildContext context, String uri) { return _navigateTo(context, uri); } /// 首页 static void goHomePage(BuildContext context) { _navigateTo(context, Routes.home, clearStack: true); } /// social detail static void goSocialDetail(BuildContext context, Forum forum) { _navigateTo(context, "${Routes.socialDetail}?data=${toJson(forum)}"); } /// social post detail static void goSocialPostDetail(BuildContext context, Post post, {bool comment = false}) { _navigateTo(context, "${Routes.socialPostDetail}?data=${toJson(post)}&comment=$comment"); } /// social user detail static Future goSocialUserDetail(BuildContext context, PostUser user) { return NavigatorUtil.goTransparentPage(context, (context,_,__) => UserDetailPage(user)); // return _navigateTo(context, "${Routes.socialUserDetail}?data=${toJson(user)}"); } static Future goGameDetails(BuildContext context, {required GameInfoData details}) { return _navigateTo(context, "${Routes.gameDetails}?details=${toJson(details)}"); } static void goGameHistory(BuildContext context, GameInfoData details) { _navigateTo(context, "${Routes.sportHistory}?details=${toJson(details)}"); } static void goRankPeopleDetails(BuildContext context, {required User details}) { _navigateTo(context, "${Routes.rankPeopleDetails}?details=${toJson(details)}"); } static void goLogin(BuildContext context, {int type = 1}) { _navigateTo(context, "${Routes.login}?type=$type", clearStack: true); } static void goLicense(BuildContext context) { _navigateTo(context, "${Routes.license}"); } static void goAuthCode(BuildContext context, {required String phone}) { _navigateTo(context, "${Routes.authCode}?phone=$phone"); } static void goRegister(BuildContext context) { _navigateTo(context, "${Routes.register}"); } static void goBasicInfo(BuildContext context, {required String username}) { _navigateTo(context, "${Routes.basicInfo}?username=${username.isNotEmpty == true ? Uri.encodeComponent(username) : ""}"); } static void goFindPassword(BuildContext context) { _navigateTo(context, "${Routes.findPassword}"); } static void goAchievementDetails(BuildContext context, {required int id, int userId = 0, bool replace = false}) { _navigateTo(context, "${Routes.achievementDetail}?id=$id&userId=$userId", replace: replace); } static void goLevel(BuildContext context) { _navigateTo(context, Routes.level); } // goback // static void goBack(){ // _navigateTo.pop(context); // } }