rive_viewmodel

Abstract interface for Rive ViewModels with lifecycle management.

Overview

This package provides a common RiveViewModel abstract class that defines the contract for generated Rive ViewModel classes. It ensures consistent lifecycle management across all ViewModels with proper resource cleanup through the dispose() method.

Features

  • Lifecycle Management: Provides dispose() method for resource cleanup
  • Disposal Tracking: Includes isDisposed getter to check disposal state
  • Pure Dart: No Flutter dependencies, can be used in any Dart project
  • Lightweight: Minimal interface with no implementation overhead

Installation

Add this package to your pubspec.yaml:

dependencies:
  rive_viewmodel: ^1.0.0

Then run:

dart pub get

Usage

This package is typically used with code generated by the rive_viewmodel_generator tool. When generating ViewModels, you can optionally enable the interface implementation.

Generated ViewModel Example

import 'package:rive_viewmodel/rive_viewmodel.dart';
import 'package:rive/rive.dart';

class MyCustomViewModel implements RiveViewModel {
  MyCustomViewModel._(this._viewModel);

  factory MyCustomViewModel.fromViewModel(ViewModelInstance viewModel) = MyCustomViewModel._;

  final ViewModelInstance _viewModel;
  bool _disposed = false;

  @override
  bool get isDisposed => _disposed;

  // ... generated getters and setters ...

  @override
  void dispose() {
    // Cleanup resources
    _viewModel.dispose();
    _disposed = true;
  }
}

Using ViewModels with the Interface

void manageViewModel(RiveViewModel viewModel) {
  // Use the ViewModel
  
  // Always dispose when done
  if (!viewModel.isDisposed) {
    viewModel.dispose();
  }
}

Benefits

  1. Type Safety: Work with ViewModels polymorphically through the common interface
  2. Consistent API: All ViewModels expose the same lifecycle methods
  3. Resource Management: Ensures proper cleanup patterns
  4. Flexibility: Optional - only use when you need the interface

License

This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.

Libraries

rive_viewmodel