Created
August 25, 2020 07:10
-
-
Save magicleon94/122f7ad93369ce8b41c626b9359ea3d9 to your computer and use it in GitHub Desktop.
Change Flare animation's colors
This file contains hidden or 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_dart/math/mat2d.dart'; | |
import 'package:flare_flutter/flare.dart'; | |
import 'package:flare_flutter/flare_actor.dart'; | |
import 'package:flare_flutter/flare_controller.dart'; | |
import 'package:flutter/material.dart'; | |
class FlareAnimationWidget extends StatefulWidget { | |
final Color fillColor; | |
final Color strokeColor; | |
const FlareAnimationWidget({ | |
Key key, | |
this.fillColor, | |
this.strokeColor, | |
}) : | |
super(key: key); | |
@override | |
_FlareAnimationWidgetState createState() => _FlareAnimationWidgetState(); | |
} | |
class _FlareAnimationWidgetState extends State<FlareAnimationWidget> | |
with FlareController { | |
FlutterActorArtboard _artboard; | |
@override | |
bool advance(FlutterActorArtboard artboard, double elapsed) { | |
return false; | |
} | |
@override | |
void initialize(FlutterActorArtboard artboard) { | |
_artboard ??= artboard; | |
var theme = Theme.of(context); | |
if (theme == null) { | |
return; | |
} | |
artboard.drawableNodes.forEach((node) { | |
try { | |
if (RegExp("Accent", caseSensitive: false).hasMatch(node.name)) { | |
var s = node as FlutterActorShape; | |
var fill = s.fill as FlutterColorFill; | |
fill?.uiColor = widget.fillColor ?? theme.accentColor; | |
} | |
if (RegExp("Track", caseSensitive: false).hasMatch(node.name)) { | |
var s = node as FlutterActorShape; | |
var stroke = s.stroke as FlutterColorStroke; | |
var fill = s.fill as FlutterColorFill; | |
stroke?.uiColor = widget.strokeColor ?? theme.textTheme.bodyText1.color; | |
fill?.uiColor = widget.strokeColor ?? theme.textTheme.bodyText1.color; | |
} | |
} on Exception catch (e) { | |
print( | |
"Error applying colors to Flare Animation: ${e.toString()}"); | |
} | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: FlareActor( | |
'animation path',//other params | |
), | |
); | |
} | |
@override | |
void setViewTransform(Mat2D viewTransform) { | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment