数据绑定
数据绑定让你可以把 Rive 编辑器中设置为可绑定的元素连接到运行时代码。通过 View Model(视图模型)和 ViewModelInstance(视图模型实例),你可以读取、写入并监听属性变化,从而让 Flutter 应用中的状态与 Rive 动画保持同步。
数据绑定通常包含以下步骤:
- 从
.riv文件中获取ViewModel。 - 创建或获取一个
ViewModelInstance。 - 将
ViewModelInstance绑定到状态机或 Artboard。 - 读取、写入或监听绑定属性。
如果你使用 RiveWidgetController,很多绑定步骤会自动完成。
视图模型
ViewModel 是在 Rive 编辑器中定义的数据结构。它描述了一组可绑定属性,例如数字、字符串、布尔值、颜色、枚举、图片、列表、Artboard 等。
在 Flutter 运行时中,你可以从 File 获取视图模型:按名称获取、按索引获取,或获取某个 Artboard 的默认视图模型。
如果你正在使用 RiveWidgetController,可以跳过手动创建 ViewModel 的步骤,直接前往 视图模型实例。
// Get reference to the File and Artboard
final file = await File.asset(
'assets/my_file.riv',
riveFactory: Factory.rive,
);
final artboard = file!.defaultArtboard()!;
// Get reference by name
file.viewModelByName("My View Model");
// Get reference by index
for (var i = 0; i < file.viewModelCount; i++) {
final indexedVM = file.viewModelByIndex(i);
}
// Get reference to the default view model for an artboard
final defaultVM = file.defaultArtboardViewModel(artboard);
// Dispose the view model when you're no longer using it
viewModel.dispose();
视图模型实例
ViewModelInstance 是 ViewModel 的具体实例。你可以把它理解为视图模型的数据对象:ViewModel 定义有哪些属性,而 ViewModelInstance 持有这些属性的实际值。
一个 ViewModel 可以有多个实例。实例可以是空白实例,也可以来自 Rive 编辑器中导出的默认实例、命名实例或索引实例。
使用 RiveWidgetController
如果你使用 RiveWidgetController,可以通过 controller.dataBind(...) 直接创建并绑定视图模型实例。
// Get reference to the File
file = await File.asset(
'assets/rewards.riv',
riveFactory: Factory.rive,
);
// Create a controller
controller = RiveWidgetController(file!);
// Data bind by name
viewModelInstance = controller.dataBind(DataBind.byName('My View Model'));
// Data bind by index
viewModelInstance = controller.dataBind(DataBind.byIndex(0));
// Auto data bind
viewModelInstance = controller.dataBind(DataBind.auto());
// Bind some existing view model instance to the controller:
viewModelInstance = controller.dataBind(DataBind.byInstance(someViewModelInstance));
// Dispose of objects you created when no longer needed
viewModelInstance.dispose();
controller.dispose();
file.dispose();
手动管理视图模型实例
如果你想自己管理视图模型实例的创建,可以从 ViewModel 创建实例。
final vm = file.viewModelByName("My View Model")!;
// Create blank
final vmiBlank = vm.createInstance();
// Create default
final vmiDefault = vm.createDefaultInstance();
// Create by index
for (int i = 0; i < vm.instanceCount; i++) {
final vmiIndexed = vm.createInstanceByIndex(i);
}
// Create by name
final vmiNamed = vm.createInstanceByName("My Instance");
// Dispose the view model instance
viewModelInstance.dispose();
绑定
绑定是把 ViewModelInstance 连接到状态机或 Artboard 的过程。绑定后,Rive 文件中使用该视图模型的属性会与运行时代码中的实例数据联动。
如果你使用 RiveWidgetController,调用以下任意方法时会自动完成绑定:
viewModelInstance = controller.dataBind(DataBind.auto());
viewModelInstance = controller.dataBind(DataBind.byName('My View Model'));
viewModelInstance = controller.dataBind(DataBind.byIndex(0));
viewModelInstance = controller.dataBind(DataBind.byInstance(someViewModelInstance));
否则,你需要确保把视图模型实例绑定到状态机或 Artboard。
final file = await File.asset(
'assets/my_file.riv',
riveFactory: Factory.rive,
);
final artboard = file!.defaultArtboard();
final stateMachine = artboard!.defaultStateMachine()!;
final vm = file.defaultArtboardViewModel(artboard)!;
final vmi = vm.createDefaultInstance()!;
// Bind to the state machine. This automatically binds to the artboard as well.
stateMachine.bindViewModelInstance(vmi);
// If you're not using a state machine, bind to the artboard
artboard.bindViewModelInstance(vmi);