app_navigator.dart 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import 'package:flutter/material.dart';
  2. /// Navigator工具类
  3. /// 更推荐使用'routers/fluro_navigator.dart'
  4. class AppNavigator {
  5. static push(BuildContext context, Widget scene) {
  6. Navigator.push(
  7. context,
  8. MaterialPageRoute(
  9. builder: (BuildContext context) => scene,
  10. ),
  11. );
  12. }
  13. /// 替换页面 当新的页面进入后,之前的页面将执行dispose方法
  14. static pushReplacement(BuildContext context, Widget scene) {
  15. Navigator.pushReplacement(
  16. context,
  17. MaterialPageRoute(
  18. builder: (BuildContext context) => scene,
  19. )
  20. );
  21. }
  22. /// 指定页面加入到路由中,然后将其他所有的页面全部pop
  23. static pushAndRemoveUntil(BuildContext context, Widget scene) {
  24. Navigator.pushAndRemoveUntil(
  25. context,
  26. MaterialPageRoute(
  27. builder: (BuildContext context) => scene,
  28. ), (route) => route == null
  29. );
  30. }
  31. static pushResult(BuildContext context, Widget scene, Function(Object) function) {
  32. Navigator.push(
  33. context,
  34. MaterialPageRoute(
  35. builder: (BuildContext context) => scene,
  36. ),
  37. ).then((result){
  38. // 页面返回result为null
  39. if (result == null){
  40. return;
  41. }
  42. function(result);
  43. }).catchError((error) {
  44. print("$error");
  45. });
  46. }
  47. /// 返回
  48. static void goBack(BuildContext context) {
  49. Navigator.pop(context);
  50. }
  51. /// 带参数返回
  52. static void goBackWithParams(BuildContext context, result) {
  53. Navigator.pop(context, result);
  54. }
  55. }