Bloc (State Management) Check Connectivity Example Using Bloc

 abstract class InternetEvent {}


class InternetLostEvent extends InternetEvent {}

class InternetGainedEvent extends InternetEvent {}
abstract class InternetState {}

class InternetInitialState extends InternetState {}

class InternetLostState extends InternetState {}

class InternetGainedState extends InternetState {}
import "dart:async";
import "package:connectivity/connectivity.dart";
import "package:firebase/Stateless_Component_UI/Bloc/internet_bloc/internet_event.dart";
import "package:firebase/Stateless_Component_UI/Bloc/internet_bloc/internet_state.dart";
import "package:flutter_bloc/flutter_bloc.dart";
/// Difference between Cubit and Bloc is that Cubit is based on Function and
/// Bloc is based on Event
/// In short, Cubit is Function based and Bloc is Event based

class InternetBloc extends Bloc<InternetEvent, InternetState>
{
final Connectivity _connectivity = Connectivity();
StreamSubscription? connectivitySubscription;

InternetBloc() : super(InternetInitialState()) {

on<InternetLostEvent>((event, emit) {
return emit(InternetLostState());
},);

on<InternetGainedEvent>((event, emit) {
return emit(InternetGainedState());
},);

connectivitySubscription = _connectivity.onConnectivityChanged.listen((result) {
if(result == ConnectivityResult.mobile || result == ConnectivityResult.wifi)
{
add(InternetGainedEvent());
}
else
{
add(InternetLostEvent());
}
});
}

@override
Future<void> close()
{
connectivitySubscription?.cancel();
return super.close();
}
}

import "package:firebase/Stateless_Component_UI/Bloc/internet_bloc/internet_bloc.dart";
import "package:firebase/Stateless_Component_UI/Bloc/internet_bloc/internet_state.dart";
import "package:flutter/material.dart";
import "package:flutter_bloc/flutter_bloc.dart";

void main()
{
runApp(const MyApp());
}
class MyApp extends StatelessWidget
{
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) {
return InternetBloc();
},
child: MaterialApp(
title: "Bloc Check Connectivity",
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.grey,
),
home: const HomeScreen2(),
),
);
}
}
class HomeScreen2 extends StatelessWidget
{
const HomeScreen2({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Check Connectivity"),
centerTitle: true,
backgroundColor: Colors.grey,
),
body: Center(
/// BlocConsumer is the combination of background Listener and UI builder
/// BlocBuilder is provide only UI builder
/// BlocListener is provide only Listener in background
/// as shown below
child: BlocConsumer<InternetBloc, InternetState>(
listener: (context, state) {
if(state is InternetGainedState)
{
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Internet Connected!"),
backgroundColor: Colors.green,
),
);
}
else if(state is InternetLostState)
{
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Internet not Connected!"),
backgroundColor: Colors.red,
),
);
}
},
builder: (context, state) {
if(state is InternetGainedState)
{
return const Text("Connected!");
}
else if(state is InternetLostState)
{
return const Text("Not Connected!");
}
else
{
return const Text("Loading...");
}
},
),
),
);
}
}

Comments

Popular posts from this blog

Second GET API Calling with Bloc simple Example in Flutter

Stack Container Scrollable Card widget UI with Custom Widget

Pagination with Bloc Pattern in Flutter