Giter Site home page Giter Site logo

bloc_hooks's Introduction

Bloc Hooks

Nothing much, just some simple hooks which I use with flutter_bloc package.

Hooks

useBloc

useBloc looks for a Bloc in your widget tree, similar to context.read (it is actually just context.read under the hood).

Widget build(BuildContext context) {
  final bloc = useBloc<CounterBloc>();
  
  return TextButton(
    onPressed: () => bloc.add(CounterEvent.increment),
    child: Text('Increment'),
  );
}

useBlocBuilder

useBlocBuilder looks for a Bloc in your widget tree and rebuilds the widget when its state changes. If your state is quite complex and you don't want to unnecesary rebuild your widget, consider useBlocSelector

Widget build(BuildContext context) {
  final count = useBlocBuilder<CounterBloc, int>();
  
  return Text('$count');
}

You can pass buildWhen callback, which will decide if the widget needs to be rebuild based on the state.

Widget build(BuildContext context) {
  final count = useBlocBuilder<CounterBloc, int>(
    buildWhen: (previousCount, currentCount) => currentCount <= 10,
  );
  
  return Text('Will only count up to 10: $count');
}

useBlocListener

useBlocListener looks for a Bloc in your widget tree and calls listener on every state update.

Widget build(BuildContext context) {
  useBlocListener((CounterBloc _, int count) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('New counter value: $count')),
    );
  });
  
  return Text('Updated count will appear in a snackbar');
}

useBlocConsumer

useBlocConsumer combines useBlocBuilder and useBlocListener. It allows you to both listen to state changes and update your UI.

Widget build(BuildContext context) {
  final count = useBlocConsumer((CounterBloc _, int count) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('New counter value: $count')),
    );
  });
  
  return Text('Count: $count');
}

useBlocSelector

useBlocSelector can be useful when you have a some complex state, but you only want your widget to rebuild when certain properties change.

Widget build(BuildContext context) {
  final (isAuthenticated, user) = useBlocSelector(
    (AuthBloc _, AuthState state) => (state.isAuthenticated, state.user),
  );

  if (!isAuthenticated || user == null) {
    return LoginScreen();
  }

  return HomeScreen(user: user);
}

bloc_hooks's People

Contributors

evaqum avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.