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

81 lines
2.6 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. static Future openWebview(String url, BuildContext context) {
  52. if (url == null || url == '') {
  53. Logger.error('跳转链接不能为空');
  54. return Future.error('跳转链接不能为空');
  55. }
  56. SkipModel model = SkipModel();
  57. model.skipIdentifier = 'pub.flutter.url';
  58. model.url = url;
  59. RouterUtil.route(model, model.toJson(), context);
  60. }
  61. static void hideKeyboard(BuildContext context){
  62. try {
  63. FocusScopeNode currentFocus = FocusScope.of(context);
  64. if (!currentFocus.hasPrimaryFocus &&
  65. currentFocus.focusedChild != null) {
  66. FocusManager.instance.primaryFocus.unfocus();
  67. }
  68. // SystemChannels.textInput.invokeMethod('TextInput.hide');
  69. // FocusScope.of(context).requestFocus(FocusNode());
  70. }catch(e, s){
  71. Logger.error(e,s);
  72. }
  73. }
  74. }