Created with <3 with dartpad.dev.
Last active
January 23, 2024 11:56
-
-
Save hardy716/1ecddd42b2d73a23cabf59784bb32ae4 to your computer and use it in GitHub Desktop.
플러터챌린지 2일차 심화문제 - 신현호
This file contains 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 'dart:math'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'HelloAnimation', | |
debugShowCheckedModeBanner: false, | |
home: HelloAnimationScreen(), | |
); | |
} | |
} | |
class HelloAnimationScreen extends StatefulWidget { | |
@override | |
State<HelloAnimationScreen> createState() => _HelloAnimationScreenState(); | |
} | |
class _HelloAnimationScreenState extends State<HelloAnimationScreen> | |
with SingleTickerProviderStateMixin { | |
late AnimationController _controller; | |
static const double starSize = 45.0; | |
static const double starRotations = 10; | |
double _top = 0; | |
double _left = 0; | |
bool isPlus = false; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController( | |
duration: const Duration(seconds: 5), | |
vsync: this, | |
)..addListener(() { | |
if (_left < MediaQuery.of(context).size.width * 0.01) { isPlus = false; } | |
if (!isPlus && _left > MediaQuery.of(context).size.width * 0.99) { | |
_top += starSize; | |
isPlus = true; | |
if (_top >= MediaQuery.of(context).size.height - starSize) { | |
_top = 0; | |
} | |
} | |
}) | |
..repeat(); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
void _changeLine(TapDownDetails details) { | |
setState(() { | |
_top = details.localPosition.dy; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return GestureDetector( | |
onTapDown: _changeLine, | |
child: Scaffold( | |
backgroundColor: Colors.black, | |
body: Stack( | |
children: [ | |
AnimatedBuilder( | |
animation: _controller, | |
builder: (context, child) { | |
final size = MediaQuery.of(context).size; | |
final horizontalMovementLength = size.width; | |
final animationValue = _controller.value * horizontalMovementLength; | |
_left = animationValue % horizontalMovementLength; | |
double rotation = _controller.value * starRotations * pi; | |
return Positioned( | |
left: _left, | |
top: _top, | |
child: Transform.rotate( | |
angle: rotation, | |
child: const Icon(Icons.star, size: starSize, color: Colors.yellow), | |
), | |
); | |
}, | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment