multi_bloc_builder 0.0.1 multi_bloc_builder: ^0.0.1 copied to clipboard
VERY COOL MULTI BLOC BUILDER
import 'package:example/state/one/data_one_cubit.dart';
import 'package:example/state/two/data_two_cubit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:multi_bloc_builder/multi_bloc_builder.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(create: (context) => DataOneCubit()),
BlocProvider(create: (context) => DataTwoCubit()),
],
child: MaterialApp(
title: 'MultiBlocBuilder Demo',
home: const MyHomePage(),
)
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
_fetch() {
context.read<DataOneCubit>().fetch();
context.read<DataTwoCubit>().fetch();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Column(
children: [
MultiBlocBuilder(
blocs: [
context.watch<DataOneCubit>(),
context.watch<DataTwoCubit>(),
],
builder: (context, states) {
DataOneState one = states[0];
DataTwoState two = states[1];
return Column(
children: [
Text(one.toString()),
Text(two.toString()),
],
);
},
),
ElevatedButton(onPressed: _fetch, child: const Text('Fetch'))
],
)
);
}
}