Skip to content

Instantly share code, notes, and snippets.

@hongsw
Created November 8, 2023 01:32
Show Gist options
  • Save hongsw/e96c940d681f75ea0122b6b3e760a5e6 to your computer and use it in GitHub Desktop.
Save hongsw/e96c940d681f75ea0122b6b3e760a5e6 to your computer and use it in GitHub Desktop.
flying-flora-4095
import 'package:flutter/material.dart';
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: const AnimationExample(),
);
}
}
class AnimationExample extends StatefulWidget {
const AnimationExample({Key? key}) : 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);
// 애니메이션 시작
_controller.forward();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Tween + CurvedAnimation'),
),
body: Center(
child: AnimatedBuilder(
animation: _animation,
builder: (context, child) {
// _animation.value는 sizeTween에서 정의한 범위 내에서 변합니다.
return Container(
width: _animation.value,
height: _animation.value,
color: Colors.blue,
alignment: Alignment.center,
child: const Text('Animating...'),
);
},
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment