Skip to content

Instantly share code, notes, and snippets.

@hardy716
Created January 30, 2024 14:45
Show Gist options
  • Save hardy716/f81ac788be0c82f70d0ca786bbc7675f to your computer and use it in GitHub Desktop.
Save hardy716/f81ac788be0c82f70d0ca786bbc7675f to your computer and use it in GitHub Desktop.
플러터챌린지 7일차 심화문제 - 신현호 

플러터챌린지 7일차 심화문제 - 신현호 

Created with <3 with dartpad.dev.

import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
debugShowCheckedModeBanner: false,
home: ILoveFlutterBootScreen(),
),
);
}
class ILoveFlutterBootScreen extends StatefulWidget {
const ILoveFlutterBootScreen({super.key});
@override
State<ILoveFlutterBootScreen> createState() => _ILoveFlutterBootScreenState();
}
class _ILoveFlutterBootScreenState extends State<ILoveFlutterBootScreen> {
static const NetworkImage background = NetworkImage('https://docs.flutter.dev/assets/images/dash/Dashatars.png');
GlobalKey gKey = GlobalKey();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: background,
fit: BoxFit.cover,
),
),
),
floatingActionButton: GestureDetector(
key: gKey,
onTap: flyingDash,
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(width: 2, color: Colors.blue),
),
child: const Padding(
padding: EdgeInsets.all(10.0),
child: Icon(
Icons.flutter_dash_rounded,
color: Colors.blue,
size: 40,
),
)
),
),
);
}
void flyingDash() {
OverlayEntry? overlayEntry;
overlayEntry = OverlayEntry(builder: (BuildContext context) {
RenderBox renderBox = gKey.currentContext!.findRenderObject() as RenderBox;
Offset beginOffset = renderBox.localToGlobal(Offset.zero);
return FlutterDash(
offset: beginOffset,
entry: overlayEntry,
arrival: MediaQuery.of(context).size.height / 2,
);
});
Overlay.of(context, debugRequiredFor: widget).insert(overlayEntry);
}
}
class FlutterDash extends StatefulWidget {
final Offset offset;
final OverlayEntry? entry;
final double arrival;
const FlutterDash({
required this.offset,
required this.entry,
required this.arrival,
});
@override
State<FlutterDash> createState() => _FlutterDashState();
}
class _FlutterDashState extends State<FlutterDash>
with TickerProviderStateMixin {
late AnimationController controller;
late Animation animationX, animationY;
late double currentX, currentY;
late Color dashColor;
late double dashSize;
int getRandomInt(int maxValue) {
return Random().nextInt(maxValue);
}
Color getRandomColor() {
return Color.fromRGBO(getRandomInt(256), getRandomInt(256), getRandomInt(256), 0.5,);
}
double getRandomSize() {
return 30 + getRandomInt(30).toDouble();
}
TweenSequence<double> backAndForth(double posX, int count) {
List<TweenSequenceItem<double>> items = [];
for (int i = 0; i < count; i++) {
double gap = 2 + getRandomInt(80) / 10;
items.add(
TweenSequenceItem<double>(
tween: Tween<double>(begin: posX + (i % 2 == 0 ? -gap : gap), end: posX + (i % 2 == 0 ? gap : -gap)),
weight: 20,
),
);
}
return TweenSequence<double>(items);
}
@override
void initState() {
super.initState();
currentX = widget.offset.dx - 10 + Random().nextInt(30);
currentY = widget.offset.dy;
dashColor = getRandomColor();
dashSize = getRandomSize();
controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 5),
);
animationX = backAndForth(currentX, 8).animate(controller);
animationY = Tween<double>(
begin: widget.offset.dy,
end: widget.arrival,
).animate(controller)
..addListener(() {
setState(() {
currentX = animationX.value;
currentY = animationY.value;
});
})
..addStatusListener((status) {
if (status == AnimationStatus.completed) {
setState(() {
widget.entry?.remove();
});
}
});
controller.forward();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Stack(
children: [
AnimatedBuilder(
animation: controller,
builder: (context, child) {
return Transform.translate(
offset: Offset(currentX, currentY),
child: Icon(Icons.flutter_dash, size: dashSize, color: dashColor),
);
},
),
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment