基础库
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

router_util.dart 3.6 KiB

4 年前
4 年前
4 年前
4 年前
4 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:provider/provider.dart';
  5. import 'package:zhiying_comm/pages/login_page/account/login_account_page.dart';
  6. import 'package:zhiying_comm/pages/login_page/login_page.dart';
  7. import 'package:zhiying_comm/util/event_util/event_util.dart';
  8. import 'package:zhiying_comm/util/event_util/login_success_event.dart';
  9. import 'package:zhiying_comm/util/shared_prefe_util.dart';
  10. import 'package:zhiying_comm/zhiying_comm.dart';
  11. class RouterUtil {
  12. /*公共跳转
  13. * skipModel 跳转参数
  14. * data 额外参数
  15. * */
  16. static Future route(SkipModel skipModel, Map<String, dynamic> data,
  17. BuildContext context) async {
  18. if (skipModel.skipIdentifier == null || skipModel.skipIdentifier == '') {
  19. print('skipIdentifier 参数不存在,无法跳转页面');
  20. return Future.error('skipIdentifier 参数不存在,无法跳转页面');
  21. }
  22. // is_jump等于0不跳转
  23. if(!EmptyUtil.isEmpty(skipModel.isJump) && skipModel.isJump == '0') {
  24. return;
  25. }
  26. print('skipIdentifier: ${skipModel.skipIdentifier}');
  27. hideKeyboard(context);
  28. if (skipModel?.requiredLogin == '1') {
  29. UserInfoModel user =
  30. await Provider.of<UserInfoNotifier>(context, listen: false)
  31. .getUserInfoModel();
  32. print(user.toString());
  33. if (user?.token == null || user.token == '') {
  34. print('need login...');
  35. return RouterUtil.goLogin(context);
  36. }
  37. }
  38. Widget page = EmptyPage();
  39. if (PageFactory.hasRegisted(skipModel.skipIdentifier)) {
  40. page = PageFactory.create(skipModel.skipIdentifier, data);
  41. } else {
  42. var result = await NetUtil.post(
  43. '/api/v1/mod/${skipModel.skipIdentifier.toString()}',
  44. method: NetMethod.GET);
  45. Logger.debug(result);
  46. page = PageFactory.create('index', Map<String, dynamic>.from(result));
  47. }
  48. return Navigator.of(context)
  49. .push(CupertinoPageRoute(builder: (BuildContext context) {
  50. return page;
  51. }));
  52. }
  53. // 跳转登录
  54. static Future goLogin(BuildContext context) async{
  55. String isIosReview = await SharedPreferencesUtil.getStringValue(GlobalConfig.IS_IOS_REVIEW, defaultVal: '0');
  56. return Navigator.of(context)
  57. .push(CupertinoPageRoute(builder: (BuildContext context) {
  58. // 苹果审核登录样式
  59. if(Platform.isIOS && isIosReview == '1'){
  60. return LoginAccountPage(null);
  61. }
  62. return LoginPage();
  63. }));
  64. }
  65. // 打开网页
  66. static Future openWebview(String url, BuildContext context) {
  67. if (url == null || url == '') {
  68. Logger.error('跳转链接不能为空');
  69. return Future.error('跳转链接不能为空');
  70. }
  71. SkipModel model = SkipModel();
  72. model.skipIdentifier = 'pub.flutter.url';
  73. model.url = url;
  74. RouterUtil.route(model, model.toJson(), context);
  75. }
  76. // 回到首页
  77. static Future goBackHomePage(BuildContext context, {bool needUpdateAuth = true}) async{
  78. if(needUpdateAuth) {
  79. await TaobaoAuth.initAuth(context);
  80. EventUtil.instance.fire(LoginSuccessEvent());
  81. }
  82. Navigator.popUntil(context, ModalRoute.withName('/homePage'));
  83. }
  84. // 隐藏键盘
  85. static void hideKeyboard(BuildContext context){
  86. try {
  87. FocusScopeNode currentFocus = FocusScope.of(context);
  88. if (!currentFocus.hasPrimaryFocus &&
  89. currentFocus.focusedChild != null) {
  90. FocusManager.instance.primaryFocus.unfocus();
  91. }
  92. // SystemChannels.textInput.invokeMethod('TextInput.hide');
  93. // FocusScope.of(context).requestFocus(FocusNode());
  94. }catch(e, s){
  95. Logger.error(e,s);
  96. }
  97. }
  98. }