Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
animateScale.dart
import 'dart:async';
import 'package:flutter/material.dart';
enum AnimationOptions { None, Transform }
void main() {
var app = new Scaffold(
body:new AnimationDemo()
);
runApp(new MaterialApp(home: app));
}
class AnimationDemo extends StatefulWidget {
@override
_AnimationDemoState createState() => new _AnimationDemoState();
}
class _AnimationDemoState extends State<AnimationDemo> with TickerProviderStateMixin {
AnimationController controller;
@override
void initState() {
super.initState();
controller = new AnimationController(
duration: const Duration(milliseconds: 2000),
vsync: this
);
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
Widget build(BuildContext context) {
return new Screen1(controller);
}
}
class Screen1 extends StatelessWidget {
Screen1(this.animationController);
final AnimationController animationController;
static AnimationOptions currentAnimation = AnimationOptions.None;
Future<Null> playAnimation(animationOption) async {
try {
currentAnimation = animationOption;
animationController.reset();
await animationController.forward().orCancel;
currentAnimation = AnimationOptions.None;
} on TickerCanceled {
// the animation got canceled, probably because we were disposed
}
}
@override
Widget build(BuildContext context) {
return new Scaffold( // 1
appBar: new AppBar(
title: new Text("Animation Styles"), // screen title
),
body: new Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new FlatButton(
child: new Text(" 1st Animate"),
onPressed: () {
this.playAnimation(AnimationOptions.Transform);
},
),
new AnimationWidget( controller: this.animationController.view),
]
)
//)
);
}
}
class AnimationWidget extends StatelessWidget {
AnimationWidget({ Key key, this.controller }) :
super(key: key) {
initializeAnimations();
}
initializeAnimations() {
scale = new Tween<double>(
begin: 1.0,
end: 0.6,
).animate(
new CurvedAnimation(
parent: controller,
curve: new Interval(
0.0, 1.0,
curve: Curves.ease,
),
),
);
}
final Animation<double> controller;
Animation<double> scale;
_animateBuilder(BuildContext context, Widget child){
switch (Screen1.currentAnimation) {
case AnimationOptions.Transform:
return _animateTransform1(context, child);
default:
return _noAnimation(context, child);
}
}
Widget _animateTransform1(BuildContext context, Widget child) {
var matrix = new Matrix4.identity()
..scale(scale.value, scale.value, scale.value);
return new Transform(
alignment: Alignment.center,
transform: matrix,
child: new Container(
width: 200.0,
height: 200.0,
color: Colors.blue,
),
);
}
Widget _noAnimation(BuildContext context, Widget child) {
return new Container(
child: new Container(
width: 200.0,
height: 200.0,
color: Colors.blue,
),
);
}
@override
Widget build(BuildContext context) {
return new AnimatedBuilder(
builder: _animateBuilder,
animation: controller,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment