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()' use to get the controller.

StateController

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

mixin ControllerInjector 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'.

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.