Last active
April 21, 2018 17:13
-
-
Save manujbahl/2bfd2e155de94b614e55c5c69fd7712c to your computer and use it in GitHub Desktop.
animateScale.dart
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 '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