Created
June 22, 2020 08:35
-
-
Save umberto-sonnino/58179d71566025b4545756a0bd75c299 to your computer and use it in GitHub Desktop.
Using FlareControls
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:math'; | |
import 'package:flare_dart/math/mat2d.dart'; | |
import 'package:flare_flutter/flare.dart'; | |
import 'package:flare_flutter/flare_actor.dart'; | |
import 'package:flare_flutter/flare_controller.dart'; | |
class DayNightControls extends FlareController { | |
FlutterActorArtboard _artboard; | |
String _animationName; | |
final double _mixSeconds = 0.1; | |
final List<FlareAnimationLayer> _animationLayers = []; | |
List<FlareAnimationLayer> get layers => _animationLayers; | |
@override | |
void initialize(FlutterActorArtboard artboard) { | |
_artboard = artboard; | |
} | |
void onCompleted(String name) {} | |
void play(String name, {double mix = 1.0, double mixSeconds = 0.2}) { | |
_animationName = name; | |
if (_animationName != null && _artboard != null) { | |
ActorAnimation animation = _artboard.getAnimation(_animationName); | |
if (animation != null) { | |
_animationLayers.add(FlareAnimationLayer() | |
..name = _animationName | |
..animation = animation | |
..mix = mix | |
..mixSeconds = mixSeconds); | |
isActive.value = true; | |
} | |
} | |
} | |
void stop(String name) { | |
_animationLayers.removeWhere((layer) => layer.name == name); | |
print(_animationLayers.length); | |
} | |
@override | |
void setViewTransform(Mat2D viewTransform) {} | |
@override | |
bool advance(FlutterActorArtboard artboard, double elapsed) { | |
List<FlareAnimationLayer> completed = []; | |
for (int i = 0; i < _animationLayers.length; i++) { | |
FlareAnimationLayer layer = _animationLayers[i]; | |
layer.mix += elapsed; | |
layer.time += elapsed; | |
double mix = (_mixSeconds == null || _mixSeconds == 0.0) | |
? 1.0 | |
: min(1.0, layer.mix / _mixSeconds); | |
if (layer.animation.isLooping) { | |
layer.time %= layer.animation.duration; | |
} | |
layer.animation.apply(layer.time, _artboard, mix); | |
if (layer.time > layer.animation.duration) { | |
completed.add(layer); | |
} | |
} | |
for (final FlareAnimationLayer animation in completed) { | |
_animationLayers.remove(animation); | |
onCompleted(animation.name); | |
} | |
return _animationLayers.isNotEmpty; | |
} | |
} |
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:flare_flutter/flare.dart'; | |
import 'package:flare_flutter/flare_actor.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:test_flare/constants.dart'; | |
import 'package:test_flare/day_night_controls.dart'; | |
import 'constants.dart'; | |
class HomePage extends StatelessWidget { | |
HomePage({Key key}) | |
: _dayNightControls = ButtonControls(), | |
super(key: key); | |
final List<String> animations = [ | |
Constants.ANIMATION_NIGHT_IDLE, | |
Constants.ANIMATION_NIGHT_TO_DAY, | |
Constants.ANIMATION_DAY_IDLE, | |
Constants.ANIMATION_DAY_TO_NIGHT, | |
]; | |
final _dayNightControls; | |
@override | |
Widget build(BuildContext context) { | |
return Stack( | |
children: [ | |
_backgroundFlare(context), | |
_flareButton(), | |
], | |
); | |
} | |
Widget _backgroundFlare(context) { | |
return FlareActor( | |
Constants.FLARE_BACKGROUND, | |
controller: _dayNightControls.bgControls, | |
fit: BoxFit.fill, | |
); | |
} | |
Widget _flareButton() { | |
return Center( | |
child: Container( | |
height: 100, | |
width: 100, | |
child: GestureDetector( | |
onTap: () => _dayNightControls.transition(), | |
child: FlareActor( | |
Constants.FLARE_CHANGE_THEME_BUTTON, | |
controller: _dayNightControls, | |
), | |
), | |
), | |
); | |
} | |
} | |
class ButtonControls extends DayNightControls { | |
bool _isIdle = true; | |
bool _isDay = false; | |
final DayNightControls _bgControls = DayNightControls(); | |
DayNightControls get bgControls => _bgControls; | |
@override | |
void initialize(FlutterActorArtboard artboard) { | |
super.initialize(artboard); | |
play(Constants.ANIMATION_NIGHT_IDLE); | |
} | |
@override | |
void play(String name, {double mix = 1.0, double mixSeconds = 0.2}) { | |
// Play the same animation also on the background. | |
_bgControls.play(name); | |
super.play(name); | |
} | |
@override | |
void onCompleted(String name) { | |
if (name == Constants.ANIMATION_NIGHT_TO_DAY) { | |
_stop(Constants.ANIMATION_NIGHT_IDLE); | |
play(Constants.ANIMATION_DAY_IDLE); | |
_isDay = true; | |
} else if (name == Constants.ANIMATION_DAY_TO_NIGHT) { | |
_stop(Constants.ANIMATION_DAY_IDLE); | |
play(Constants.ANIMATION_NIGHT_IDLE); | |
_isDay = false; | |
} | |
// Looping animations never complete: | |
// If this is called, it means that one of the two transitioning | |
// animations has finished playing. | |
_isIdle = true; | |
super.onCompleted(name); | |
} | |
void _stop(String animationName) { | |
stop(animationName); | |
_bgControls.stop(animationName); | |
} | |
void transition() { | |
if (!_isIdle) { | |
return; | |
} | |
_isIdle = false; | |
if (_isDay) { | |
play(Constants.ANIMATION_DAY_TO_NIGHT); | |
} else { | |
play(Constants.ANIMATION_NIGHT_TO_DAY); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment