Created
December 6, 2018 11:31
-
-
Save tunitowen/07b8580c39f7727ee3057164a01df032 to your computer and use it in GitHub Desktop.
Multiple Flare Animations - Flutter
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 'package:flutter/material.dart'; | |
import 'package:flare/flare_actor.dart'; | |
const String ANIM_JUST_NIGHT = "just_night"; | |
const String ANIM_NIGHT_TO_DAY = "night_to_day"; | |
const String ANIM_DAY_TO_NIGHT = "day_to_night"; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blueGrey, | |
), | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
String currentAnimation = ANIM_JUST_NIGHT; | |
bool isAnimating = false; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Column( | |
mainAxisAlignment: MainAxisAlignment.start, | |
mainAxisSize: MainAxisSize.min, | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: [ | |
SizedBox( | |
height: 266, | |
child: FlareActor( | |
"assets/xmas.flr", | |
alignment: Alignment.center, | |
fit: BoxFit.fill, | |
animation: currentAnimation, | |
callback: (string) { | |
isAnimating = false; | |
debugPrint(string); | |
}, | |
)), | |
Expanded( | |
child: Center( | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: <Widget>[ | |
RaisedButton( | |
color: Colors.blueAccent, | |
child: Text( | |
"Make it daytime", | |
style: TextStyle(color: Colors.white, fontSize: 20), | |
), | |
onPressed: () { | |
_animateTo(ANIM_NIGHT_TO_DAY); | |
}), | |
RaisedButton( | |
color: Colors.black, | |
child: Text( | |
"Yawn, night please", | |
style: TextStyle(color: Colors.white, fontSize: 20), | |
), | |
onPressed: () { | |
_animateTo(ANIM_DAY_TO_NIGHT); | |
}), | |
], | |
)), | |
) | |
], | |
), | |
); | |
} | |
void _animateTo(String anim) { | |
if (_canAnimateTo(anim)) { | |
isAnimating = true; | |
setState(() { | |
currentAnimation = anim; | |
}); | |
} | |
} | |
bool _canAnimateTo(String anim) { | |
if (currentAnimation == ANIM_JUST_NIGHT) { | |
return anim == ANIM_NIGHT_TO_DAY; | |
} | |
return (!isAnimating && currentAnimation != anim); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment