基础库
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

107 líneas
3.9 KiB

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