vmate 0.0.3 copy "vmate: ^0.0.3" to clipboard
vmate: ^0.0.3 copied to clipboard

State management with MVVM pattern

VMate #

VMate is a lightweight and intuitive Flutter package that brings ViewModel lifecycle management to your widgets with minimal boilerplate. Inspired by MVVM architecture patterns, VMate separates business logic from UI code in a clean and reusable way.

โœจ Features #

  • Simple ViewModel abstraction

  • Lifecycle hooks: onInit, onReady, and onClose

  • Access to widget parameters inside ViewModel

  • Auto-managed ViewModel binding with VMateWidget

  • Familiar StatefulWidget-like development with added structure

๐Ÿ› ๏ธ Installation #

Add this to your pubspec.yaml:

dependencies:
  vmate: <latest_version>

Then run:

flutter pub get

๐Ÿš€ Getting Started #

  1. Create your ViewModel
class CounterViewModel extends ViewModel<CounterPage> {
  late int count;

  @override
  void onInit() {
    super.onInit();
    count = widget.initialValue; // Accessing widget parameters here
    debugPrint('CounterViewModel initialized with value: $count');
  }

  void increment() {
    count++;
  }

  @override
  void onClose() {
    debugPrint('CounterViewModel disposed');
    super.onClose();
  }
}

  1. Create your Widget
class CounterPage extends VMateWidget<CounterViewModel> {
  final int initialValue;

  const CounterPage({super.key, this.initialValue = 0});

  @override
  CounterViewModel createViewModel() => CounterViewModel();

  @override
  Widget build(BuildContext context, CounterViewModel vm) {
    return Scaffold(
      appBar: AppBar(title: const Text('Counter')),
      body: Center(child: Text('Count: ${vm.count}')),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
         //replace with your own state management way
         setState(() => vm.increment());
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}

๐Ÿ” Lifecycle Hooks #

  • onInit() - Called during initState, safe for initializations.

  • onReady() - Called after the first frame is rendered.

  • onClose() - Called in dispose, good for cleanups.

๐Ÿ“ฆ Architecture #

  • ViewModel: Base class with lifecycle support and access to the widget.

  • VMState: A custom State subclass that manages the ViewModel lifecycle and binds it to the widget.

  • VMateWidget: An abstract StatefulWidget that wires everything together using VMState.

๐Ÿงช Best Practices #

  • Keep business logic in your ViewModel.

  • Avoid storing UI state in ViewModel unless explicitly necessary.

0
likes
150
points
158
downloads

Publisher

unverified uploader

Weekly Downloads

State management with MVVM pattern

Repository (GitHub)

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter

More

Packages that depend on vmate