state_groups 0.3.1
state_groups: ^0.3.1 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({Key? key}) : super(key: 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({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends SyncState<void, MyHomePage> {
int _counter = 0;
_MyHomePageState() : super(exampleStateGroup);
// 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.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}