Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Flutter AnimationController example
import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Padding(
padding: const EdgeInsets.only(top: 20, left: 20, right: 20),
child: PageAnimation()
)
)
));
class PageAnimation extends StatefulWidget {
@override
PageAnimationState createState() => PageAnimationState();
}
class PageAnimationState extends State<PageAnimation> with SingleTickerProviderStateMixin {
final sizeFactor = 150;
AnimationController controller;
Animation animation;
bool _isRunning;
@override
void initState() {
super.initState();
controller = AnimationController( // animate 0 to 1 over duration specified
duration: Duration(milliseconds: 800),
vsync: this
);
animation = Tween<double>(begin: 100, end: 300).animate(controller)
..addListener(() {
// print(controller.value);
setState(() {});
});
controller.repeat(min: 0.4);
_isRunning = true;
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
if (_isRunning) {
controller.stop();
_isRunning = false;
} else {
controller.repeat(min: 0.4);
_isRunning = true;
}
},
child: Center(
// child: Text('Hello, World', style: TextStyle(
// fontSize: sizeFactor * controller.value
// ))
child: Container(
width: animation.value,
height: animation.value,
color: Color.fromRGBO(134, 64, 248, controller.value)
)
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.