observable_widgets 1.0.0

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

observable_widgets

Base stateful widget that automatically subscribe itself to the app state change events. Also automatically unsubscribe when the widget is disposed.

Getting Started

You only have to do 3 things:

  1. Your States have to extend 'BaseState'
  2. This states have to implement the method 'onStateChanged', responsible of update the widget state and call set state if required.
  3. When you update your app state, you have to raise the 'StateChanged' event with the new state.

This project purpose is to learn about the framework and make simpler ways to work with it. Feel free to create pull request and create issues.

Notes

TODO:

  • Test
  • Doc

[1.0.0] - (17-09-2018)

Base stateful widget that automatically subscribe itself to the app state change events. Also automatically unsubscribe when the widget is disposed.

Getting Started

You only have to do 3 things:

  1. Your States have to extend 'BaseState'
  2. This states have to implement the method 'onStateChanged', responsible of update the widget state and call set state if required.
  3. When you update your app state, you have to raise the 'StateChanged' event with the new state.

This project purpose is to learn about the framework and make simpler ways to work with it. Feel free to create pull request and create issues.

Notes

TODO:

  • Test
  • Doc

example/example.dart

import 'package:flutter/material.dart';
import 'package:observable_widgets/base_state.dart';
import 'package:observable_widgets/state_changed.dart';
void main() => runApp(new MyApp());

class AppState {
  int _intData;
  AppState(this._intData);
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Example'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends BaseState<MyHomePage> {
  int _counter = 0;
  _MyHomePageState();

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
    //this will rebuild with +1 and then again with another +1
    //but is to fast so we only see +2
    var falseCounter = _counter +1;
    eventBus.fire(new StateChanged(new AppState(falseCounter)));
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'You have pushed the button this many times:',
            ),
            new Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  @override
  void onStateChanged(Object newStateObject) {
    // I tried to did it Generic, but the editor don't recognize any model as a class in this overriden method
    // Cast object to the model
    var newState = newStateObject as AppState;
    // Compare previous state with the new one, if is not changed, is not rebuilded
    if (_counter != newState._intData) {
      // Call setstate to update the state
      if (mounted) {
        setState(() {
          _counter = newState._intData;
        });
      }
    }
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  observable_widgets: ^1.0.0

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:observable_widgets/base_state.dart';
import 'package:observable_widgets/state_changed.dart';
  
Version Uploaded Documentation Archive
1.0.0 Sep 17, 2018 Go to the documentation of observable_widgets 1.0.0 Download observable_widgets 1.0.0 archive
0.0.2 Sep 15, 2018 Go to the documentation of observable_widgets 0.0.2 Download observable_widgets 0.0.2 archive
0.0.1 Sep 15, 2018 Go to the documentation of observable_widgets 0.0.1 Download observable_widgets 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
10
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
51
Learn more about scoring.

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

  • Dart: 2.0.0
  • pana: 0.12.3
  • Flutter: 0.8.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Suggestions

The description is too short.

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
event_bus ^1.0.1 1.0.1
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
build_runner ^0.9.0
flutter_test
json_serializable ^0.5.4