SignalWidget<T extends SignalModel> class

A widget intended to be used with signals.

It automatically rebuilds when one of the provided signals are emitted. The constructor takes an optional model argument. This way the SignalModel.dispose and SignalModel.init can automatically be called at appropriate times, init is called when it is passed to this widget and dispose is called when the widget's own dispose method is called. The passed model can be accessed inside the SignalWidget.builder using following way:

  final myModel = ModelStore.get<ModelType>();

Or you can create a convenient model getter inside your model in this manner.

    static MyModel get get {
       final model = ModelStore.get<MyModel>();
      if(model == null) {
      // You might return the model as well instead of throwing
      // and use null check operator instead. I do this just for
      // for a cleaner look when using this method(no ! sign in the code).
        throw("MyModel not found");
      }
       return model;
     }

Now you can access the model in this way:

final model = MyModel.get;
//or use it directly
MyModel.get.myModelFields;

Let us assume you have a model that holds an integer(count) and your ui shows the current value of the count. The model may look like this:

class CountModel extends SignalModel {
    static final countChanged = Signal();

    //model Accessor
     static CountModel get get {
       final model = ModelStore.get<CountModel>();
       if(model == null) {
         throw("CountModel not found");
       }
       return model;
     }

     //main parts
     void incrementCount() {
       _counter++;
       countChanged();
     }
     int get count => _counter;

     int _counter = 0;
}

When the user changes the value, the user interface should be updated. This can be implemented in the following way:

       SignalWidget(
          signal: CountModel.countChanged,
          model: CountModel(),
          builder: (context) => Text(
           CountModel.get.count.toString(), //<-- model being accessed
           style: Theme.of(context).textTheme.headlineMedium,
            ),
          ),

See ModelStore class to know more about managing models manually.

Inheritance

Constructors

SignalWidget({Set<Signal>? signals, required Widget builder(BuildContext context), Signal? signal, T? model, VoidCallback? onInit, VoidCallback? onDispose, VoidCallback? onActivate, VoidCallback? onDeactivate})

Properties

builder Widget Function(BuildContext context)
The widget to be built by this widget.
final
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
onActivate VoidCallback?
A callback which is called when this widget is reinserted into the tree after having been removed via deactivate method of the state object of this widget.
final
onDeactivate VoidCallback?
A callback which is called when the deactivate method of this widget's state is called.
final
onDispose VoidCallback?
A callback which does some work when the widget is disposed.
final
onInit VoidCallback?
A callback which does some work when the widget is created.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
signal Signal?
Singular variant of SignalWidget.signals for aesthetic purposes.
final
signals Set<Signal>?
Signals which when emitted rebuilds the widget.
final

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() State<StatefulWidget>
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
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