Bloc Pattern in Flutter | Part 1💙

Do you feel Bloc is tough? Me too!

Let’s make it easy then!😍

Let’s start with the coding!

flutter_bloc: ^6.1.1
import 'package:bloc_counter_example/home.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
import 'package:flutter/material.dart';class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int counter = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => setState(() {
counter++;
}),
),
appBar: AppBar(
title: Text('Bloc Counter Example'),
),
body: Center(
child: Text(
'$counter',
style: TextStyle(fontSize: 50.0),
),
),
);
}
}
enum CounterEvent { increment }
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'counter_event.dart';
class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0);
@override
Stream<int> mapEventToState(CounterEvent event) async* {
switch (event) {
case CounterEvent.increment:
yield state + 1;
break;
default:
break;
}
}
}
switch (event) {
case CounterEvent.increment:
yield state + 1;
break;
default:
break;
}
import 'package:bloc_counter_example/bloc/counter_bloc.dart';
import 'package:bloc_counter_example/home.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: BlocProvider<CounterBloc>(
create: (context) => CounterBloc(),
child: MyHomePage(),
),
);
}
}
BlocProvider<CounterBloc>(
create: (context) => CounterBloc(),
child: MyHomePage(),
),
CounterBloc _counterBloc;
_counterBloc = BlocProvider.of<CounterBloc>(context);
body: Center(
child: BlocBuilder<CounterBloc, int>(
builder: (context, state) {
return Text(
'$state',
style: TextStyle(fontSize: 50.0),
);
},
),
),
onPressed: () => _counterBloc.add(CounterEvent.increment),
import 'package:bloc_counter_example/bloc/counter_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'bloc/counter_event.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
CounterBloc _counterBloc;
@override
void dispose() {
_counterBloc.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
_counterBloc = BlocProvider.of<CounterBloc>(context);
return Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => _counterBloc.add(CounterEvent.increment),
),
appBar: AppBar(
title: Text('Bloc Counter Example'),
),
body: Center(
child: BlocBuilder<CounterBloc, int>(
builder: (context, state) {
return Text(
'$state',
style: TextStyle(fontSize: 50.0),
);
},
),
),
);
}
}

Don’t forget to connect with me on:

Don’t stop, until you are breathing!💙
- Abhishek Doshi

Flutter Developer