shard 0.0.2
shard: ^0.0.2 copied to clipboard
A powerful, lightweight state management solution for Flutter with built-in persistence, debounce, throttle, and seamless widget integration.
import 'package:flutter/material.dart';
import 'package:shard/shard.dart';
void main() {
runApp(const MyApp());
}
/// Simple counter shard example
class CounterShard extends Shard<int> {
CounterShard() : super(0);
void increment() => emit(state + 1);
void decrement() => emit(state - 1);
void reset() => emit(0);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Shard Counter Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: ShardProvider<CounterShard>(
create: () => CounterShard(),
child: const CounterScreen(),
),
);
}
}
class CounterScreen extends StatelessWidget {
const CounterScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Shard Counter Example'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ShardBuilder<CounterShard, int>(
builder: (context, count) {
return Text(
'$count',
style: Theme.of(context).textTheme.displayLarge,
);
},
),
const SizedBox(height: 24),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () => context.read<CounterShard>().decrement(),
child: const Icon(Icons.remove),
),
const SizedBox(width: 16),
ElevatedButton(
onPressed: () => context.read<CounterShard>().reset(),
child: const Text('Reset'),
),
const SizedBox(width: 16),
ElevatedButton(
onPressed: () => context.read<CounterShard>().increment(),
child: const Icon(Icons.add),
),
],
),
],
),
),
);
}
}