flutter_view_model 0.1.16

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new71

flutter_view_model

pub package

A view model framework inspired by Microsofts INotifyPropertyChanged interface.

Getting Started

Add view_model to your pubspec.yaml dependencies:

...
dependencies:
  flutter:
    sdk: flutter

  flutter_view_model: <current version>
...

Usage

A basic view model:

// Import the packages.
import 'package:flutter_view_model/flutter_view_model.dart';

// 1. Create a ViewModel
class PersonViewModel extends ViewModel {
  // Properties

  String _firstName;
  static const String firstNamePropName = "firstName";
  String get firstName => _firstName;
  set firstName(String value) {
    // When the value of _firstName and value are not equal, than the _firstName value gets updated 
    // via the provided function and a PropertyChangedEvent is send. 
    if (updateValue(firstNamePropName, _firstName, value, (v) => _firstName = v)) { // When the _firstName changes than also the fullName changes.
      notifyPropertyChanged(fullNamePropName);
    }
  }

  String _lastName;
  static const String lastNamePropName = "lastName";
  String get lastName => _lastName;
  set lastName(String value) {
    if (updateValue(lastNamePropName, _lastName, value, (v) => _lastName = v)) {
      notifyPropertyChanged(fullNamePropName);
    }
  }

  // ReadOnly Property
  static const String fullNamePropName = "fullName";
  String get fullName => "$_firstName $lastNamePropName";

  // Methods

  PersonViewModel(this._firstName, this._lastName) : super();
}

A view model provider:

// 2. Create a ViewModelProvider
class PersonViewModelProvider extends ViewModelProvider<PersonViewModel> {
  // Properties

  // Methods
  PersonViewModelProvider({Key key, @required PersonViewModel viewModel, @required WidgetBuilder childBuilder}) : super(key: key, viewModel: viewModel, childBuilder: childBuilder);

  static PersonViewModel of(BuildContext context) {
    return (context.inheritFromWidgetOfExactType(PersonViewModelProvider) as PersonViewModelProvider).viewModel;
  }
}

The build function within your view:

// Within your widget build function...
  Widget build(BuildContext context) {
    // Top most Widget is the view model provider
    return LoginPageViewModelProvider( viewModel: PersonViewModel("Fred", "Flinstone")  childBuilder: (ctx) {
      // get the view Model via the PersonViewModelProvider...
      final viewModel = PersonViewModelProvider.of(ctx);
      // ViewModelPropertyWidgetBuilder rebuilds its child whenever the given property (propertyName) of the given view model changes.
      return return ViewModelPropertyWidgetBuilder(
        viewModel: viewModel,
        propertyName: PersonViewModel.fullNamePropName,
        builder: (context, _) {
          return Text(viewModel.fullName)
        }
    }));
  }

Code snipets:

	"ViewModel Class":{
		"prefix": "vmc",
		"body": [
			"class $1ViewModel extends ViewModel {",
			"  // Properties ",
			"",
			"  // Methods ",
			"",
			"  $1ViewModel(): super();",
			"",
			" }"
		]
	},

	"View Model Property":{
		"prefix": "vmp",
		"body": [
			"  ${1:type} _${2:propertyName};",
			"  static const String $2PropName = \"$2\"; ",
			"  $1 get $2 => _$2;",
			"  set $2($1 value){",
			"    updateValue($2PropName, _$2, value, (v) => _$2 = v);",
			"  }"
		]
	},

	"View Model Provider":{
		"prefix": "vmp",
		"body": [
			"class ${1:ViewModel}ViewModelProvider extends ViewModelProvider<${1:ViewModel}ViewModel> {",
			"  // Properties ",
			"",
			"  // Methods ",
			"  $1ViewModelProvider({Key key, @required ${1:ViewModel}ViewModel viewModel, @required WidgetBuilder childBuilder}) : super(key: key, viewModel: viewModel, childBuilder: childBuilder);",
			"",	
			"  static $1ViewModel of(BuildContext context) {",
			"    return (context.inheritFromWidgetOfExactType(${1}ViewModelProvider) as ${1:ViewModel}ViewModelProvider).viewModel;",
			"  }",
			"}"
		]
	},


	"View Model Property Widget Builder":{
		"prefix": "vmpwb",
		"body": [
			"  new ViewModelPropertyWidgetBuilder(",
			"  viewModel: ${1:viewModelIntance},",
			"  propertyName: \"${2:propertyName}\",",
			"  builder: (context, snapshot) {",
			"    return Text(\"$1.$2\");",
			"  ",
			"  }),"
		]
	},

[0.1.15]

ProgressBarrierWidget: Added "initialIsInProgress" to get the initial state of the widget.

[0.1.15]

BusyIndicator: changed function signature of "doAction" to "Future<void> doAction(AsyncAction action) async"

[0.1.14]

Fix flutter_view_model.dart to export BusyIndicator and BusyIndicatorBarrierWidget.

[0.1.13]

Added BusyIndicator: A model to express a busy state. Added BusyIndicatorBarrierWidget: A widget visualising a BusyIndicator isBusy state.

[0.1.12]

IExceptionDialogTextProvider: changed translateException(Exception ex) to translateExceptionOrError(Object exceptionOrError);

[0.1.11]

Added ProgressBarrierWidget: Displays a barrier that contains a the progress content (typically something like CircularProgressIndicator), when the isInProgress stream contains a TRUE value other whise the defaultContent is displayed. Added CommandProgressBarrierWidget: Child of ProgressBarrierWidget while the isInProgress stream used the commands isExecuting stream.

[0.1.10]

