vm 1.0.4 copy "vm: ^1.0.4" to clipboard
vm: ^1.0.4 copied to clipboard

A very simple flutter plugin that implements the `MVVM` pattern.

Flutter VM (ViewModel) #

A very simple flutter plugin that implements the MVVM pattern.

Model–View–ViewModel (MVVM) is a software architectural pattern that facilitates the separation of the development of the graphical user interface (the view) – be it via a markup language or GUI code – from the development of the business logic or back-end logic (the model) so that the view is not dependent on any specific model platform.

Build Status Pub package Star on GitHub License: MIT

MVVM

Getting Started #

  • Add this to your pubspec.yaml
    dependencies:
    vm: ^1.0.3
    
  • Get the package from Pub:
    flutter packages get
    
  • Import it in your file
    import 'package:vm/vm.dart';
    

Features #

...

Usage #

First create a ViewModel

class CounterViewModel extends ViewModel {
  int value = 0;
  int progress = 0;

  void increment() {
    value++;
    if (value % 5 == 0) {
      progress += 1;
    }
    notifyListeners();
  }
}

Using CounterViewModel with ViewModelBuilder

ViewModelBuilder<CounterViewModel>(
  model: CounterViewModel(),
  builder: (context, model, child) {
    return Text('Counter: ${model.value}');
  },
)

Rebuild only if progress changed

ViewModelBuilder<CounterViewModel>(
  model: counterModel,
  shouldRebuild: (prev, next) => prev.progress != next.progress
  builder: (context, model, child) => Text('Progress: ${model.progress}'),
)

Or using context.select

ViewModelBuilder<CounterViewModel>(
  model: counterModel,
  builder: (context, model, child) => const _Progress(),
)
class _Progress extends StatelessWidget {
  const _Progress({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final progress = context.select((CounterViewModel m) => m.progress);
    return Text('Progress: $progress');
  }
}

Rebuild only if progress is an odd number

ViewModelBuilder<CounterViewModel>(
  model: counterModel,
  shouldRebuild: (prev, next) => next.progress & 1 == 1,
  builder: (context, model, child) => Text('Progress: ${model.progress}'),
)

Check out the complete Example

Changelog #

Please have a look in CHANGELOG

Maintainers #

License #

License: MIT

3
likes
160
points
30
downloads

Publisher

unverified uploader

Weekly Downloads

A very simple flutter plugin that implements the `MVVM` pattern.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, provider

More

Packages that depend on vm