Created
August 23, 2020 17:56
-
-
Save guid-empty/0215734fde6e674adc4cf39da10779ac to your computer and use it in GitHub Desktop.
Explicit Animations - AnimatedWidget - FadeOutAngleTransition
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 'dart:math'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: MyHomePage(), | |
); | |
} | |
} | |
const double _logoSize = 200.0; | |
class OtusLogo extends StatelessWidget { | |
const OtusLogo(); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
child: Image.network('https://otus.ru/static/img/logo-2.8602b.svg'), | |
width: _logoSize, | |
height: _logoSize, | |
color: Colors.green, | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class ArcClipper extends CustomClipper<Path> { | |
final double _clipFactor; | |
ArcClipper(this._clipFactor); | |
@override | |
Path getClip(Size size) { | |
final radius = size.width / 2; | |
final center = Offset(size.width / 2, size.height / 2); | |
final angle = pi * 2 * _clipFactor; | |
final path = Path() | |
..moveTo(center.dx, center.dy) | |
..lineTo(center.dx, center.dy - radius) | |
..arcTo( | |
Rect.fromCenter( | |
center: center, height: size.height, width: size.width), | |
-pi / 2, | |
angle, | |
true) | |
..lineTo(center.dx, center.dy) | |
..close(); | |
return path; | |
} | |
@override | |
bool shouldReclip(CustomClipper<Path> oldClipper) { | |
return true; | |
} | |
} | |
class FadeOutAngleTransition extends AnimatedWidget { | |
final Animation<double> animation; | |
final Widget child; | |
FadeOutAngleTransition( | |
{Key key, @required this.animation, @required this.child}) | |
: super(key: key, listenable: animation); | |
@override | |
Widget build(BuildContext context) { | |
return AnimatedBuilder( | |
animation: animation, | |
builder: (_, Widget childWidget) { | |
return ClipPath( | |
child: childWidget, clipper: ArcClipper(animation.value)); | |
}, | |
child: child, | |
); | |
} | |
} | |
class _MyHomePageState extends State<MyHomePage> | |
with SingleTickerProviderStateMixin { | |
AnimationController _controller; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController( | |
vsync: this, | |
duration: const Duration(seconds: 2), | |
); | |
} | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Explicit Animations - AnimatedWidget - FadeOutAngleTransition'), | |
), | |
body: Center( | |
child: Container( | |
height: 300, | |
child: Stack( | |
children: <Widget>[ | |
FadeOutAngleTransition( | |
animation: _controller, | |
child: const OtusLogo(), | |
), | |
], | |
), | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
if (_controller.isAnimating) { | |
_controller.stop(); | |
} else { | |
_controller.repeat(); | |
} | |
setState(() {}); | |
}, | |
child: | |
_controller.isAnimating ? Icon(Icons.stop) : Icon(Icons.play_arrow), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment