mirror of
https://github.com/rive-app/rive-flutter.git
synced 2025-06-29 03:07:17 +08:00
Adds custom controller for speed example
This commit is contained in:
57
example/lib/custom_controller.dart
Normal file
57
example/lib/custom_controller.dart
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
/// 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;
|
||||||
|
|
||||||
|
/// Stops the animation on the next apply
|
||||||
|
bool _stopOnNextApply = false;
|
||||||
|
|
||||||
|
SpeedController(
|
||||||
|
String animationName, {
|
||||||
|
double mix = 1,
|
||||||
|
this.speedMultiplier = 1,
|
||||||
|
}) : super(
|
||||||
|
animationName,
|
||||||
|
mix: mix,
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
void apply(RuntimeArtboard artboard, double elapsedSeconds) {
|
||||||
|
if (_stopOnNextApply || instance == null) {
|
||||||
|
isActive = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
instance!.animation.apply(instance!.time, coreContext: artboard, mix: mix);
|
||||||
|
if (!instance!.advance(elapsedSeconds * speedMultiplier)) {
|
||||||
|
_stopOnNextApply = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onActivate() => _stopOnNextApply = false;
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:rive_example/custom_controller.dart';
|
||||||
import 'package:rive_example/play_one_shot_animation.dart';
|
import 'package:rive_example/play_one_shot_animation.dart';
|
||||||
import 'package:rive_example/play_pause_animation.dart';
|
import 'package:rive_example/play_pause_animation.dart';
|
||||||
import 'package:rive_example/example_state_machine.dart';
|
import 'package:rive_example/example_state_machine.dart';
|
||||||
@ -118,6 +119,20 @@ class Home extends StatelessWidget {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
const SizedBox(
|
||||||
|
height: 10,
|
||||||
|
),
|
||||||
|
ElevatedButton(
|
||||||
|
child: const Text('Custom Controller - Speed'),
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute<void>(
|
||||||
|
builder: (context) => const SpeedyAnimation(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
Reference in New Issue
Block a user