Last active
March 24, 2019 13:28
-
-
Save mono0926/e47dfb6d0dbc030002658cce7c5ee2d5 to your computer and use it in GitHub Desktop.
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 'package:flutter/material.dart'; | |
import 'package:flutter/rendering.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData(primarySwatch: Colors.red), | |
title: 'Flutter Demo', | |
home: HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
const HomePage({Key key}) : super(key: key); | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> | |
with SingleTickerProviderStateMixin { | |
AnimationController _animation; | |
Animation<double> _radiusAnimation; | |
@override | |
void initState() { | |
super.initState(); | |
_animation = AnimationController( | |
vsync: this, | |
duration: const Duration(milliseconds: 1000), | |
); | |
_radiusAnimation = _animation.drive(Tween<double>( | |
begin: 0.2, | |
end: 0.5, | |
)); | |
_animation.repeat(reverse: true); | |
} | |
@override | |
void dispose() { | |
_animation.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Title'), | |
), | |
body: AnimatedBuilder( | |
animation: _radiusAnimation, | |
builder: (context, child) => CustomPaint( | |
painter: Sky(radius: _radiusAnimation.value), | |
child: child, | |
), | |
child: SizedBox.expand(), | |
), | |
); | |
} | |
} | |
class Sky extends CustomPainter { | |
const Sky({ | |
@required this.radius, | |
}) : super(); | |
final double radius; | |
@override | |
void paint(Canvas canvas, Size size) { | |
var rect = Offset.zero & size; | |
var gradient = RadialGradient( | |
center: Alignment.center, | |
radius: radius, | |
colors: const [Color(0xFFFF0000), Color(0xFFFFCCFF)], | |
stops: [0.2, 1.0], | |
); | |
canvas.drawRect( | |
rect, | |
Paint()..shader = gradient.createShader(rect), | |
); | |
} | |
@override | |
bool shouldRepaint(Sky oldDelegate) => oldDelegate.radius != radius; | |
} |
Author
mono0926
commented
Mar 24, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment