如何在flutter中使用rive的状态机?
Posted
技术标签:
【中文标题】如何在flutter中使用rive的状态机?【英文标题】:How to use rive's state machine in flutter? 【发布时间】:2021-07-08 12:37:16 【问题描述】:我创建了一个 .riv 文件,其中包含 3 个状态动画:开始、处理、结束,它们位于“状态机”中。 Rive 团队最近宣布了一项具有动态变化动画的新功能,它是“状态机”。不确定,如何在颤振项目中使用它,即如何动态改变动画的值。如果有人需要一些代码,没问题,我可以提供。此外,链接到 rive 的“状态机”https://www.youtube.com/watch?v=0ihqZANziCk。我没有找到与此新功能相关的任何示例。请帮忙!谢谢。
【问题讨论】:
【参考方案1】:rives pub 包站点上有示例。这是状态机的一个。 example_state_machine.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:rive/rive.dart';
/// An example showing how to drive two boolean state machine inputs.
class ExampleStateMachine extends StatefulWidget
const ExampleStateMachine(Key? key) : super(key: key);
@override
_ExampleStateMachineState createState() => _ExampleStateMachineState();
class _ExampleStateMachineState extends State<ExampleStateMachine>
/// Tracks if the animation is playing by whether controller is running.
bool get isPlaying => _controller?.isActive ?? false;
Artboard? _riveArtboard;
StateMachineController? _controller;
SMIInput<bool>? _hoverInput;
SMIInput<bool>? _pressInput;
@override
void initState()
super.initState();
// Load the animation file from the bundle, note that you could also
// download this. The RiveFile just expects a list of bytes.
rootBundle.load('assets/rocket.riv').then(
(data) async
// Load the RiveFile from the binary data.
final file = RiveFile.import(data);
// The artboard is the root of the animation and gets drawn in the
// Rive widget.
final artboard = file.mainArtboard;
var controller =
StateMachineController.fromArtboard(artboard, 'Button');
if (controller != null)
artboard.addController(controller);
_hoverInput = controller.findInput('Hover');
_pressInput = controller.findInput('Press');
setState(() => _riveArtboard = artboard);
,
);
@override
Widget build(BuildContext context)
return Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
title: const Text('Button State Machine'),
),
body: Center(
child: _riveArtboard == null
? const SizedBox()
: MouseRegion(
onEnter: (_) => _hoverInput?.value = true,
onExit: (_) => _hoverInput?.value = false,
child: GestureDetector(
onTapDown: (_) => _pressInput?.value = true,
onTapCancel: () => _pressInput?.value = false,
onTapUp: (_) => _pressInput?.value = false,
child: SizedBox(
width: 250,
height: 250,
child: Rive(
artboard: _riveArtboard!,
),
),
),
),
),
);
【讨论】:
【参考方案2】:另一个答案已经过时了。
class SimpleStateMachine extends StatefulWidget
const SimpleStateMachine(Key? key) : super(key: key);
@override
_SimpleStateMachineState createState() => _SimpleStateMachineState();
class _SimpleStateMachineState extends State<SimpleStateMachine>
SMITrigger? _bump;
void _onRiveInit(Artboard artboard)
final controller = StateMachineController.fromArtboard(artboard, 'bumpy');
artboard.addController(controller!);
_bump = controller.findInput<bool>('bump') as SMITrigger;
void _hitBump() => _bump?.fire();
@override
Widget build(BuildContext context)
return Scaffold(
appBar: AppBar(
title: const Text('Simple Animation'),
),
body: Center(
child: GestureDetector(
child: RiveAnimation.network(
'https://cdn.rive.app/animations/vehicles.riv',
fit: BoxFit.cover,
onInit: _onRiveInit,
),
onTap: _hitBump,
),
),
);
查看 RIVE 指南:
https://help.rive.app/runtimes/state-machines
【讨论】:
以上是关于如何在flutter中使用rive的状态机?的主要内容,如果未能解决你的问题,请参考以下文章