Created
April 22, 2020 23:16
-
-
Save MisterJimson/7b72ba9246c655002e32c526c0bc3846 to your computer and use it in GitHub Desktop.
animated_rotation_demo
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/foundation.dart'; | |
import 'package:flutter/widgets.dart'; | |
import 'dart:math' show pi; | |
void main() => runApp(MyHomePage()); | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _counter = 0; | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text("AnimatedRotation example"), | |
), | |
body: Center( | |
child: AnimatedRotation( | |
angle: _counter, | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'You have pushed the button this many times:', | |
), | |
Text( | |
'$_counter', | |
style: Theme.of(context).textTheme.headline4, | |
), | |
], | |
), | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
setState(() { | |
_counter++; | |
}); | |
}, | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
), | |
); | |
} | |
} | |
/// Animated version of [Transform.rotate] which automatically transitions the | |
/// rotation over time. | |
class AnimatedRotation extends ImplicitlyAnimatedWidget { | |
/// Creates a widget that rotates its child by a value that animates | |
/// implicitly. | |
/// | |
/// The [angle], [curve], and [duration] arguments must not be null. | |
AnimatedRotation({ | |
Key key, | |
@required this.angle, | |
this.child, | |
Curve curve = Curves.linear, | |
Duration duration = const Duration(seconds: 1), | |
}) : assert(angle != null), | |
super(key: key, curve: curve, duration: duration); | |
/// The amount degrees to rotate the child clockwise. | |
final num angle; | |
/// The widget to rotate | |
final Widget child; | |
@override | |
_AnimatedRotationState createState() => _AnimatedRotationState(); | |
@override | |
void debugFillProperties(DiagnosticPropertiesBuilder properties) { | |
super.debugFillProperties(properties); | |
properties.add(DiagnosticsProperty<num>('angle', angle)); | |
} | |
} | |
class _AnimatedRotationState extends AnimatedWidgetBaseState<AnimatedRotation> { | |
Tween<num> _angle; | |
num _degToRad(num deg) => deg * (pi / 180.0); | |
@override | |
void forEachTween(TweenVisitor<dynamic> visitor) { | |
_angle = visitor( | |
_angle, widget.angle, (dynamic value) => Tween<num>(begin: value)); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Transform.rotate( | |
angle: _degToRad(_angle.evaluate(animation)), | |
child: widget.child, | |
); | |
} | |
@override | |
void debugFillProperties(DiagnosticPropertiesBuilder description) { | |
super.debugFillProperties(description); | |
description.add(DiagnosticsProperty<Tween<double>>('angle', _angle, | |
defaultValue: null)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment