Bloc (State Management) Counter App

import "package:flutter/material.dart";
import "package:flutter_bloc/flutter_bloc.dart";

void main()
{
runApp(const MyApp());
}
class CounterBloc extends Cubit<int>
{
CounterBloc() : super(0);

@override
void increment()
{
return emit(state + 1);
}

@override
void decrement()
{
return emit(state - 1);
}
}
class MyApp extends StatelessWidget
{
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Bloc Counter App",
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.grey,
),
home: BlocProvider(
create: (context) {
return CounterBloc();
},
child: const CounterPage2(),
),
);
}
}
class CounterPage2 extends StatelessWidget
{
const CounterPage2({super.key});
@override
Widget build(BuildContext context) {
final CounterBloc counterBloc = BlocProvider.of<CounterBloc>(context, listen: false);

return Scaffold(
appBar: AppBar(
title: const Text("Bloc State Management"),
centerTitle: true,
backgroundColor: Colors.grey,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Counter Value : ", style: Theme.of(context).textTheme.headlineLarge),
BlocBuilder<CounterBloc, int>(
builder: (context, state) {
return Text("$state", style: Theme.of(context).textTheme.headlineLarge);
},
),
const SizedBox(height: 50.0),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return BlocProvider(
create: (context) {
return CounterBloc();
},
child: CounterPage3(title: "${counterBloc.state}"),
);
},
),
);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
fixedSize: const Size(350.0,50.0),
),
child: const Text("Go to Next Page",
style: TextStyle(color: Colors.white, fontSize: 20.0),
),
),
],
),
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
onPressed: () {
counterBloc.increment();
},
heroTag: null,
backgroundColor: Colors.blue,
child: const Icon(Icons.add, color: Colors.white),
),
const SizedBox(width: 20.0),
FloatingActionButton(
onPressed: () {
counterBloc.decrement();
},
heroTag: null,
backgroundColor: Colors.blue,
child: const Icon(Icons.remove, color: Colors.white),
),
],
),
);
}
}
class CounterPage3 extends StatelessWidget
{
const CounterPage3({super.key, required this.title});
final String title;

@override
Widget build(BuildContext context) {

return Scaffold(
appBar: AppBar(
title: const Text("Second Page"),
centerTitle: true,
backgroundColor: Colors.grey,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("This is the value of Counter Below : ",
style: Theme.of(context).textTheme.headlineLarge),
const SizedBox(height: 50.0),
BlocBuilder<CounterBloc, int>(
builder: (context, state) {
return Text(title, style: Theme.of(context).textTheme.headlineLarge);
},
),
],
),
),
);
}
}

Comments

Popular posts from this blog

Pagination with Bloc Pattern in Flutter

ExpansionPanel with ExpansionPanelList with Complete Collapse Operation in Flutter

Pagination First Practical in Flutter