mirror of
https://github.com/rive-app/rive-flutter.git
synced 2025-05-22 15:56:32 +08:00
47 lines
1.2 KiB
Dart
47 lines
1.2 KiB
Dart
/// Demonstrates how to create a custom controller to change the speed of an
|
|
/// animation
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:rive/rive.dart';
|
|
|
|
class SpeedyAnimation extends StatelessWidget {
|
|
const SpeedyAnimation({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Custom Controller'),
|
|
),
|
|
body: Center(
|
|
child: RiveAnimation.network(
|
|
'https://cdn.rive.app/animations/vehicles.riv',
|
|
fit: BoxFit.cover,
|
|
animations: const ['idle'],
|
|
controllers: [SpeedController('curves', speedMultiplier: 3)],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class SpeedController extends SimpleAnimation {
|
|
final double speedMultiplier;
|
|
|
|
SpeedController(
|
|
String animationName, {
|
|
double mix = 1,
|
|
this.speedMultiplier = 1,
|
|
}) : super(animationName, mix: mix);
|
|
|
|
@override
|
|
void apply(RuntimeArtboard artboard, double elapsedSeconds) {
|
|
if (instance == null || !instance!.keepGoing) {
|
|
isActive = false;
|
|
}
|
|
instance!
|
|
..animation.apply(instance!.time, coreContext: artboard, mix: mix)
|
|
..advance(elapsedSeconds * speedMultiplier);
|
|
}
|
|
}
|