state_groups 0.4.0
state_groups: ^0.4.0 copied to clipboard
State management for the rest of us. State_groups is an easy way of managing state. In fact it doesn't manage state at all, it's stateless and only focuses on messaging.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:state_groups/state_groups.dart';
StateGroup<void> exampleStateGroup = StateGroup<void>();
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({required this.title, super.key});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends SyncState<void, MyHomePage> {
_MyHomePageState() : super(exampleStateGroup);
int _counter = 0;
// Normally we would use a setState() call here but because we're using state
// groups we don't have to
void _incrementCounter() {
_counter++;
exampleStateGroup.notifyAll();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
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),
),
);
}
}