mvvm_builder 1.0.8+4

  • Readme
  • Changelog
  • Example
  • Installing
  • 76

mvvm_builder #

mvvm_builder is a Flutter plugin to help you implement MVVM design pattern with flutter. MVVM = Model - View - ViewModel

Installation #

To use this plugin, add mvvm_builder as a dependency in your pubspec.yaml file.

Why use MVVM design patterns #

Implement a good design pattern can just simplify code, make more code testable, allow better performance... The idea is to split business logic from your view. Your view has to stay dumb, and this plugin will help you to respect the pattern.

Usage #

1 - import MVVMPage Widget from package

import 'package:mvvm_builder/mvvm_builder.dart';

2 - Create your Model #

class MyViewModel extends MVVMModel {
  String title;
  List<TodoModel> todoList;
}

class TodoModel {
  String title, subtitle;

  TodoModel(this.title, this.subtitle);
}

3 - Create your Presenter #

class MyPresenter extends Presenter<MyViewModel> {

  MyPresenter(MyViewModel model, MyViewInterface view) : super(model, view);

  @override
  Future onInit() async {
    this.viewModel.title = "My todo list";
    this.viewModel.todoList = List();
    // init your view model here -- load from network etc ... 
    for(int i = 0; i < 15; i++) {
      this.viewModel.todoList.add(new TodoModel("TODO $i", "my task $i"));
    }
    this.refreshView(); // call this at the end if onInit takes time
  }
}

4 - define your view interface #

for example:

abstract class MyViewInterface {
  
  void showMessage(String message);
  
}

5 - Create your Page #

You page must implement your view interface. For example :

class _MyAppState extends State<MyApp> implements MyViewInterface{
  
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();

  @override
  void initState() {
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MVVMPage<MyPresenter, MyViewModel>(
        builder: (context, presenter, model) {
          return Scaffold(
            key: _scaffoldKey,
            appBar: AppBar(title: Text(model.title)),
            body: ListView.separated(
              itemBuilder: (context, index) => InkWell(
                onTap: () => presenter.onClickItem(index),
                child: ListTile(
                  title: Text(model.todoList[index].title),
                  subtitle: Text(model.todoList[index].subtitle),
                ),
              ),
              separatorBuilder: (context, index) => Divider(height: 1) ,
              itemCount: model.todoList.length
            )
          );
        },
        presenter: MyPresenter(new MyViewModel(), this),
      )
    );
  }

  @override
  void showMessage(String message) {
    _scaffoldKey.currentState.showSnackBar(new SnackBar(content: Text(message)));
  }
}

Test #

This pattern is really usefull for testing as it allows you to test business logic separately from rendering. The another good thing of this pattern is the ability to test your view with many combination of viewModel settings without the need of chaining actions before having the desired view state. You can now test your view alone, presenter alone and test them together.

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

1.0.8+4 #

  • Add function to be called after view init in presenter

1.0.8+3 #

  • Calling setState now verify if object is mounted to avoid memory leaks

1.0.8+2 #

  • Presenter will no longer crash if call refreshView with a mockedView

1.0.8 #

  • adding presenter getter for MVVMPage in order to test
  • adding Key to MVVMPage

1.0.7+4 #

  • fix documentation

1.0.7+3 #

  • fix kotlin version to 1.3.10

1.0.7+2 #

  • fix build error on iOS : ld: symbol(s) not found for architecture arm64

1.0.7+1 #

  • fix build error on iOS : ld: symbol(s) not found for architecture arm64

1.0.7 #

  • update to swift 5

1.0.6 #

  • fix ios import swift file

1.0.5 #

  • fix pod swift version in podspec

1.0.4 #

  • fix pod swift version to => config.build_settings['SWIFT_VERSION'] = '4.2'

1.0.3 #

  • _MVVMContentState now call super.dispose first

1.0.2 #

  • kotlin_version is now '1.3.0'

1.0.1 #

  • Fix formatting i18N
  • fix pubspec descrption, removed author

1.0.0 #

  • Fix formatting
  • Removed deprecated method inheritFromWidgetOfExactType from presenter_builder

0.0.1 #

  • First version : Creates a MMVMPage with MVVM design pattern

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:mvvm_builder/mvvm_builder.dart';

void main() => runApp(MyApp());


class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> implements MyViewInterface{

  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();

  @override
  void initState() {
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MVVMPage<MyPresenter, MyViewModel>(
        builder: (context, presenter, model) {
          return Scaffold(
            key: _scaffoldKey,
            appBar: AppBar(title: Text(model.title)),
            body: ListView.separated(
              itemBuilder: (context, index) => InkWell(
                onTap: () => presenter.onClickItem(index),
                child: ListTile(
                  title: Text(model.todoList[index].title),
                  subtitle: Text(model.todoList[index].subtitle),
                ),
              ),
              separatorBuilder: (context, index) => Divider(height: 1) ,
              itemCount: model.todoList.length
            )
          );
        },
        presenter: MyPresenter(new MyViewModel(), this),
      )
    );
  }

  @override
  void showMessage(String message) {
    _scaffoldKey.currentState.showSnackBar(new SnackBar(content: Text(message)));
  }
}

abstract class MyViewInterface {

  void showMessage(String message);

}


class MyPresenter extends Presenter<MyViewModel, MyViewInterface> {

  MyPresenter(MyViewModel model, MyViewInterface myView) : super(model, myView);

  @override
  Future onInit() async {
    this.viewModel.title = "My todo list";
    this.viewModel.todoList = List();
    for(int i = 0; i < 15; i++) {
      this.viewModel.todoList.add(new TodoModel("TODO $i", "my task $i"));
    }
    this.refreshView();
  }

  onClickItem(int index) {
    viewInterface.showMessage("Item clicked $index");
  }
}


class MyViewModel extends MVVMModel {
  String title;
  List<TodoModel> todoList;
}

class TodoModel {
  String title, subtitle;

  TodoModel(this.title, this.subtitle);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  mvvm_builder: ^1.0.8+4

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:mvvm_builder/mvvm_builder.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
52
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
76
Learn more about scoring.

We analyzed this package on Apr 3, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Format lib/component_builder.dart.

Run flutter format to format lib/component_builder.dart.

Format lib/generated/i18n.dart.

Run flutter format to format lib/generated/i18n.dart.

Format lib/presenter_builder.dart.

Run flutter format to format lib/presenter_builder.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test