bloc_effects 2.0.0 copy "bloc_effects: ^2.0.0" to clipboard
bloc_effects: ^2.0.0 copied to clipboard

Cubit and Bloc abstractions and Flutter Widget that make it easy to add UI Effects to the BLoC state management.

example/lib/main.dart

import 'package:bloc_effects/bloc_effects.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

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

abstract class DemoEffect {}

class ShowBottomSheet implements DemoEffect {
  const ShowBottomSheet();
}

class DemoCubit extends Cubit<void> with Effects<DemoEffect> {
  DemoCubit() : super(null);

  void onButtonPressed() => emitEffect(const ShowBottomSheet());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late DemoCubit _demoCubit;

  @override
  void initState() {
    super.initState();
    _demoCubit = DemoCubit();
  }

  @override
  void dispose() {
    _demoCubit.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: BlocEffectListener<DemoCubit, DemoEffect>(
          effector: _demoCubit,
          listener: (context, effect) {
            if (effect is ShowBottomSheet) {
              showBottomSheet(
                context: context,
                builder: (c) => Material(
                  child: Container(
                    color: Colors.black12,
                    height: 150,
                  ),
                ),
              );
            }
          },
          child: Center(
            child: ElevatedButton(
              onPressed: _demoCubit.onButtonPressed,
              child: const Icon(Icons.upload),
            ),
          ),
        ),
      ),
    );
  }
}
18
likes
160
points
1.96k
downloads

Publisher

verified publisherdarttech.dev

Weekly Downloads

Cubit and Bloc abstractions and Flutter Widget that make it easy to add UI Effects to the BLoC state management.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, flutter_bloc

More

Packages that depend on bloc_effects