基础库
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

96 行
3.2 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){
  72. Navigator.popUntil(context, ModalRoute.withName('/homePage'));
  73. }
  74. // 隐藏键盘
  75. static void hideKeyboard(BuildContext context){
  76. try {
  77. FocusScopeNode currentFocus = FocusScope.of(context);
  78. if (!currentFocus.hasPrimaryFocus &&
  79. currentFocus.focusedChild != null) {
  80. FocusManager.instance.primaryFocus.unfocus();
  81. }
  82. // SystemChannels.textInput.invokeMethod('TextInput.hide');
  83. // FocusScope.of(context).requestFocus(FocusNode());
  84. }catch(e, s){
  85. Logger.error(e,s);
  86. }
  87. }
  88. }