Skip to content

Instantly share code, notes, and snippets.

@manujbahl
Last active April 30, 2018 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manujbahl/01657c34badc26c1907b14fd9327e816 to your computer and use it in GitHub Desktop.
Save manujbahl/01657c34badc26c1907b14fd9327e816 to your computer and use it in GitHub Desktop.
animatePerspective
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("Animate"),
onPressed: () {
this.playAnimation(AnimationOptions.Transform);
},
),
new Center(
child: new AnimationWidget( controller: this.animationController.view),
)
]
)
//)
);
}
}
class AnimationWidget extends StatelessWidget {
AnimationWidget({ Key key, this.controller }) :
super(key: key) {
initializeAnimations();
}
initializeAnimations() {
depth = new Tween<double>(
begin: 10.0,
end: 1000.0,
).animate(
new CurvedAnimation(
parent: controller,
curve: new Interval(
0.0, 1.0,
curve: Curves.ease,
),
),
);
}
final Animation<double> controller;
Animation<double> depth;
_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()
..setEntry(3, 2, -1/(depth.value));
return new Transform(
alignment: Alignment.center,
transform: matrix,
child: new Container(
width: 200.0,
height: 200.0,
color: Colors.blue,
child: new Center(
child: new Text("Test this shit")
)
),
);
}
Widget _noAnimation(BuildContext context, Widget child) {
return new Container(
child: new Container(
width: 200.0,
height: 200.0,
color: Colors.blue,
child: new Center(
child: new Text("Test this shit")
)
),
);
}
@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