impulse_signals 0.8.0
impulse_signals: ^0.8.0 copied to clipboard
An integration of signals into the impulse_flutter package.
Impulse Signals #
Impulse extension for signals by Rody Davis. This package provides a Controller class that simplifies managing the lifecycle of multiple signals and effects, similar to SignalsMixin. It also exports impulse_flutter and signals_flutter so everything is neatly provided in a single library.
Features #
- Controller Base Class: Group related signals and effects together.
- Automatic Disposal: Signals and effects created through the controller are automatically disposed of when the controller is dropped from the Impulse store.
- Support for All Signal Types: Includes helpers for standard signals, computed signals, future signals, stream signals, and more.
Getting Started #
Add impulse_signals:
flutter pub add impulse_signals
This readme is very brief and assumes you already know how impulse_flutter works. Read the impulse_flutter documentation here.
Usage Example #
// 1. Define a reference to a Controller
final counterRef = Ref((store) => CounterController());
// 2. Define a controller
class CounterController extends Controller {
// 3. create various singals using createX functions
late final count = createSignal(0);
late final countEven = createComputed(() => count.value % 2 == 0);
void increment() => count.value++;
}
void main() {
runApp(
// 4. Wrap your application in a StoreScope
const StoreScope(child: MyApp()),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(home: CounterPage());
}
}
class CounterPage extends SignalWidget {
const CounterPage({super.key});
@override
Widget build(BuildContext context) {
// 5. Depend on the controller
final controller = context.use(counterRef);
return Scaffold(
appBar: AppBar(title: const Text('Impulse Counter Example')),
body: Center(
child: Column(
children: [
// 6. read signals from the controller
Text(
'Count: ${controller.count.value}',
style: Theme.of(context).textTheme.headlineMedium,
),
Text(
'Count is ${controller.countEven.value ? 'even' : 'odd'}.',
style: Theme.of(context).textTheme.labelSmall,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: controller.increment,
child: const Icon(Icons.add),
),
);
}
}
Modifier async utilities #
The async utilities in this package use [AsyncState] from signals, other from that they remain the same as described in impulse_flutter
Modified reactivity #
The package modifies how the store reacts to ChangeNotifier. If a value is a signal reactivity is disabled. This means it is safe to provide signals using Ref. It also automatically disposes signals whenever the Ref gets disposed. Behavior regarding ChangeNotifiers is otherwise unchanged and will behave exactly the same as it does in signals_flutter.
See also #
- impulse for core concepts and advanced usage.
- impulse_flutter for a full documentation of
impulseand how to use it in Flutter. - API reference for a detailed description of all API points.
License #
This project is licensed under the MIT License.