bloc_provider_package 0.0.1+1 bloc_provider_package: ^0.0.1+1 copied to clipboard
A package for managing state using BlocProvider and MultiBlocProvider.
import 'package:bloc_provider_package/bloc_provider_package.dart';
import 'package:example/counter_bloc.dart';
import 'package:example/counter_bloc2.dart';
import 'package:flutter/material.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 MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => CounterBloc(),
),
BlocProvider(
create: (context) => CounterBloc2(),
),
],
child: MyHomePage(title: 'Flutter Demo Home Page'),
));
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
// Title text
const Text(
'You have pushed the button this many times:',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 20),
// First Counter StreamBuilder with Card style
Card(
elevation: 5,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: StreamBuilder(
stream: context.read<CounterBloc>().counterStream,
builder: (context, snapshot) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'Counter 1',
),
Text(
'${snapshot.data ?? 0}',
),
],
);
},
),
),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: context.read<CounterBloc>().incrementCounter,
style: ElevatedButton.styleFrom(
padding:
const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: const Text('Increment Counter 1',
style: TextStyle(fontSize: 18)),
),
const SizedBox(height: 20),
// Second Counter StreamBuilder with Card style
Card(
elevation: 5,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: StreamBuilder(
stream: context.read<CounterBloc2>().counterStream,
builder: (context, snapshot) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'Counter 2',
),
Text(
'${snapshot.data ?? 0}',
),
],
);
},
),
),
),
const SizedBox(height: 20),
// Decrement Button
ElevatedButton(
onPressed: context.read<CounterBloc2>().decrementCounter,
style: ElevatedButton.styleFrom(
padding:
const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: const Text('Decrement Counter 2',
style: TextStyle(fontSize: 18)),
),
],
),
),
),
);
}
}