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

65 lines
2.1 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:provider/provider.dart';
  3. import 'package:zhiying_comm/pages/login_page/login_page.dart';
  4. import 'package:zhiying_comm/zhiying_comm.dart';
  5. class RouterUtil {
  6. /*公共跳转
  7. * skipModel 跳转参数
  8. * data 额外参数
  9. * */
  10. static Future route(SkipModel skipModel, Map<String, dynamic> data,
  11. BuildContext context) async {
  12. if (skipModel.skipIdentifier == null || skipModel.skipIdentifier == '') {
  13. print('skipIdentifier 参数不存在,无法跳转页面');
  14. return Future.error('skipIdentifier 参数不存在,无法跳转页面');
  15. }
  16. print('skipIdentifier: ${skipModel.skipIdentifier}');
  17. if (skipModel?.requiredLogin == '1') {
  18. UserInfoModel user =
  19. await Provider.of<UserInfoNotifier>(context, listen: false)
  20. .getUserInfoModel();
  21. print(user.toString());
  22. if (user?.token == null || user.token == '') {
  23. print('need login...');
  24. RouterUtil.goLogin(context);
  25. }
  26. }
  27. Widget page = EmptyPage();
  28. if (PageFactory.hasRegisted(skipModel.skipIdentifier)) {
  29. page = PageFactory.create(skipModel.skipIdentifier, data);
  30. } else {
  31. var result = await NetUtil.post(
  32. '/api/v1/mod/${skipModel.skipIdentifier.toString()}',
  33. method: NetMethod.GET);
  34. Logger.debug(result);
  35. page = PageFactory.create('index', Map<String, dynamic>.from(result));
  36. }
  37. return Navigator.of(context)
  38. .push(MaterialPageRoute(builder: (BuildContext context) {
  39. return page;
  40. }));
  41. }
  42. // 跳转登录
  43. static Future goLogin(BuildContext context) {
  44. return Navigator.of(context)
  45. .push(MaterialPageRoute(builder: (BuildContext context) {
  46. return LoginPage();
  47. }));
  48. }
  49. static Future openWebview(String url, BuildContext context) {
  50. if (url == null || url == '') {
  51. Logger.error('跳转链接不能为空');
  52. return Future.error('跳转链接不能为空');
  53. }
  54. SkipModel model = SkipModel();
  55. model.skipIdentifier = 'pub.flutter.url';
  56. model.url = url;
  57. RouterUtil.route(model, null, context);
  58. }
  59. }