Added AnimatedObservableList: AnimatedObservableList encapsulate an AnimatedList widget for an ObservableList property.

[0.1.9]

Fixes: NotifyPropertyChanged isPausingSendNotifications

[0.1.8]

ObservableList<>: Removed indexOfWhere() because there is indexWhere.

[0.1.7]

  • ObservableList<>: Added indexOfWhere() to find the index of an elemente by using a test function.

[0.1.6]

  • ObservableList<>.removeWhere() is notifing ItemRemovedEvent.
  • NotifyPropertyChanged: Add method "doWhileStopNotification", which allows to manipulate the list without sending notifications.

[0.1.5]

  • Added "AnimatedObservableList": Quickly build an AnimatedList widget from an ObservableList<>.

[0.1.4]

  • Added "isPausingSendNotifications" to INotifyPropertyChanged and NotifyPropertyChanged.
  • Added "isBlank()" and changed impl. of "isEmpty()" within string_utils.dart

[0.1.3]

  • fixes AppNavigator.

[0.1.2]

  • Readme corrections.

[0.1.1]

  • Readme corrections.

[0.1.0]

  • Initial version.

example/main.dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_view_model/flutter_view_model.dart';


class PersonViewModel extends ViewModel {
  // Properties

  String _firstName;
  static const String firstNamePropName = "firstName";
  String get firstName => _firstName;
  set firstName(String value) {
    if (updateValue(firstNamePropName, _firstName, value, (v) => _firstName = v)) {
      notifyPropertyChanged(fullNamePropName);
    }
  }

  String _lastName;
  static const String lastNamePropName = "lastName";
  String get lastName => _lastName;
  set lastName(String value) {
    if (updateValue(lastNamePropName, _lastName, value, (v) => _lastName = v)) {
      notifyPropertyChanged(fullNamePropName);
    }
  }

  static const String fullNamePropName = "fullName";
  String get fullName => "$_firstName $lastNamePropName";

  // Methods

  PersonViewModel(this._firstName, this._lastName) : super();
}

class PersonViewModelProvider extends ViewModelProvider<PersonViewModel> {
  // Properties

  // Methods
  PersonViewModelProvider({Key key, @required PersonViewModel viewModel, @required WidgetBuilder childBuilder}) : super(key: key, viewModel: viewModel, childBuilder: childBuilder);

  static PersonViewModel of(BuildContext context) {
    return (context.inheritFromWidgetOfExactType(PersonViewModelProvider) as PersonViewModelProvider).viewModel;
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_view_model: ^0.1.16

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_view_model/flutter_view_model.dart';
  
Version Uploaded Documentation Archive
0.1.16 Nov 6, 2018 Go to the documentation of flutter_view_model 0.1.16 Download flutter_view_model 0.1.16 archive
0.1.15 Nov 5, 2018 Go to the documentation of flutter_view_model 0.1.15 Download flutter_view_model 0.1.15 archive
0.1.14 Nov 5, 2018 Go to the documentation of flutter_view_model 0.1.14 Download flutter_view_model 0.1.14 archive
0.1.13 Nov 5, 2018 Go to the documentation of flutter_view_model 0.1.13 Download flutter_view_model 0.1.13 archive
0.1.12 Nov 5, 2018 Go to the documentation of flutter_view_model 0.1.12 Download flutter_view_model 0.1.12 archive
0.1.11 Nov 5, 2018 Go to the documentation of flutter_view_model 0.1.11 Download flutter_view_model 0.1.11 archive
0.1.10 Nov 5, 2018 Go to the documentation of flutter_view_model 0.1.10 Download flutter_view_model 0.1.10 archive
0.1.9 Nov 3, 2018 Go to the documentation of flutter_view_model 0.1.9 Download flutter_view_model 0.1.9 archive
0.1.8 Nov 3, 2018 Go to the documentation of flutter_view_model 0.1.8 Download flutter_view_model 0.1.8 archive
0.1.7 Nov 3, 2018 Go to the documentation of flutter_view_model 0.1.7 Download flutter_view_model 0.1.7 archive

All 17 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
43
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
71
Learn more about scoring.

We analyzed this package on Nov 14, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.6
  • Flutter: 0.11.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/services/navigation/page_root_widget.dart. (-0.50 points)

Analysis of lib/services/navigation/page_root_widget.dart reported 1 hint:

line 5 col 7: This class inherits from a class marked as @immutable, and therefore should be immutable (all instance fields must be final).

Fix lib/widgets/progress_barrier/busy_indicator_widget.dart. (-0.50 points)

Analysis of lib/widgets/progress_barrier/busy_indicator_widget.dart reported 1 hint:

line 5 col 7: This class inherits from a class marked as @immutable, and therefore should be immutable (all instance fields must be final).

Fix lib/widgets/progress_barrier/command_progress_barrier_widget.dart. (-0.50 points)

Analysis of lib/widgets/progress_barrier/command_progress_barrier_widget.dart reported 1 hint:

line 7 col 7: This class inherits from a class marked as @immutable, and therefore should be immutable (all instance fields must be final).

Fix lib/widgets/progress_barrier/progress_barrier_widget.dart. (-0.50 points)

Analysis of lib/widgets/progress_barrier/progress_barrier_widget.dart reported 1 hint:

line 5 col 7: This class inherits from a class marked as @immutable, and therefore should be immutable (all instance fields must be final).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
rx_command ^4.0.2 4.0.2
rxdart ^0.19.0 0.19.0
Transitive dependencies
collection 1.14.11
meta 1.1.6
quiver_hashcode 2.0.0
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test