san-smith / blocify

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

blocify

A library that makes using the BLoC (Business Logic Component) design pattern easier.

Usage

Full example can be found here

DataBloc

import 'package:blocify/blocify.dart';

...


// Create data bloc
DataBloc<bool> _switchBloc = DataBloc(true);


// Use data bloc
Widget _getSwitch() {
  return BlocBuilder<DataBloc<bool>, DataState<bool>>(
    bloc: _switchBloc,
    builder: (context, state) {
      return Switch(
        value: state.data,
        // Update data in state
        onChanged: (value) => _switchBloc.add(
          DataSetEvent(value),
        ),
      );
    },
  );
}

SingleAsyncOpBloc

import 'package:blocify/blocify.dart';

...


// Create bloc
SingleAsyncOpBloc<User> _bloc = SingleAsyncOpBloc();

void _findUser() {
  if (_controller.text.isNotEmpty) {
    FocusScope.of(context).unfocus();
    // Send the event with an async request
    _bloc.add(
        SingleAsyncOpRequestEvent(_repository.getUser(_controller.text)));
  }
}

// Use bloc
Widget _getContent() {
  return BlocBuilder<SingleAsyncOpBloc<User>, SingleAsyncOpState<User>>(
    bloc: _bloc,
    builder: (context, state) {
      // Use state for updating UI
      if (state is SingleAsyncOpLoadingState) {
        return Center(child: CircularProgressIndicator());
      }
      if (state is SingleAsyncOpReadyState<User>) {
        return SingleChildScrollView(
          padding: EdgeInsets.only(bottom: 20),
          child: _getUserInfo(state.data),
        );
      }
      if (state is SingleAsyncOpErrorState) {
        return Center(
          child: Text('''Can't find user "${_controller.text}"'''),
        );
      }
      return Container();
    },
  );
}

About

License:MIT License


Languages

Language:Dart 93.8%Language:Kotlin 3.0%Language:Swift 3.0%Language:Objective-C 0.3%