Skip to content

Instantly share code, notes, and snippets.

@e200
Last active January 2, 2021 14:22
Show Gist options
  • Save e200/b1c5c42074bfe48f0df45580576baaa7 to your computer and use it in GitHub Desktop.
Save e200/b1c5c42074bfe48f0df45580576baaa7 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'dart:math';
void main() {
runApp(
MaterialApp(
theme: ThemeData.dark(),
home: Scaffold(body: HomePage()),
debugShowCheckedModeBanner: false,
),
);
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
double _angle = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: AnimatedRotationButton(
child: Text(
'Clique me!',
style: TextStyle(color: Colors.white),
),
angle: _angle,
onPressed: () {
setState(() {
_angle += 45 * pi / 180;
});
},
),
),
);
}
}
class AnimatedRotationButton extends ImplicitlyAnimatedWidget {
final Widget child;
final double angle;
final Function onPressed;
const AnimatedRotationButton({
Key key,
this.child,
this.angle,
this.onPressed,
}) : super(key: key, duration: const Duration(milliseconds: 300));
@override
ImplicitlyAnimatedWidgetState createState() => _AnimatedRotationButtonState();
}
class _AnimatedRotationButtonState
extends AnimatedWidgetBaseState<AnimatedRotationButton> {
Tween<double> _angleTween;
@override
void forEachTween(visitor) {
_angleTween = visitor(
_angleTween,
widget.angle,
(value) => Tween<double>(begin: value),
);
}
@override
Widget build(BuildContext context) {
return Transform.rotate(
angle: _angleTween.evaluate(animation),
child: RaisedButton(
color: Colors.blue,
child: widget.child,
onPressed: widget.onPressed,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment