bloc 0.3.0 copy "bloc: ^0.3.0" to clipboard
bloc: ^0.3.0 copied to clipboard

outdated

The goal of this package is to make it easy to implement the BLoC Design Pattern (Business Logic Component).

example/lib/main.dart

import 'dart:async';

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

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  final CounterBloc _counterBloc = CounterBloc();

  @override
  Widget build(BuildContext context) {
    return BlocBuilder(
      bloc: _counterBloc,
      builder: ((
        BuildContext context,
        int count,
      ) {
        return MaterialApp(
          title: 'Flutter Demo',
          home: Scaffold(
            appBar: AppBar(title: Text('Counter')),
            body: Center(
              child: Text(
                '$count',
                style: TextStyle(fontSize: 24.0),
              ),
            ),
            floatingActionButton: Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              mainAxisAlignment: MainAxisAlignment.end,
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.symmetric(vertical: 5.0),
                  child: FloatingActionButton(
                    child: Icon(Icons.add),
                    onPressed: _counterBloc.increment,
                  ),
                ),
                Padding(
                  padding: EdgeInsets.symmetric(vertical: 5.0),
                  child: FloatingActionButton(
                    child: Icon(Icons.remove),
                    onPressed: _counterBloc.decrement,
                  ),
                ),
              ],
            ),
          ),
        );
      }),
    );
  }
}

abstract class CounterEvent {}

class IncrementCounter extends CounterEvent {}

class DecrementCounter extends CounterEvent {}

class CounterBloc extends Bloc<CounterEvent, int> {
  int get initialState => 0;

  void increment() {
    dispatch(IncrementCounter());
  }

  void decrement() {
    dispatch(DecrementCounter());
  }

  @override
  Stream<int> mapEventToState(int state, CounterEvent event) async* {
    if (event is IncrementCounter) {
      yield state + 1;
    }
    if (event is DecrementCounter) {
      yield state - 1;
    }
  }
}
2898
likes
0
pub points
99%
popularity

Publisher

verified publisherbloclibrary.dev

The goal of this package is to make it easy to implement the BLoC Design Pattern (Business Logic Component).

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, rxdart

More

Packages that depend on bloc