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.

Use StatefulWidget as a constructor, the widget is passed to the controller. A big request to use the methods of the life cycle controller.

Docs

StateController

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

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

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

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

ControllerInjector>

ControllerInjector is an mixin. For the status link and the controller, you must specify YourState with ControllerInjector and override method createStateController().

Example


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

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

class _MyHomePageState extends State<MyHomePage>
    with ControllerInjector<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:',
            ),
            Text(
              '${controller.counter}',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: controller.increment,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }

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

class _MyHomePageController extends StateController<MyHomePage> {
  int _counter = 0;
  int get counter => _counter;

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

Libraries

state_controller
Copyright 2018 Miolin Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.