Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented strongly-typed page transition types. #2

Merged
merged 1 commit into from
Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ It is really easy to use!
## Examples

```dart
Navigator.push(context,PageTransition(type:'fade', child: DetailScreen()));
Navigator.push(context,PageTransition(type: PageTransitionType.fade, child: DetailScreen()));

Navigator.push(context,PageTransition(type:'leftToRigth', child: DetailScreen()));
Navigator.push(context,PageTransition(type: PageTransitionType.leftToRight, child: DetailScreen()));

Navigator.push(context,PageTransition(type:'scale',aligment: Alignment.bottomCenter, child: DetailScreen()));
Navigator.push(context,PageTransition(type: PageTransitionType.scale, alignment: Alignment.bottomCenter, child: DetailScreen()));

Navigator.push(context,PageTransition(type:'size', aligment: Alignment.bottomCenter,child: DetailScreen()));
Navigator.push(context,PageTransition(type: PageTransitionType.size, alignment: Alignment.bottomCenter,child: DetailScreen()));

Navigator.push(context,PageTransition(type:'rotate', duration: Duration(second:1), child: DetailScreen()));
Navigator.push(context,PageTransition(type: PageTransitionType.rotate, duration: Duration(second:1), child: DetailScreen()));
```
## Usage for Named Route Parameters
First you have to add MaterialApp property name onGenerateRoute like below and in switch cases you can Transition your new routes;
```dart
onGenerateRoute: (settings) {
switch (settings.name) {
case '/second':
return PageTransition(child: SecondPage(), type: 'scale');
return PageTransition(child: SecondPage(), type: PageTransitionType.scale);
break;
default:
return null;
Expand All @@ -38,7 +38,7 @@ After that you can route new route
Navigator.pushNamed(context, '/second');
```
## Type Of transition
`fade, rightToLeft, leftToright, UpToDown, DownToUp, scale(with alignment) ,rotate(with alignment), size(with alignment)`
`fade, rightToLeft, leftToRight, upToDown, downToUp, scale(with alignment), rotate(with alignment), size(with alignment)`

## Curves
You can use any type of CurvedAnimation Curves
Expand Down
18 changes: 9 additions & 9 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ class MyHomePage extends StatelessWidget {
child: Text('Fade Second Page'),
onPressed: () {
Navigator.push(
context, PageTransition(type: 'fade', child: SecondPage()));
context, PageTransition(type: PageTransitionType.fade, child: SecondPage()));
},
),
RaisedButton(
child: Text('Left To Right Slide Second Page'),
onPressed: () {
Navigator.push(context,
PageTransition(type: 'leftToRight', child: SecondPage()));
PageTransition(type: PageTransitionType.leftToRight, child: SecondPage()));
},
),
RaisedButton(
Expand All @@ -53,7 +53,7 @@ class MyHomePage extends StatelessWidget {
PageTransition(
alignment: Alignment.bottomCenter,
curve: Curves.bounceOut,
type: 'size',
type: PageTransitionType.size,
child: SecondPage()));
},
),
Expand All @@ -64,7 +64,7 @@ class MyHomePage extends StatelessWidget {
context,
PageTransition(
curve: Curves.bounceOut,
type: 'rotate',
type: PageTransitionType.rotate,
alignment: Alignment.topCenter,
child: SecondPage()));
},
Expand All @@ -76,30 +76,30 @@ class MyHomePage extends StatelessWidget {
context,
PageTransition(
curve: Curves.linear,
type: 'scale',
type: PageTransitionType.scale,
alignment: Alignment.topCenter,
child: SecondPage()));
},
),
RaisedButton(
child: Text('Upto Down Second Page'),
child: Text('Up to Down Second Page'),
onPressed: () {
Navigator.push(
context,
PageTransition(
curve: Curves.linear,
type: 'upToDown',
type: PageTransitionType.upToDown,
child: SecondPage()));
},
),
RaisedButton(
child: Text('Down To Up Second Page'),
child: Text('Down to Up Second Page'),
onPressed: () {
Navigator.push(
context,
PageTransition(
curve: Curves.linear,
type: 'DownToUp',
type: PageTransitionType.downToUp,
child: SecondPage()));
},
),
Expand Down
29 changes: 20 additions & 9 deletions lib/page_transition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@ library page_transition;

import 'package:flutter/material.dart';

enum PageTransitionType {
fade,
rightToLeft,
leftToRight,
upToDown,
downToUp,
scale,
rotate,
size,
}

class PageTransition<T> extends PageRouteBuilder<T> {
final Widget child;
final String type;
final PageTransitionType type;
final Curve curve;
final Alignment alignment;
final Duration duration;
Expand All @@ -27,10 +38,10 @@ class PageTransition<T> extends PageRouteBuilder<T> {
Animation<double> secondaryAnimation,
Widget child) {
switch (type) {
case 'fade':
case PageTransitionType.fade:
return FadeTransition(opacity: animation, child: child);
break;
case 'rightToLeft':
case PageTransitionType.rightToLeft:
return SlideTransition(
transformHitTests: true,
position: new Tween<Offset>(
Expand All @@ -46,7 +57,7 @@ class PageTransition<T> extends PageRouteBuilder<T> {
),
);
break;
case 'leftToRight':
case PageTransitionType.leftToRight:
return SlideTransition(
transformHitTests: true,
position: Tween<Offset>(
Expand All @@ -62,7 +73,7 @@ class PageTransition<T> extends PageRouteBuilder<T> {
),
);
break;
case 'upToDown':
case PageTransitionType.upToDown:
return SlideTransition(
transformHitTests: true,
position: Tween<Offset>(
Expand All @@ -78,7 +89,7 @@ class PageTransition<T> extends PageRouteBuilder<T> {
),
);
break;
case 'DownToUp':
case PageTransitionType.downToUp:
return SlideTransition(
transformHitTests: true,
position: Tween<Offset>(
Expand All @@ -94,7 +105,7 @@ class PageTransition<T> extends PageRouteBuilder<T> {
),
);
break;
case 'scale':
case PageTransitionType.scale:
return ScaleTransition(
alignment: alignment,
scale: CurvedAnimation(
Expand All @@ -108,7 +119,7 @@ class PageTransition<T> extends PageRouteBuilder<T> {
child: child,
);
break;
case 'rotate':
case PageTransitionType.rotate:
return new RotationTransition(
alignment: alignment,
turns: animation,
Expand All @@ -122,7 +133,7 @@ class PageTransition<T> extends PageRouteBuilder<T> {
),
);
break;
case 'size':
case PageTransitionType.size:
return Align(
alignment: alignment,
child: SizeTransition(
Expand Down