state_controller 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 46

state_controller #

https://pub.dartlang.org/packages/state_controller

A new Flutter package.

About #

This library is aimed at separating the logic of drawing a widget and changing its state. Please look at the example in the project.

Example #


class App extends StatelessWidget {
// This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ControllerProviderConfig(
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyHomePage(title: 'Flutter Demo Home Page'),
      ),
      configurations: [
        Configuration(
          type: CounterController,
          creator: () => CounterController(),
          singleton: true,
        ),
      ],
    );
  }
}

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

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState
    extends StateWithController<MyHomePage, CounterController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            StreamBuilder(
              initialData: 0,
              stream: controller.counterStream,
              builder: (context, snapshot) {
                return Text(
                  '${snapshot.data}',
                  style: Theme.of(context).textTheme.display1,
                );
              },
            ),
          ],
        ),
      ),
      floatingActionButton: IncrementFab(),
    );
  }
}

class IncrementFab extends StatefulWidget {
  @override
  _IncrementFabState createState() => _IncrementFabState();
}

class _IncrementFabState
    extends StateWithController<IncrementFab, CounterController> {
  @override
  Widget build(BuildContext context) {
    return FloatingActionButton(
      onPressed: controller.increment,
      tooltip: 'Increment',
      child: Icon(Icons.add),
    );
  }
}

class CounterController extends StateController {
  int _count = 0;
  final StreamController<int> _counterStreamController = StreamController();

  Stream<int> get counterStream => _counterStreamController.stream;

  @override
  void init() {
    // TODO: implement init
  }

  @override
  void onConfigurationChanged() {
    // TODO: implement onConfigurationChanged
  }

  void increment() {
    _counterStreamController.add(++_count);
  }

  @override
  void dispose() {
    _counterStreamController.close();
    super.dispose();
  }
}


Docs #

ControllerProviderConfig #

Used to configure the 'ControllerProvider' and support hot reload.

ControllerProvider #

method 'addControllerConfiguration(Configuration configuration)' allows you to add a configuration at run time.

method 'provide

StateController #

method get 'context' returns 'BuildContext' from the last updated 'Widget' (use carefully).

mixin ControllerInjector

This class is used to create and provide 'StateController' in your 'State'.

abstract StateWithController extends State with ControllerInjector #

Only be used for a short record if you are not inheriting from your 'State'.

[1.0.0] - 23.02.2019

  • Global refactor
  • Change API (look README.md)
  • Hot reload support

[0.2.0] - 16.12.2018

  • Added LifecycleSubject and LifecycleCallback
  • Add ControllerCreator, ControllerInheritedWidget, ControllerProvider
  • Change class ControllerInjector
  • Remove widget in StateController

[0.1.1] - 29.11.2018

  • fix README for pub.dartlang.

[0.1.0] - 29.11.2018

  • Added more details in README.

[0.0.1] - 29.11.2018

  • Initial release.

example/lib/main.dart

import 'dart:async';

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

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

class App extends StatelessWidget {
// This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ControllerProviderConfig(
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyHomePage(title: 'Flutter Demo Home Page'),
      ),
      configurations: [
        Configuration(
          type: _MyHomePageController,
          creator: () => _MyHomePageController(),
          singleton: true,
        ),
      ],
    );
  }
}

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

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState
    extends StateWithController<MyHomePage, _MyHomePageController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            StreamBuilder(
              stream: controller.counterStream,
              builder: (context, snapshot) {
                return Text(
                  '${snapshot.data}',
                  style: Theme.of(context).textTheme.display1,
                );
              },
            ),
          ],
        ),
      ),
      floatingActionButton: IncrementFab(),
    );
  }
}

class IncrementFab extends StatefulWidget {
  @override
  _IncrementFabState createState() => _IncrementFabState();
}

class _IncrementFabState
    extends StateWithController<IncrementFab, _MyHomePageController> {
  @override
  Widget build(BuildContext context) {
    return FloatingActionButton(
      onPressed: controller.increment,
      tooltip: 'Increment',
      child: Icon(Icons.add),
    );
  }
}

class _MyHomePageController extends StateController {
  int _count = 0;
  final StreamController<int> _counterStreamController = StreamController()
    ..add(0);

  Stream<int> get counterStream => _counterStreamController.stream;

  @override
  void init() {
    // TODO: implement init
  }

  @override
  void onConfigurationChanged() {
    // TODO: implement onConfigurationChanged
  }

  void increment() {
    _counterStreamController.add(++_count);
  }

  @override
  void dispose() {
    _counterStreamController.close();
    super.dispose();
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  state_controller: ^1.0.0

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:state_controller/state_controller.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
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]
46
Learn more about scoring.

We analyzed this package on Oct 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/state_controller.dart.

Run flutter format to format lib/state_controller.dart.

Maintenance suggestions

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test