reversible_bloc 0.0.2 reversible_bloc: ^0.0.2 copied to clipboard
A bloc mixin that enables to rollback states.
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:reversible_bloc_example/cubit/my_counter_cubit.dart';
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: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({Key? key}) : super(key: key);
final _counterCubit = MyCounterCubit();
void _incrementCounter() {
_counterCubit.increment();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Reversible Cubit'),
),
body: BlocProvider.value(
value: _counterCubit,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
BlocBuilder<MyCounterCubit, int>(
bloc: _counterCubit,
builder: (context, counter) {
return Text(
'$counter',
style: Theme.of(context).textTheme.headline4,
);
},
),
ElevatedButton(
child: const Text('Revert state'),
onPressed: _counterCubit.revert,
),
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}