基础库
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

router_util.dart 4.0 KiB

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