Files
rive-flutter/example/lib/carousel.dart
HayesGordon 82e5dc984c perf: fix didUpdateWidget and artboard config
This resolves a performance and usability issue where certain conditions would result in Rive needlessly reconfiguring/initializing the Rive artboard (as well as downloading/loading Rive files). If `onInit`, `animations`, `controllers`, or `stateMachines` were passed in as an argument to `RiveAnimation` the above issue is triggered on each widget rebuild. Under certain conditions this could result in an animation constantly restarting, bad performance, or Flutter ending up in a `setState` callback loop.

This PR also clears the list of local controllers each time init is called.

Resolves: https://github.com/rive-app/rive-flutter/issues/277

This also fixes bugs in our example app
- Play/Pause not working
- One shot animation behaving oddly

Diffs=
9af05d044 docs: update changelog
cb7fd6d14 test: add rive animation onInit tests
107ae16bc refactor: naming and call logic
6857aa691 docs: add additional code docs
f3ba4f015 perf: fix didUpdateWidget configure loop
7d0aaaff3 Adjust RiveAnimation didUpdateWidget condition (https://github.com/rive-app/rive-flutter/issues/278)
d4c6dd4ab Add more helper functions
6a8f9e249 Fix tess for C++11 and add to github action (#4571)
c8b5fdadd More SIMD features
87f079a10 RawPath::Iter improvements
2023-01-10 08:59:52 +00:00

101 lines
2.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:rive/rive.dart';
/// An example showing how to drive a StateMachine via one numeric input.
class AnimationCarousel extends StatefulWidget {
const AnimationCarousel({Key? key}) : super(key: key);
@override
_AnimationCarouselState createState() => _AnimationCarouselState();
}
class _AnimationCarouselState extends State<AnimationCarousel> {
final riveAnimations = [
const RiveCustomAnimationData(
name: 'assets/liquid_download.riv',
),
const RiveCustomAnimationData(
name: 'assets/little_machine.riv',
stateMachines: ['State Machine 1'],
),
const RiveCustomAnimationData(
name: 'assets/off_road_car.riv',
),
const RiveCustomAnimationData(
name: 'assets/rocket.riv',
stateMachines: ['Button'],
animations: ['Roll_over'],
),
const RiveCustomAnimationData(
name: 'assets/skills.riv',
stateMachines: ['Designer\'s Test'],
),
// not a pretty out of the box example
const RiveCustomAnimationData(
name: 'assets/light_switch.riv',
stateMachines: ['Switch'],
),
// v6.0 file,
// 'assets/teeny_tiny.riv',
// const RiveCustomAnimationData(name: 'assets/teeny_tiny.riv'),
];
var _index = 0;
void next() {
setState(() {
_index += 1;
});
}
void previous() {
setState(() {
_index -= 1;
});
}
@override
Widget build(BuildContext context) {
final _indexToShow = _index % riveAnimations.length;
return Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
title: const Text('Animation Carousel'),
),
body: Center(
child: Row(
children: [
GestureDetector(
onTap: previous,
child: const Icon(Icons.arrow_back),
),
Expanded(
child: RiveAnimation.asset(
riveAnimations[_indexToShow].name,
animations: riveAnimations[_indexToShow].animations,
stateMachines: riveAnimations[_indexToShow].stateMachines,
),
),
GestureDetector(
onTap: next,
child: const Icon(Icons.arrow_forward),
),
],
),
),
);
}
}
@immutable
class RiveCustomAnimationData {
final String name;
final List<String> animations;
final List<String> stateMachines;
const RiveCustomAnimationData({
required this.name,
this.animations = const [],
this.stateMachines = const [],
});
}