基础库
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.
 
 
 
 
 

87 line
2.8 KiB

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