How davidchow24/simple_controller works
Project type and purpose
• Type: A Flutter “plugin” / Dart package that provides a lightweight state management solution.
• Purpose: To offer an intuitive API (SimpleController, createState, createCommand, Provider widgets, context.use, controller.build) for managing reactive UI state with minimal boilerplate.
File structure
• lib/simple_controller.dart: The single entry point containing all core classes and APIs.
• example/: A full Flutter project showcasing usage on Android, iOS, web, desktop.
• pubspec.yaml: Declares Flutter as dependency, marks package as a plugin.
README for additional insights
• Core concepts: SimpleController (extends ChangeNotifier), reactive state (createState, createRefState), commands (createCommand), Provider widgets, context.use<T>(), controller.build().
• No external services or databases—purely in-process state management.
Main components
A. Flutter Application (UI Layer)
– Widgets tree
– Example app’s CounterWidget, CounterButton, MainApp, etc.
B. simple_controller Package
– SimpleController (base class)
– State objects (State<T>, RefState<T>)
– Command objects (Command<T>)
– Provider widgets (SimpleControllerProvider, multi)
– Extension method (BuildContext.use<T>())
– Builder method (controller.build())
C. Underlying Flutter Framework
– ChangeNotifier (foundation)
– InheritedWidget mechanism (used by Provider)
– BuildContext, Element tree
Relationships and data flows
1. Provider widget injects one or more SimpleController instances into the Flutter widget tree (InheritedWidget).
2. Any descendant widget calls context.use<T>() to retrieve a controller instance.
3. Widgets invoke controller.createCommand().execute(...) or mutate state via controller.createState().value.
4. SimpleController (ChangeNotifier) calls notifyListeners().
5. controller.build(select:…, builder:…) listens to changes and triggers rebuild of only affected widgets.
Architectural patterns and design principles
• Observer Pattern: SimpleController notifies listeners upon state changes.
• Provider Pattern: Controllers are scoped to widget subtrees.
• Reactive Programming: createState and createRefState produce “reactive” state holders.
• Separation of Concerns: UI widgets remain stateless; logic lives in controllers.
Essential elements
– Clear labels for each component and subcomponent
– Highlight of key APIs and their interactions