navigation_util.dart 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. Future<T> routeToPage<T extends Object>(BuildContext context, Widget page) {
  4. if (Platform.isAndroid) {
  5. return Navigator.of(context).push(
  6. _buildPageRoute(page),
  7. );
  8. } else {
  9. return Navigator.of(context).push(
  10. MaterialPageRoute(
  11. builder: (BuildContext context) {
  12. return page;
  13. },
  14. ),
  15. );
  16. }
  17. }
  18. void replacePage(BuildContext context, Widget page) {
  19. Navigator.of(context).pushReplacement(
  20. _buildPageRoute(page),
  21. );
  22. }
  23. PageRouteBuilder<T> _buildPageRoute<T extends Object>(Widget page) {
  24. return PageRouteBuilder(
  25. pageBuilder: (
  26. BuildContext context,
  27. Animation<double> animation,
  28. Animation<double> secondaryAnimation,
  29. ) {
  30. return page;
  31. },
  32. transitionsBuilder: (
  33. BuildContext context,
  34. Animation<double> animation,
  35. Animation<double> secondaryAnimation,
  36. Widget child,
  37. ) {
  38. return Align(
  39. child: FadeTransition(
  40. opacity: animation,
  41. child: child,
  42. ),
  43. );
  44. },
  45. transitionDuration: Duration(milliseconds: 200),
  46. opaque: false,
  47. );
  48. }