view_model 0.1.2

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

view_model #

Support MVVM Pattern

Example usages

Getting Started #

For help getting started with Flutter, view our online documentation.

For help on editing package code, view the documentation.

0.1.2 #

  • Added example project
  • Refactor ViewModelProvider
  • Deleting Page and PageState
  • Added method ViewModel.newInheritedWidget

0.1.1 #

  • Initial Release.

example/lib/main.dart

import 'dart:async';

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

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

class MyApp extends StatelessWidget {
  final ViewModelFactory factory = MyViewModelFactory();

  @override
  Widget build(BuildContext context) {
    return ViewModelProvider(
      factory: factory,
      child: buildApp(context),
    );
  }

  Widget buildApp(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  HomeViewModel viewModel;

  @override
  Widget build(BuildContext context) {
    viewModel = ViewModelProvider.of(context).provide(HomeViewModel);
    return Scaffold(
      appBar: AppBar(
        title: Text('ViewModel test'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            StreamBuilder(
              stream: viewModel.clickStream,
              builder: (context, snapshot) =>
                  Text(snapshot.hasData ? '${snapshot.data}' : '0'),
            )
          ],
        ),
      ),
      floatingActionButton: ViewModel.newInheritedWidget(
          viewModel: viewModel,
          child: MyWidget()),
    );
  }

  @override
  void dispose() {
    viewModel.dispose();
    super.dispose();
  }
}

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  HomeViewModel viewModel;

  @override
  Widget build(BuildContext context) {
    viewModel = ViewModel.of<HomeViewModel>(context);
//    ------------ or ------------
//    viewModel = ViewModelProvider.of(context).provide(HomeViewModel);
//    Provider ensures the singleton of the instance before it is destroyed
    return FloatingActionButton(
      onPressed: () {
        viewModel.increment();
      },
      tooltip: 'Increment',
      child: new Icon(Icons.add),
    );
  }
}

class HomeViewModel extends ViewModel {
  StreamController<int> _clickStreamController;
  Stream<int> clickStream;

  HomeViewModel(ViewModelProvider provider) : super(provider) {
    _clickStreamController = new StreamController<int>.broadcast();
    var counter = 0;
    clickStream = _clickStreamController.stream.map((val) {
      counter += val;
      return counter;
    });
  }

  void increment() {
    _clickStreamController.add(1);
  }

  @override
  void dispose() {
    // implement dispose
    super.dispose();
  }
}

class MyViewModelFactory extends ViewModelFactory {

  @override
  T crateViewModel<T extends ViewModel>(Type type, ViewModelProvider provider) {
    var vm;
    switch (type) {
      case HomeViewModel:
        vm = HomeViewModel(provider);
        break;
    }
    if (vm == null) throw Exception('Not implemented class $type');
    return vm;
  }

}

Use this package as a library

1. Depend on it

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


dependencies:
  view_model: ^0.1.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or 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:view_model/view_model.dart';
  
Version Uploaded Documentation Archive
0.2.1 Jul 30, 2018 Go to the documentation of view_model 0.2.1 Download view_model 0.2.1 archive
0.2.0 Jul 29, 2018 Go to the documentation of view_model 0.2.0 Download view_model 0.2.0 archive
0.1.2 Jul 29, 2018 Go to the documentation of view_model 0.1.2 Download view_model 0.1.2 archive
0.1.1 Jul 27, 2018 Go to the documentation of view_model 0.1.1 Download view_model 0.1.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
--
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
--
Overall:
Weighted score of the above. [more]
0
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://dart.dev/dart-2#migration.

Maintenance issues and suggestions

Make sure dartdoc successfully runs on your package's source files. (-10 points)

Dependencies were not resolved.