Skip to content

Instantly share code, notes, and snippets.

@hardy716
Last active January 29, 2024 21:58
Show Gist options
  • Save hardy716/aa63402c2e6b0999df07e58eef46608f to your computer and use it in GitHub Desktop.
Save hardy716/aa63402c2e6b0999df07e58eef46608f to your computer and use it in GitHub Desktop.
플러터챌린지 6일차 심화문제 - 신현호

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

Created with <3 with dartpad.dev.

import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
debugShowCheckedModeBanner: false,
home: MyFencyButtonScreen(),
),
);
}
class MyFencyButtonScreen extends StatefulWidget {
const MyFencyButtonScreen({super.key});
@override
State<MyFencyButtonScreen> createState() => _MyFencyButtonScreenState();
}
class _MyFencyButtonScreenState extends State<MyFencyButtonScreen> with TickerProviderStateMixin {
late AnimationController controller;
late Animation animation;
bool isClick = false;
List<Shadow> generateTextShadows(Color color, double blurRadius) {
const List<Offset> textOffsets = [
Offset(0, 1), Offset(1, 0), Offset(0, -1), Offset(-1, 0),
];
return List<Shadow>.generate(
textOffsets.length,
(index) => Shadow(
color: color,
blurRadius: blurRadius,
offset: textOffsets[index],
),
);
}
@override
void initState() {
controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 1200),
);
controller.addStatusListener((status) {
if (status == AnimationStatus.completed) {
Future.delayed(const Duration(milliseconds: 800), () {
controller.forward(from: 0);
});
}
});
controller.addListener(() {
setState(() {});
});
super.initState();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.black,
child: Center(
child: AnimatedBuilder(
animation: controller,
builder: (context, child) {
return CustomPaint(
painter: isClick ? _GlitterPainter(controller.value * 5) : null,
child: Container(
padding: const EdgeInsets.all(4),
child: GestureDetector(
onTap: () {
setState(() {
isClick = !isClick;
isClick ? controller.forward() : controller.stop();
});
},
child: Container(
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(16),
),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Text(
"Flutter Boot \nClick me 😎",
style: Theme.of(context).textTheme.titleLarge?.copyWith(
color: isClick ? Colors.yellow : Colors.grey,
shadows: isClick ? generateTextShadows(Colors.red, 2) : null,
),
),
),
),
),
),
);
},
),
),
);
}
}
class _GlitterPainter extends CustomPainter {
final double angle;
_GlitterPainter(this.angle);
@override
void paint(Canvas canvas, Size size) {
final Rect rect = Offset.zero & size;
final Path path = Path()..addRRect(RRect.fromRectAndRadius(rect, const Radius.circular(16)));
final Paint blurPaint = Paint()
..style = PaintingStyle.stroke
..strokeWidth = 5
..color = Colors.red
..maskFilter = const MaskFilter.blur(BlurStyle.normal, 6.0);
canvas.drawPath(path, blurPaint);
final Paint gradientPaint = Paint()
..style = PaintingStyle.stroke
..strokeWidth = 5
..maskFilter = const MaskFilter.blur(BlurStyle.normal, 1.0)
..shader = SweepGradient(
colors: const [Colors.red, Colors.yellow, Colors.red],
stops: const [0, 0.5, 1],
transform: GradientRotation(angle),
).createShader(rect);
canvas.drawPath(path, gradientPaint);
}
@override
bool shouldRepaint(covariant _GlitterPainter oldDelegate) {
return oldDelegate.angle != angle;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment