-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathconnection_app.dart
92 lines (84 loc) · 2.65 KB
/
connection_app.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:example/src/list_details_app.dart';
import 'package:example/src/post/model/post_details.dart';
import 'package:example/src/tools/connection/connectivity_plus_repository.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_bloc_patterns/connection.dart';
void main() => runApp(ConnectionApp());
class ConnectionApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Connection App',
theme: ThemeData(primarySwatch: Colors.green),
home: BlocProvider(
create: (_) => ConnectionBloc(
ConnectivityPlusRepository(
Connectivity(),
),
),
child: const ConnectionPage(),
),
);
}
}
class ConnectionPage extends StatelessWidget {
const ConnectionPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Post')),
body: ConnectionListener(
onOnline: _showOnlineSnackbar,
onOffline: _showOfflineSnackbar,
child: ConnectionBuilder(
online: (context) => PostDetailsView(
PostDetails(
id: 1,
title: 'Lorem Ipsum',
body:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
),
),
offline: (context) => const _OfflineView(),
),
),
);
}
void _showOnlineSnackbar(BuildContext context) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: const Text('You are back online.'),
backgroundColor: Colors.green[300],
),
);
}
void _showOfflineSnackbar(BuildContext context) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: const Text('You are offline.'),
backgroundColor: Colors.red[300],
),
);
}
}
class _OfflineView extends StatelessWidget {
const _OfflineView();
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.signal_wifi_0_bar_rounded, size: 64),
const SizedBox(height: 16),
Text(
'You are offline. Check your Internet connection.',
style: Theme.of(context).textTheme.bodyMedium,
),
],
),
);
}
}