forked from AseemWangoo/flutter_programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InternetConnectivity.dart
157 lines (140 loc) · 4.48 KB
/
InternetConnectivity.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http; //for making request
import 'dart:async'; //for asynchronous features
import 'dart:convert'; //for converting the response to desired format. e.g: JSON
import 'package:connectivity/connectivity.dart'; //connectivity package...also see the pubspec.yaml
import 'package:flutter/services.dart'; //PlatForm Exception
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(home: new ButtonOptions());
}
}
class ButtonOptions extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new ButtonOptionsState();
}
}
class ButtonOptionsState extends State<ButtonOptions> {
String _connectionStatus;
final Connectivity _connectivity = new Connectivity();
//For subscription to the ConnectivityResult stream
StreamSubscription<ConnectivityResult> _connectionSubscription;
/*
ConnectivityResult is an enum with the values as { wifi, mobile, none }.
*/
@override
void initState() {
super.initState();
// initConnectivity(); before calling on button press
_connectionSubscription =
_connectivity.onConnectivityChanged.listen((ConnectivityResult result) {
setState(() {
_connectionStatus = result.toString();
});
});
print("Initstate : $_connectionStatus");
}
//For cancelling the stream subscription...Good way to release resources
@override
void dispose() {
_connectionSubscription.cancel();
super.dispose();
}
//called in initState
/*
_connectivity.checkConnectivity() checks the connection state of the device.
Recommended way is to use onConnectivityChanged stream for listening to connectivity changes.
It is done in initState function.
*/
Future<Null> initConnectivity() async {
String connectionStatus;
try {
connectionStatus = (await _connectivity.checkConnectivity()).toString();
} on PlatformException catch (e) {
print(e.toString());
connectionStatus = "Internet connectivity failed";
}
if (!mounted) {
return;
}
setState(() {
_connectionStatus = connectionStatus;
});
print("InitConnectivity : $_connectionStatus");
if(_connectionStatus == "ConnectivityResult.mobile" || _connectionStatus == "ConnectivityResult.wifi") {
getData();
} else {
print("You are not connected to internet");
}
}
//makes the request
Future<String> getData() async {
http.Response response = await http.get(
Uri.encodeFull("https://jsonplaceholder.typicode.com/posts"),
headers: {"Accept": "application/json"});
List data = JSON.decode(response.body);
print(data[1]);
}
final TextEditingController _controller = new TextEditingController();
String str = "";
String submitStr = "";
void _changeText(String val) {
setState(() {
submitStr = val;
});
print("On RaisedButton : The text is $submitStr");
}
void _onSubmit(String val) {
print("OnSubmit : The text is $val");
setState(() {
submitStr = val;
});
}
@override
Widget build(BuildContext context) {
void _onChanged(String value) {
print('"OnChange : " $value');
}
return new Scaffold(
appBar: new AppBar(
title: new Text('First Screen'),
),
body: new Container(
padding: const EdgeInsets.all(10.0),
child: new Column(
children: <Widget>[
new TextField(
decoration: new InputDecoration(
hintText: "Type something...",
),
onChanged: (String value) {
_onChanged(value);
},
controller: _controller,
onSubmitted: (String submittedStr) {
_onSubmit(submittedStr);
_controller.text = "";
},
),
new Text('$submitStr'),
new RaisedButton(
child: new Text("Click me"),
onPressed: () {
//_changeText(_controller.text);
//getData();
initConnectivity();
// countT();
_controller.text = _connectionStatus;
},
)
],
),
),
);
}
}