state_controller 0.2.0

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

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.

Docs #

LifecycleCallback #

'onInitState' called when creating a controller in 'initState()' of your state.

'onUpdateWidget' called after changing the widget and before the method 'build()' of your state.

'onDispose' called when the 'dispose()' of your state.

mixin LifecycleSubject<W extends StatefulWidget>

'subscribe(LifecycleCallback callback)' subscribe to change lifecycle in this state.

StateController implements LifecycleCallback #

'context' get BuildContext from ControllerCreator

'changeState' this method calls 'setState()' in itself, and therefore requires the same parameters.

abstract class ControllerCreator<W extends StatefulWidget, T extends StateController> extends State<W>

This class is used to create and provide StateController

ControllerInjector<SC extends StateController>

This class injected your StateController.

Custom provide controller #

Use ControllerProvider.provide<YourStateController>(context);

Example #


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

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

class _MyHomePageState extends ControllerCreator<MyHomePage, _MyHomePageController> {

  @override
  Widget buildWidget(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:',
            ),
            Text(
              '${controller.counter}',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: IncrementFab(),
    );
  }

  @override
  _MyHomePageController createController() => _MyHomePageController();
}

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

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


class _MyHomePageController extends StateController {
  int _counter = 0;

  int get counter => _counter;
  
  void increment() {
    changeState(() {
      _counter++;
    });
  }
}

[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 '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 MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

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

class _MyHomePageState extends ControllerCreator<MyHomePage, _MyHomePageController> {

  @override
  Widget buildWidget(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:',
            ),
            Text(
              '${controller.counter}',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: IncrementFab(),
    );
  }

  @override
  _MyHomePageController createController() => _MyHomePageController();
}

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

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


class _MyHomePageController extends StateController {
  int _counter = 0;

  int get counter => _counter;

  @override
  void onInitState() {
    // TODO: implement onCreate
  }

  @override
  void onUpdateWidget() {
    // TODO: implement onWidgetUpdate
  }

  void increment() {
    changeState(() {
      _counter++;
    });
  }

  @override
  void onDispose() {
    // TODO: implement onDispose
    super.onDispose();
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  state_controller: ^0.2.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:state_controller/state_controller.dart';
  
Version Uploaded Documentation Archive
0.2.0 Dec 16, 2018 Go to the documentation of state_controller 0.2.0 Download state_controller 0.2.0 archive
0.1.1 Nov 29, 2018 Go to the documentation of state_controller 0.1.1 Download state_controller 0.1.1 archive
0.1.0 Nov 29, 2018 Go to the documentation of state_controller 0.1.0 Download state_controller 0.1.0 archive
0.0.1 Nov 29, 2018 Go to the documentation of state_controller 0.0.1 Download state_controller 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
41
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]
66
Learn more about scoring.

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

  • Dart: 2.1.0
  • pana: 0.12.13
  • Flutter: 1.2.0

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
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test