Flutter는 애니메이션 기능을 통해 사용자 경험(UX)을 개선할 수 있습니다.
이 글에서는 Flutter에서 버튼 클릭 시 색상이 부드럽게 변하는 애니메이션을 구현하는 방법을 단계별로 설명합니다.
Flutter 프로젝트를 설정하고 애니메이션을 구현하기 위한 준비 과정을 진행합니다.
터미널에서 다음 명령어를 실행하여 새 프로젝트를 생성합니다:
flutter create color_animation_button
cd color_animation_button
추가 패키지 없이 기본 Flutter 환경에서 진행합니다.
main.dart 파일에 기본 구조를 작성합니다:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Button Color Animation',
home: ColorAnimationButton(),
);
}
}
ColorAnimationButton 위젯을 작성하여 색상 변화 애니메이션을 구현합니다:
class ColorAnimationButton extends StatefulWidget {
@override
_ColorAnimationButtonState createState() => _ColorAnimationButtonState();
}
class _ColorAnimationButtonState extends State<ColorAnimationButton>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<Color?> _colorAnimation;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: Duration(seconds: 1),
vsync: this,
);
_colorAnimation = ColorTween(
begin: Colors.blue,
end: Colors.red,
).animate(_controller);
}
void _onButtonPressed() {
if (_controller.status == AnimationStatus.completed) {
_controller.reverse();
} else {
_controller.forward();
}
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Color Animation Button'),
),
body: Center(
child: AnimatedBuilder(
animation: _colorAnimation,
builder: (context, child) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
primary: _colorAnimation.value,
padding: EdgeInsets.symmetric(horizontal: 30, vertical: 15),
),
onPressed: _onButtonPressed,
child: Text(
'Press Me',
style: TextStyle(color: Colors.white, fontSize: 18),
),
);
},
),
),
);
}
}
Flutter의 애니메이션 기능은 간단한 동작으로도 UX를 크게 향상시킬 수 있습니다. 위 예제를 바탕으로 더욱 다양한 애니메이션을 구현해 보세요. 다음 글에서는 여러 애니메이션을 조합하는 방법을 다룰 예정입니다.
제스처와 고급 물리 효과를 결합하는 방법 (0) | 2025.01.03 |
---|---|
제스처와 애니메이션 결합하기 (0) | 2025.01.03 |
Flutter 고급 애니메이션 패턴 (0) | 2025.01.02 |
Flutter UI 디자인 패턴 (0) | 2025.01.01 |
클라우드 파이어스토어를 활용한 고급 데이터 관리 기법 (0) | 2025.01.01 |