connectivity_bloc

Creator: coderz1093

Last updated:

0 purchases

connectivity_bloc Image
connectivity_bloc Images

Languages

Categories

Add to Cart

Description:

connectivity bloc

Connectivity Bloc #
Connectivity Bloc allows to continuously check the connection state in an application.
Installation #

Add the latest version of package to your pubspec.yaml (and run dart pub get connectivity_bloc):

dependencies:
connectivity_bloc: ^0.0.1
copied to clipboard

Import the package and use it in your Flutter App.

import 'package:connectivity_bloc/connectivity_bloc.dart';
copied to clipboard
Usage #


Install flutter_bloc dependency with this command flutter pub add flutter_bloc


Create and provide the bloc to your widget using BlocProvider


I suggest to provide it from the main child then all his children will access to the bloc object
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:connectivity_bloc/connectivity_bloc.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => ConnectivityBloc(),
child: MaterialApp(
title: 'Connectivity Bloc Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Connectivity Bloc Demo'),
)
);
}
}
copied to clipboard

Using BlocBuilder to rebuild your widget when the states change.



class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);

final String title;

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

@override
Widget build(BuildContext context) {
return BlocBuilder<ConnectivityBloc, ConnectivityState>(
builder: (context, state) => Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
backgroundColor: Colors.white,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
(state is ConnectivityFailureState) ? 'No internet connexion' : 'Internet connexion is ok',
style: TextStyle(
color: (state is ConnectivityFailureState) ? Colors.red : Colors.green
),
),
],
),
),
)
);
}
}
copied to clipboard
Here are the different stats that can occur:

ConnectivityInitialState : this is the initial state
ConnectivitySuccessState : this is the state when the connexion is ok
ConnectivityFailureState : this is the state when there is no connexion

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.