λλ νλ¬ν°λ₯Ό λ°°μΈλ Navigator.of(context).push()λ₯Ό ν΅ν΄ λ¨μ νμ΄μ§ μ νλ§ λλμ€ μμλ€.
νμ§λ§ 곡μλ¬Έμλ₯Ό 보λμ€ PageRouteBuilderλ₯Ό ν΅ν΄ μ νμ μ’λ λ νΉνκ² ν μ μλ€λκ²μ μμλ€.
μ¬μ©λ²μ λ§€μ° κ°λ¨νλ€.
Route _createRoute() {
return PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => const Page2(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
const begin = Offset(0.0, 1.0);
const end = Offset.zero;
final tween = Tween(begin: begin, end: end);
final offsetAnimation = animation.drive(tween);
return child;
},
);
}
μ μ½λκ° μΆκ°ν΄μ£Όλ©΄ λλ€.
μνμ΄μ§κ° λ°μμλΆν° μμͺ½μΌλ‘ μ¬λΌμ€κ² λλ―λ‘ offsetμ μ£Όκ² λλ€.
begin λλ end κ°μ (0.0~1.0) κ°μΌλ‘ μ€μ
μ΄λ CurveTweenμ μ¬μ©νμ¬ ν¨κ³Όλ₯Ό μ€μ μλ€.
var curve = Curves.ease;
var curveTween = CurveTween(curve: curve);
μ 체μ½λ
import 'package:flutter/material.dart';
void main() => runApp(
MyApp(),
);
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.of(context).push(
_createRoute(),
);
},
child: Text('Go'),
),
),
);
}
}
// https://docs.flutter.dev/cookbook/animation/page-route-animation
// μ€κ°μμ νμ΄μ§μ΄λμ ν¨κ³Όλ₯Ό μ€μ μμ
Route _createRoute() {
return PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => Page2(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
const begin = Offset(0.0, 1.0);
const end = Offset.zero;
const curve = Curves.ease;
final tween = Tween(begin: begin, end: end);
final curvedAnimation = CurvedAnimation(parent: animation, curve: curve);
return SlideTransition(
position: tween.animate(curvedAnimation),
child: child,
);
},
);
}
class Page2 extends StatelessWidget {
const Page2({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Text('Page 2'),
),
);
}
}
μ λ΄μ©μ 곡μλ¬Έμλ₯Ό μ°Έκ³ νμ¬ μμ±νμμ΅λλ€.
https://docs.flutter.dev/cookbook/animation/page-route-animation