state_controller 1.0.0 state_controller: ^1.0.0 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.
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<W extends StatefulWidget, T extends StateController> on State
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'.