LogoSignals.dart
Copy Markdown
rodydavis/signals.dart 999999

SignalStatefulWidget

A reactive StatefulWidget that implicitly tracks and rebuilds on signal changes.

A reactive StatefulWidget that implicitly tracks and rebuilds on signal changes.

Subclasses of SignalStatefulWidget establish a dynamic reactive context directly at the Flutter element layer. Any signal value accessed synchronously inside State's build method is implicitly tracked and subscribed to. When a signal changes, only that element is rebuilt.

This offers a clean, Javascript-style reactivity experience while retaining all stateful lifecycles. It is the modern, highly recommended replacement for the legacy, deprecated SignalsMixin.

Implicit Reactivity Example (Stateful)#

class CounterControl extends SignalStatefulWidget {
  const CounterControl({super.key});

  @override
  State<CounterControl> createState() => _CounterControlState();
}

class _CounterControlState extends State<CounterControl> {
  // Create state signals directly inside the State object:
  final count = signal(0);

  @override
  Widget build(BuildContext context) {
    // Automatically tracked on access and safely unsubscribed on unmount:
    return ElevatedButton(
      onPressed: () => count.value++,
      child: Text('Count: ${count.value}'),
    );
  }
}

Important

Only signals accessed synchronously during the execution of the build method are tracked. Signals read inside async callbacks, listeners, or deferred tasks are not subscribed to.

Constructors#

View Constructors
SignalStatefulWidget({super.key})

Constructor for SignalStatefulWidget.

Methods#

View Methods
StatefulElement createElement()