基础库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

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