我是 flutter 新手,对此一无所知,我从 rive 应用程序创建了一个简单的设计,并尝试在 flutter 应用程序上运行,但动画不运行。我从 flutter 下载的动画作品很少起作用,有些不起作用,下面链接上的一个也不起作用。它只是静态的 png 图像。我尝试将动画名称更改为idle或center,但仍然不起作用。
这是我正在使用的代码,
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:rive/rive.dart';
class ExampleAnimation extends StatefulWidget {
const ExampleAnimation({Key? key}) : super(key: key);
@override
_ExampleAnimationState createState() => _ExampleAnimationState();
}
class _ExampleAnimationState extends State<ExampleAnimation> {
void _togglePlay() {
if (_controller == null) {
return;
}
setState(() => _controller!.isActive = !_controller!.isActive);
}
bool get isPlaying => _controller?.isActive ?? false;
Artboard? _riveArtboard;
RiveAnimationController? _controller;
@override
void initState() {
super.initState();
rootBundle.load('assets/rive/new.riv').then(
(data) async {
// Load the RiveFile from the binary data.
final file = RiveFile.import(data);
final artboard = file.mainArtboard;
// ignore: cascade_invocations
artboard.addController(_controller = SimpleAnimation('animate'));
setState(() => _riveArtboard = artboard);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Animation Example'),
),
body: Center(
child: _riveArtboard == null
? const SizedBox()
: Rive(artboard: _riveArtboard!),
),
floatingActionButton: FloatingActionButton(
onPressed: _togglePlay,
tooltip: isPlaying ? 'Pause' : 'Play',
child: Icon(
isPlaying ? Icons.pause : Icons.play_arrow,
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
我刚刚遇到了同样的问题,在之前的回答中,他提到更改为他工作的动画控制器的名称,不幸的是,我不知道在哪里选择该动画控制器值以及它是什么,所以只是想澄清一下。
在你的 Rive 画板中,会有一个名为动画的列,你必须为动画控制器指定该名称,以选择要播放的动画。

这是动画名称不正确,在这一行我只需更改 rive 应用程序上的动画名称。
artboard.addController(_controller = SimpleAnimation('Animation Name'));
Run Code Online (Sandbox Code Playgroud)
对于我来说,动画名称只是“动画 1”。所以添加它解决了我的问题。
| 归档时间: |
|
| 查看次数: |
4038 次 |
| 最近记录: |