state_controller 0.1.1 state_controller: ^0.1.1 copied to clipboard
A new Flutter package.
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<W extends StatefulWidget, SC extends StateController
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++;
});
}
}