isolate_bloc 0.1.0 isolate_bloc: ^0.1.0 copied to clipboard
A dart package that helps implement the BLoC pattern. BLoC works in Isolate and don't slow down UI.
import 'package:flutter/material.dart';
import 'package:isolate_bloc/isolate_bloc.dart';
void main(List<String> arguments) async {
await initialize(isolatedFunc);
runApp(
MaterialApp(
home: IsolateBlocProvider<CounterBloc, int>(
child: CounterScreen(),
),
),
);
}
class CounterScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Counter'),
),
body: Center(
child: IsolateBlocListener<CounterBloc, int>(
listener: (context, state) => print("New bloc state: $state"),
child: IsolateBlocBuilder<CounterBloc, int>(
builder: (context, state) {
return Text('You tapped $state times');
},
),
),
),
floatingActionButton: Column(
mainAxisSize: MainAxisSize.min,
children: [
FloatingActionButton(
heroTag: 'Increment',
onPressed: () =>
context.isolateBloc<CounterBloc>().add(CountEvent.increment),
child: Icon(Icons.add),
),
SizedBox(height: 10),
FloatingActionButton(
heroTag: 'Decrement',
onPressed: () =>
context.isolateBloc<CounterBloc>().add(CountEvent.decrement),
child: Icon(Icons.remove),
),
],
),
);
}
}
Future<void> isolatedFunc() async {
IsolateBloc.observer = SimpleBlocObserver();
register(create: () => CounterBloc());
}
class CounterBloc extends IsolateBloc<CountEvent, int> {
CounterBloc() : super(0);
@override
void onEventReceived(CountEvent event) {
emit(event == CountEvent.increment ? state + 1 : state - 1);
}
}
enum CountEvent {
increment,
decrement,
}
class SimpleBlocObserver extends IsolateBlocObserver {
void onEvent(IsolateBloc bloc, Object event) {
print("New $event for $bloc");
super.onEvent(bloc, event);
}
void onTransition(IsolateBloc bloc, Transition transition) {
print("New ${transition.nextState} from $bloc");
super.onTransition(bloc, transition);
}
void onError(IsolateBloc bloc, Object error, StackTrace stackTrace) {
print("$error in $bloc");
super.onError(bloc, error, stackTrace);
}
}