123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'device_utils.dart';
- enum AnimationType { rightToLeft, leftToRight, bottomToTop, topToBottom }
- class GotoPage {
-
- static void _switchPageNotNamed({
- required BuildContext context,
- required dynamic targetPage,
- AnimationType? animationType = AnimationType.rightToLeft,
- int startMills = 300,
- Object? arguments,
- num isReplace = 0,
- void Function(dynamic value)? cb,
- }) {
- PageRoute pageRoute;
- if (DeviceUtils.isAndroid) {
-
- pageRoute = PageRouteBuilder(
- settings: RouteSettings(name: context.widget.key.toString(), arguments: arguments),
- transitionDuration: Duration(milliseconds: startMills),
- transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
- return SlideTransition(
- position: _createSlideTween(animationType).animate(animation),
- child: child,
- );
- },
- pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
- return targetPage;
- },
- );
- } else if (DeviceUtils.isIOS) {
-
- pageRoute = CupertinoPageRoute(
- builder: (BuildContext context) {
- return targetPage;
- },
- settings: RouteSettings(name: context.widget.key.toString(), arguments: arguments),
- );
- } else {
-
- pageRoute = MaterialPageRoute(
- builder: (BuildContext context) {
- return targetPage;
- },
- settings: RouteSettings(name: context.widget.key.toString(), arguments: arguments),
- );
- }
- if (isReplace == 0) {
-
- Navigator.of(context).push(pageRoute).then((value) {
- if (cb != null) {
- cb(value);
- }
- });
- } else if (isReplace == 1) {
-
- Navigator.of(context).pushReplacement(pageRoute).then((value) {
- if (cb != null) {
- cb(value);
- }
- });
- } else {
-
- Navigator.of(context).pushAndRemoveUntil(pageRoute, (Route<dynamic>? route) => route == null).then((value) {
- if (cb != null) {
- cb(value);
- }
- });
- }
- }
-
- static void _switchPageByNamed({
- required BuildContext context,
- required String targetPage,
- AnimationType? animationType = AnimationType.rightToLeft,
- int startMills = 300,
- Object? arguments,
- num isReplace = 0,
- void Function(dynamic value)? cb,
- }) {
- if (isReplace == 0) {
-
- Navigator.of(context).pushNamed(targetPage, arguments: arguments).then((value) => {
- if (cb != null) {cb(value)}
- });
- } else if (isReplace == 1) {
-
- Navigator.of(context).pushReplacementNamed(targetPage, arguments: arguments).then((value) => {
- if (cb != null) {cb(value)}
- });
- } else {
-
- Navigator.of(context)
- .pushNamedAndRemoveUntil(targetPage, (Route route) => false, arguments: arguments)
- .then((value) => {
- if (cb != null) {cb(value)}
- });
- }
- }
-
- static void pushPage({
- required BuildContext context,
- required dynamic targetPage,
- AnimationType? animationType = AnimationType.rightToLeft,
- int startMills = 300,
- Object? arguments,
- num isReplace = 0,
- void Function(dynamic value)? cb,
- }) {
- bool _isPushNamed = targetPage is String ? true : false;
- if (!_isPushNamed) {
-
- _switchPageNotNamed(
- context: context,
- targetPage: targetPage,
- animationType: animationType,
- startMills: startMills,
- arguments: arguments,
- isReplace: isReplace,
- cb: cb,
- );
- } else {
-
- _switchPageByNamed(
- context: context,
- targetPage: targetPage,
- animationType: animationType,
- startMills: startMills,
- arguments: arguments,
- isReplace: isReplace,
- cb: cb,
- );
- }
- }
-
- static void pushPageBySlide({
- required BuildContext context,
- required dynamic targetPage,
-
- AnimationType? animationType = AnimationType.rightToLeft,
- bool? isPushNamed = true,
- bool opaque = true,
- Object? arguments,
- int startMills = 300,
- num isReplace = 0,
- void Function(dynamic value)? cb,
- }) {
- bool _isPushNamed = targetPage is String ? true : false;
- if (_isPushNamed) {
-
- _switchPageByNamed(
- context: context, targetPage: targetPage, arguments: arguments, isReplace: isReplace, cb: cb);
- } else {
-
- PageRoute pageRoute = PageRouteBuilder(
- settings: RouteSettings(name: context.widget.key.toString(), arguments: arguments),
-
- opaque: opaque,
- pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
- return targetPage;
- },
- transitionDuration: Duration(milliseconds: startMills),
-
- transitionsBuilder:
- (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
- return SlideTransition(
- position: _createSlideTween(animationType).animate(animation),
- child: child,
- );
- },
- );
- if (isReplace == 0) {
-
- Navigator.of(context).push(pageRoute).then((value) {
- if (cb != null) {
- cb(value);
- }
- });
- } else if (isReplace == 1) {
-
- Navigator.of(context).pushReplacement(pageRoute).then((value) {
- if (cb != null) {
- cb(value);
- }
- });
- } else {
-
- Navigator.of(context).pushAndRemoveUntil(pageRoute, (Route<dynamic>? route) => route == null).then((value) {
- if (cb != null) {
- cb(value);
- }
- });
- }
- }
- }
- static _createSlideTween(animationType) {
- Offset currentOffset;
- switch (animationType) {
- case AnimationType.rightToLeft:
- currentOffset = Offset(1, 0);
- break;
- case AnimationType.leftToRight:
- currentOffset = Offset(-1, 0);
- break;
- case AnimationType.bottomToTop:
- currentOffset = Offset(0, 1);
- break;
- case AnimationType.topToBottom:
- currentOffset = Offset(0, -1);
- break;
- default:
- currentOffset = Offset(1, 0);
- }
- return Tween<Offset>(
- begin: currentOffset,
- end: Offset.zero,
- );
- }
-
- static void pushPageByFade({
- required BuildContext context,
- required Widget targetPage,
- bool opaque = false,
- dynamic arguments,
- int startMills = 300,
- num isReplace = 0,
- Function(dynamic value)? cb,
- }) {
- PageRoute pageRoute = PageRouteBuilder(
- settings: RouteSettings(name: context.widget.key.toString(), arguments: arguments ?? null),
-
- opaque: opaque,
- pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
- return targetPage;
- },
- transitionDuration: Duration(milliseconds: startMills),
-
- transitionsBuilder:
- (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
- return FadeTransition(
- opacity: animation,
- child: child,
- );
- },
- );
- if (isReplace == 0) {
-
- Navigator.of(context).push(pageRoute).then((value) {
- if (cb != null) {
- cb(value);
- }
- });
- } else if (isReplace == 1) {
-
- Navigator.of(context).pushReplacement(pageRoute).then((value) {
- if (cb != null) {
- cb(value);
- }
- });
- } else {
-
- Navigator.of(context).pushAndRemoveUntil(pageRoute, (Route<dynamic>? route) => route == null).then((value) {
- if (cb != null) {
- cb(value);
- }
- });
- }
- }
- static Route _createRoute(parentContext, Widget targetPage, int startMills, int reversMills, bool opaque, {dynamic arguments}) {
- return PageRouteBuilder<void>(
- settings: RouteSettings(name: parentContext?.widget?.key ?? '', arguments: arguments ?? null),
- opaque: opaque,
-
- pageBuilder: (context, animation, secondaryAnimation) {
- return targetPage;
- },
- transitionDuration: Duration(milliseconds: startMills),
- reverseTransitionDuration: Duration(milliseconds: reversMills),
- transitionsBuilder: (context, animation, secondaryAnimation, child) {
- var rectAnimation = _createTween(parentContext).chain(CurveTween(curve: Curves.ease)).animate(animation);
- return Stack(
- children: [
- PositionedTransition(rect: rectAnimation, child: child),
- ],
- );
- },
- );
- }
- static Tween<RelativeRect> _createTween(BuildContext context) {
- var windowSize = MediaQuery.of(context).size;
- var box = context.findRenderObject() as RenderBox;
- var rect = box.localToGlobal(Offset.zero) & box.size;
- var relativeRect = RelativeRect.fromSize(rect, windowSize);
- return RelativeRectTween(
- begin: relativeRect,
- end: RelativeRect.fill,
- );
- }
- }
|