import 'package:flutter/material.dart';
import 'package:page_transition/page_transition.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Page Transition Example',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: MyHomePage(),
// Custom route generator for named routes
onGenerateRoute: (settings) {
switch (settings.name) {
case '/second':
return PageTransition(
child: SecondPage(),
type: PageTransitionType.theme,
settings: settings,
reverseDuration: Duration(seconds: 3),
);
default:
return null;
}
},
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Page Transition Example'),
),
body: Center(
child: ListView(
children: <Widget>[
// Button for triggering Fade Transition
ElevatedButton(
child: Text('Fade Transition'),
onPressed: () {
Navigator.push(
context,
PageTransition(
type: PageTransitionType.fade,
child: SecondPage(),
),
);
},
),
SizedBox(height: 16),
// Button for triggering Left To Right Transition
ElevatedButton(
child: Text('Left To Right Transition'),
onPressed: () {
Navigator.push(
context,
PageTransition(
type: PageTransitionType.leftToRight,
child: SecondPage(),
),
);
},
),
SizedBox(height: 16),
// Button for triggering Right To Left Transition (iOS SwipeBack)
ElevatedButton(
child: Text('Right To Left Transition (iOS SwipeBack)'),
onPressed: () {
Navigator.push(
context,
PageTransition(
type: PageTransitionType.rightToLeft,
isIos: true,
child: SecondPage(),
),
);
},
),
SizedBox(height: 16),
// Button for triggering Left To Right with Fade Transition
ElevatedButton(
child: Text('Left To Right with Fade Transition'),
onPressed: () {
Navigator.push(
context,
PageTransition(
type: PageTransitionType.leftToRightWithFade,
alignment: Alignment.topCenter,
child: SecondPage(),
),
);
},
),
SizedBox(height: 16),
// Button for triggering Right To Left Pop Transition
ElevatedButton(
child: Text('Right To Left Pop Transition'),
onPressed: () {
Navigator.push(
context,
PageTransition(
alignment: Alignment.bottomCenter,
curve: Curves.easeInOut,
duration: Duration(milliseconds: 600),
reverseDuration: Duration(milliseconds: 600),
type: PageTransitionType.rightToLeftPop,
child: SecondPage(),
childCurrent: MyHomePage(),
),
);
},
),
],
),
),
);
}
}
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text('This is the second page.'),
],
),
),
);
}
}