Created
May 7, 2024 22:04
-
-
Save kelvinwieth/afc92fb9dc0104197ee98aecd40fcabd to your computer and use it in GitHub Desktop.
Multi bloc listener + configurator pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
void main() { | |
runApp( | |
ReceitasApp(), | |
); | |
} | |
class ReceitasApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: SobremesaConfigurator.mount(), | |
); | |
} | |
} | |
class PudimCubit extends Cubit<int> { | |
PudimCubit() : super(0); | |
void increment() => emit(state + 1); | |
} | |
class BoloCubit extends Cubit<int> { | |
BoloCubit() : super(0); | |
void increment() => emit(state + 1); | |
} | |
class SobremesaConfigurator { | |
static Widget mount() { | |
return MultiBlocProvider( | |
providers: [ | |
BlocProvider(create: (_) => PudimCubit()), | |
BlocProvider(create: (_) => BoloCubit()), | |
], | |
child: SobremesaScreen(), | |
); | |
} | |
} | |
class SobremesaScreen extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
BlocBuilder<PudimCubit, int>( | |
builder: (_, count) => Text('Pudins: ${count}'), | |
), | |
const SizedBox(width: 16), | |
BlocBuilder<BoloCubit, int>( | |
builder: (_, count) => Text('Bolos: ${count}'), | |
), | |
], | |
), | |
), | |
floatingActionButton: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
FloatingActionButton( | |
onPressed: () => context.read<PudimCubit>().increment(), | |
child: Text('P'), | |
), | |
const SizedBox(height: 4), | |
FloatingActionButton( | |
onPressed: () => context.read<BoloCubit>().increment(), | |
child: Text('B'), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment