signals_flutter 6.0.0-rc.0 signals_flutter: ^6.0.0-rc.0 copied to clipboard
The signals library exposes four core functions which are the building blocks to model any business logic you can think of.
import 'package:flutter/material.dart';
import 'package:signals_flutter/signals_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
home: const Example(),
);
}
}
class Example extends StatefulWidget {
const Example({super.key, this.title = 'Example'});
final String title;
@override
State<Example> createState() => _ExampleState();
}
class _ExampleState extends State<Example> with SignalsMixin {
late final _counter = this.createSignal(0);
void _incrementCounter() => _counter.value++;
@override
void initState() {
super.initState();
_counter.onDispose(() {
debugPrint('disposed ${_counter.globalId} counter!');
});
createEffect(() {
debugPrint('count: ${_counter()}');
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
actions: [
IconButton(
tooltip: 'Open another counter',
icon: const Icon(Icons.timer),
onPressed: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const Example(title: 'Another Counter'),
),
),
),
],
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}