基础库
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

router_util.dart 3.5 KiB

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