Created with <3 with dartpad.dev.
Created
November 15, 2023 00:33
-
-
Save hongsw/3a981e579019d1fceaf3ad437efd4883 to your computer and use it in GitHub Desktop.
Class X Animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
// 요구사항 : 이론 애니메이션과 클래스를 응용한 과제로써, | |
// 네모모양을 조절하고 색상도 변경하는 별도의 클래스를 만들고 | |
// 이를 한화면에 2개를 위아래로 표현한다. | |
// 만든 클래스에서 네임디드 파라미터를 이용하여 | |
// Tween Animation의 최종 사이즈와 색상을 지정하여 | |
// 버튼을 클릭했을때 동작되도록 한다. | |
void main() => runApp(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
home: Scaffold( | |
appBar: AppBar(title: const Text('Class X Animation')), | |
body: Column(children: [ | |
SizedBox( | |
height: 300.0, | |
child: AnimationExample(color: Colors.blue), | |
), | |
SizedBox( | |
height: 300.0, | |
child: AnimationExample(color: Colors.red), | |
) | |
]))); | |
} | |
} | |
class AnimationExample extends StatefulWidget { | |
final Color color; | |
const AnimationExample({Key? key, this.color = Colors.grey}) : super(key: key); | |
@override | |
_AnimationExampleState createState() => _AnimationExampleState(); | |
} | |
class _AnimationExampleState extends State<AnimationExample> | |
with SingleTickerProviderStateMixin { | |
late final AnimationController _controller; | |
late final Animation<double> _animation; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController( | |
duration: const Duration(seconds: 2), | |
vsync: this, | |
); | |
// Tween을 사용하여 시작과 끝 크기를 정의합니다. 여기서는 100에서 200으로 변합니다. | |
final sizeTween = Tween<double>(begin: 100.0, end: 200.0); | |
// CurvedAnimation을 사용하여 속도 곡선을 정의합니다. | |
final curve = CurvedAnimation( | |
parent: _controller, | |
curve: Curves.easeInCubic, | |
); | |
// Tween과 CurvedAnimation을 결합합니다. | |
_animation = sizeTween.animate(curve); | |
// 리니어한 애니메이션을 위해 Tween만 사용합니다. | |
// _animation = sizeTween.animate(_controller); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: AnimatedBuilder( | |
animation: _animation, | |
builder: (context, child) { | |
// _animation.value는 sizeTween에서 정의한 범위 내에서 변합니다. | |
return Column( | |
children: [ | |
TextButton( | |
style: ButtonStyle( | |
backgroundColor: MaterialStateProperty.all<Color>( | |
Colors.blue), // Set the background color to blue | |
foregroundColor: MaterialStateProperty.all<Color>( | |
Colors.white), // Set the text color to white | |
padding: MaterialStateProperty.all<EdgeInsetsGeometry>( | |
EdgeInsets.all( | |
16.0)), // Increase padding for larger touch area | |
), | |
onPressed: () { | |
if (_controller.status == AnimationStatus.completed) { | |
_controller.reverse(); | |
} else { | |
_controller.forward(); | |
} | |
}, | |
child: Text('Animate')), | |
Padding( | |
padding: EdgeInsets.symmetric(horizontal: 30, vertical: 30), | |
child: Container( | |
width: _animation.value, | |
height: _animation.value, | |
color: widget.color, | |
alignment: Alignment.center, | |
child: Text('Animating... ${widget.color.value}'), | |
), | |
), | |
], | |
); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment