import
'package:flutter/material.dart'
;
void
main() {
runApp(
MyApp(),
);
}
class
MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return
MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green,
),
debugShowCheckedModeBanner:
false
,
home: FirstScreen(),
);
}
}
class
FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return
Scaffold(
appBar: AppBar(
title: Text(
'First Screen'
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Hero(
tag:
'Screen 1'
,
child: Container(
width: 100,
height: 100,
color: Colors.green,
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
Navigator.of(context).push(
PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) {
return
SecondScreen();
},
transitionsBuilder:
(context, animation, secondaryAnimation, child) {
const
begin = Offset(1.0, 0.0);
const
end = Offset.zero;
const
curve = Curves.easeInOut;
var tween = Tween(begin: begin, end: end)
.chain(CurveTween(curve: curve));
var offsetAnimation = animation.drive(tween);
return
SlideTransition(
position: offsetAnimation,
child: child,
);
},
),
);
},
child: Text(
'Go to Second Screen'
),
),
],
),
),
);
}
}
class
SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return
Scaffold(
appBar: AppBar(
title: Text(
'Second Screen'
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Hero(
tag:
'Screen 2'
,
child: Container(
width: 200,
height: 200,
color: Colors.orange,
),
),
],
),
),
);
}
}