navigation_util.dart 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. Future<T> routeToPage<T extends Object>(
  4. BuildContext context,
  5. Widget page, {
  6. bool forceCustomPageRoute = false,
  7. }) {
  8. if (Platform.isAndroid || forceCustomPageRoute) {
  9. return Navigator.of(context).push(
  10. _buildPageRoute(page),
  11. );
  12. } else {
  13. return Navigator.of(context).push(
  14. MaterialPageRoute(
  15. builder: (BuildContext context) {
  16. return page;
  17. },
  18. ),
  19. );
  20. }
  21. }
  22. void replacePage(BuildContext context, Widget page) {
  23. Navigator.of(context).pushReplacement(
  24. _buildPageRoute(page),
  25. );
  26. }
  27. PageRouteBuilder<T> _buildPageRoute<T extends Object>(Widget page) {
  28. return PageRouteBuilder(
  29. pageBuilder: (
  30. BuildContext context,
  31. Animation<double> animation,
  32. Animation<double> secondaryAnimation,
  33. ) {
  34. return page;
  35. },
  36. transitionsBuilder: (
  37. BuildContext context,
  38. Animation<double> animation,
  39. Animation<double> secondaryAnimation,
  40. Widget child,
  41. ) {
  42. return Align(
  43. child: FadeTransition(
  44. opacity: animation,
  45. child: child,
  46. ),
  47. );
  48. },
  49. transitionDuration: const Duration(milliseconds: 200),
  50. opaque: false,
  51. );
  52. }
  53. class TransparentRoute extends PageRoute<void> {
  54. TransparentRoute({
  55. @required this.builder,
  56. RouteSettings settings,
  57. }) : assert(builder != null),
  58. super(settings: settings, fullscreenDialog: false);
  59. final WidgetBuilder builder;
  60. @override
  61. bool get opaque => false;
  62. @override
  63. Color get barrierColor => null;
  64. @override
  65. String get barrierLabel => null;
  66. @override
  67. bool get maintainState => true;
  68. @override
  69. Duration get transitionDuration => const Duration(milliseconds: 350);
  70. @override
  71. Widget buildPage(
  72. BuildContext context,
  73. Animation<double> animation,
  74. Animation<double> secondaryAnimation,
  75. ) {
  76. final result = builder(context);
  77. return FadeTransition(
  78. opacity: Tween<double>(begin: 0, end: 1).animate(animation),
  79. child: Semantics(
  80. scopesRoute: true,
  81. explicitChildNodes: true,
  82. child: result,
  83. ),
  84. );
  85. }
  86. }