ViewWidget<T extends ViewModel> class abstract

A ViewWidget is a simplified version of a StatefulWidget + ViewState. It is a generic class that takes a ViewModel as a type parameter. By default, it uses the built-in service locator to resolve the ViewModel. You can register your ViewModel in the service locator before running the app:

void setupLocator() {
  SL.I.registerFactory(() => CounterViewModel());
}

You can override the resolveViewModel method to inject a ViewModel. This is useful for testing.

Cascade State Composition (CSC)

ViewWidget enables Cascade State Composition where each widget maintains its own isolated ViewModel while cascading state changes to children through reactive constructor injection.

How it works:

  • Parent manages its own state via ViewModel
  • Parent injects data into children via constructor
  • Children receive injected data and manage their own state
  • State changes cascade down but never up
// Each level can:
// 1. Modify its own state
// 2. Inject data into child (influencing child's UI)

Parent
  ├─ Own State: ParentViewModel
  └─ Injects into child: parentData
       ↓
     Child
       ├─ Own State: ChildViewModel
       ├─ Receives from parent: parentData
       └─ Injects into child: childData
            ↓
          GrandChild
            ├─ Own State: GrandChildViewModel
            └─ Receives from parent: childData
Inheritance

Constructors

ViewWidget({Key? key})
const

Properties

hashCode → int
The hash code for this object.
no setterinherited
key → Key?
Controls how one widget replaces another widget in the tree.
finalinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

build(BuildContext context, T viewModel) → Widget
Override this method to provide a Widget to be built.
createElement() → StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() → ViewState<T, ViewWidget<T>>
Creates the mutable state for this widget at a given location in the tree.
override
debugDescribeChildren() → List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onInit(BuildContext context, T viewModel) → void
Override this method to provide a custom onInit callback.
onUpdate(BuildContext context, T viewModel) → void
Override this method to react to widget updates.
resolveViewModel(BuildContext context) → T?
Override this method to provide a custom ViewModel instance. By default, it retrieves the ViewModel from the service locator. Override this method to provide a custom ViewModel instance using a different method. e.g. GetIt, Provider, Constructor injection etc.
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) → String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) → String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String
Returns a one-line detailed description of the object.
inherited
toStringShort() → String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited