Skip to content

Commit

Permalink
Merge pull request #3 from NaagAlgates/1-unhandled-exception-bad-stat…
Browse files Browse the repository at this point in the history
…e-cannot-emit-new-states-after-calling-close

Resolved the bad state issue when navigating to the second screen
  • Loading branch information
NaagAlgates authored Aug 3, 2022
2 parents 75c830a + c09dafa commit e69bd11
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 99 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 0.0.5+11
# 0.0.6+13

* Added callback method when online
* Added callback method when offline
* Updated readme
* Added Offline screen on top of all widgets
* Use the widget only once for the entire app life cycle
* Updated default lookup url to github.com
72 changes: 44 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,20 @@ class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
return InternetWidget(
offline: const Center(child: Text('No Internet')),
// ignore: avoid_print
whenOffline: () => print('No Internet'),
// ignore: avoid_print
whenOnline: () => print('Connected to internet'),
loadingWidget: const Center(child: Text('Loading')),
online: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
Expand All @@ -72,31 +80,39 @@ class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Center(
child: InternetWidget(
online: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
child: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const SecondScreen(),
),
);
},
child: const Text('Navigate'),
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
),
);
}
Expand Down
78 changes: 44 additions & 34 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_no_internet_widget/flutter_no_internet_widget.dart';
import 'package:no_internet_widget_example/second_screen.dart';

void main() {
runApp(const MyApp());
Expand All @@ -11,12 +12,20 @@ class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
return InternetWidget(
offline: const Center(child: Text('No Internet')),
// ignore: avoid_print
whenOffline: () => print('No Internet'),
// ignore: avoid_print
whenOnline: () => print('Connected to internet'),
loadingWidget: const Center(child: Text('Loading')),
online: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
Expand All @@ -41,38 +50,39 @@ class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Center(
child: InternetWidget(
offline: const Scaffold(
body: Center(child: Text('No Internet')),
child: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
// ignore: avoid_print
whenOffline: () => print('No Internet'),
online: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const SecondScreen(),
),
);
},
child: const Text('Navigate'),
)
],
),
),
// ignore: avoid_print
whenOnline: () => print('Connected to internet'),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
),
);
}
Expand Down
15 changes: 15 additions & 0 deletions example/lib/second_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:flutter/material.dart';

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

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Second Screen'),
),
body: const Center(child: Text('Second Screen')),
);
}
}
4 changes: 2 additions & 2 deletions lib/src/_cubit/internet_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class InternetCubit extends Cubit<InternetState> {
///Internet State cubit
InternetCubit({
Connectivity? connectivity,
this.urlLookup = 'example.com',
this.urlLookup = 'github.com',
}) : super(const InternetState()) {
this.connectivity = connectivity ?? Connectivity();
_init();
Expand Down Expand Up @@ -142,7 +142,7 @@ class InternetCubit extends Cubit<InternetState> {
}

Future<List<InternetAddress>> _lookupAddress() async {
final _lookupUrl = urlLookup ?? 'example.com';
final _lookupUrl = urlLookup ?? 'github.com';
final _internetAddress = await InternetAddress.lookup(
_lookupUrl,
);
Expand Down
62 changes: 32 additions & 30 deletions lib/src/flutter_no_internet_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,39 +68,41 @@ class InternetWidget extends StatelessWidget {

@override
Widget build(BuildContext context) {
return ResponsiveSizer(
builder: (context, orientation, screenType) {
return BlocProvider<InternetCubit>(
create: (context) => InternetCubit(
urlLookup: lookupUrl,
),
child: Scaffold(
body: Builder(
builder: (_) {
return BlocBuilder<InternetCubit, InternetState>(
builder: (_, state) {
if (state.cubitStatus == CubitStatus.busy) {
if (loadingWidget != null) {
return loadingWidget!;
return MaterialApp(
home: ResponsiveSizer(
builder: (context, orientation, screenType) {
return BlocProvider<InternetCubit>(
create: (context) => InternetCubit(
urlLookup: lookupUrl,
),
child: Scaffold(
body: Builder(
builder: (_) {
return BlocBuilder<InternetCubit, InternetState>(
builder: (_, state) {
if (state.cubitStatus == CubitStatus.busy) {
if (loadingWidget != null) {
return loadingWidget!;
}
return const Center(
child: CircularProgressIndicator(),
);
}
return const Center(
child: CircularProgressIndicator(),
return SizedBox(
width: width ?? 100.0.w,
height: height ?? 100.0.h,
child: state.internetStatus == InternetStatus.connected
? _getOnlineWidget()
: _getOfflineWidget(),
);
}
return SizedBox(
width: width ?? 100.0.w,
height: height ?? 100.0.h,
child: state.internetStatus == InternetStatus.connected
? _getOnlineWidget()
: _getOfflineWidget(),
);
},
);
},
},
);
},
),
),
),
);
},
);
},
),
);
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: flutter_no_internet_widget
description: A new Flutter widget to show online or offline widget without any
extra code or dependencies.
version: 0.0.5+11
version: 0.0.6+13
homepage: https://github.com/NaagAlgates/flutter_no_internet_widget

environment:
Expand Down

0 comments on commit e69bd11

Please sign in to comment.