flutter_hooks_bloc 0.17.0 copy "flutter_hooks_bloc: ^0.17.0" to clipboard
flutter_hooks_bloc: ^0.17.0 copied to clipboard

A flutter_bloc reimplementation based on flutter_hooks with the same API.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_hooks_bloc/flutter_hooks_bloc.dart';

void main() {
  runApp(MyApp());
}

class CounterCubit extends Cubit<int> {
  CounterCubit(int state) : super(state);

  void increment() => emit(state + 1);

  void decrement() => emit(state - 1);
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider<CounterCubit>(
      create: (_) => CounterCubit(0),
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        home: MyHomePage(title: 'BlocHook Example'),
      ),
    );
  }
}

class MyHomePage extends HookWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) {
    print('MyHomePage');
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('You have pushed the button this many times:'),
            HookBuilder(builder: (ctx) {
              print('HookBuilder');
              final counter = useBloc<CounterCubit, int>(
                onEmitted: (_, prev, curr) {
                  print('listener: $prev $curr');
                  return curr % 3 == 0; // allows rebuild
                },
              );
              return Text(
                '$counter',
                style: Theme.of(context).textTheme.headlineMedium,
              );
            }),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => context.read<CounterCubit>().increment(),
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
34
likes
160
pub points
52%
popularity

Publisher

unverified uploader

A flutter_bloc reimplementation based on flutter_hooks with the same API.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

bloc, flutter, flutter_bloc, flutter_hooks

More

Packages that depend on flutter_hooks_bloc