isolate_bloc 0.0.1 copy "isolate_bloc: ^0.0.1" to clipboard
isolate_bloc: ^0.0.1 copied to clipboard

outdated

A new Flutter package project.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:isolate_bloc/isolate_bloc.dart';

void main(List<String> arguments) async {
  await initialize(isolatedFunc);
  IsolateBloc.observer = SimpleBlocObserver();
  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 {
  register(create: () => CounterBloc());
}

class CounterBloc extends IsolateBloc<CountEvent, int> {
  var _counter = 0;

  CounterBloc() : super(0);

  @override
  void onEventReceived(CountEvent event) {
    event == CountEvent.increment ? _counter++ : _counter--;
    emit(_counter);
  }
}

enum CountEvent {
  increment,
  decrement,
}

class SimpleBlocObserver extends IsolateBlocObserver {
  void onEvent(IsolateBlocWrapper bloc, Object event) {
    print("New event: $event from bloc: $bloc");
    super.onEvent(bloc, event);
  }

  void onTransition(IsolateBlocWrapper bloc, Transition transition) {
    print("New state ${transition.nextState} from $bloc");
    super.onTransition(bloc, transition);
  }

  void onError(IsolateBlocWrapper bloc, Object error, [StackTrace stackTrace]) {
    print("Error: $error in $bloc");
    super.onError(bloc, error, stackTrace);
  }
}
44
likes
0
pub points
68%
popularity

Publisher

unverified uploader

A new Flutter package project.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, nested, provider, uuid

More

Packages that depend on isolate_bloc