|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:http/http.dart' as http; //for making request |
| 3 | +import 'dart:async'; //for asynchronous features |
| 4 | +import 'dart:convert'; //for converting the response to desired format. e.g: JSON |
| 5 | +import 'package:connectivity/connectivity.dart'; //connectivity package...also see the pubspec.yaml |
| 6 | +import 'package:flutter/services.dart'; //PlatForm Exception |
| 7 | + |
| 8 | +void main() { |
| 9 | + runApp(new MyApp()); |
| 10 | +} |
| 11 | + |
| 12 | +class MyApp extends StatelessWidget { |
| 13 | + @override |
| 14 | + Widget build(BuildContext context) { |
| 15 | + return new MaterialApp(home: new ButtonOptions()); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +class ButtonOptions extends StatefulWidget { |
| 20 | + @override |
| 21 | + State<StatefulWidget> createState() { |
| 22 | + return new ButtonOptionsState(); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +class ButtonOptionsState extends State<ButtonOptions> { |
| 27 | + String _connectionStatus; |
| 28 | + final Connectivity _connectivity = new Connectivity(); |
| 29 | + |
| 30 | + //For subscription to the ConnectivityResult stream |
| 31 | + StreamSubscription<ConnectivityResult> _connectionSubscription; |
| 32 | + |
| 33 | + /* |
| 34 | + ConnectivityResult is an enum with the values as { wifi, mobile, none }. |
| 35 | + */ |
| 36 | + @override |
| 37 | + void initState() { |
| 38 | + super.initState(); |
| 39 | + // initConnectivity(); before calling on button press |
| 40 | + _connectionSubscription = |
| 41 | + _connectivity.onConnectivityChanged.listen((ConnectivityResult result) { |
| 42 | + setState(() { |
| 43 | + _connectionStatus = result.toString(); |
| 44 | + }); |
| 45 | + }); |
| 46 | + print("Initstate : $_connectionStatus"); |
| 47 | + } |
| 48 | + |
| 49 | + //For cancelling the stream subscription...Good way to release resources |
| 50 | + @override |
| 51 | + void dispose() { |
| 52 | + _connectionSubscription.cancel(); |
| 53 | + super.dispose(); |
| 54 | + } |
| 55 | + |
| 56 | + //called in initState |
| 57 | + /* |
| 58 | + _connectivity.checkConnectivity() checks the connection state of the device. |
| 59 | + Recommended way is to use onConnectivityChanged stream for listening to connectivity changes. |
| 60 | + It is done in initState function. |
| 61 | + */ |
| 62 | + Future<Null> initConnectivity() async { |
| 63 | + String connectionStatus; |
| 64 | + |
| 65 | + try { |
| 66 | + connectionStatus = (await _connectivity.checkConnectivity()).toString(); |
| 67 | + } on PlatformException catch (e) { |
| 68 | + print(e.toString()); |
| 69 | + connectionStatus = "Internet connectivity failed"; |
| 70 | + } |
| 71 | + |
| 72 | + if (!mounted) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + setState(() { |
| 77 | + _connectionStatus = connectionStatus; |
| 78 | + }); |
| 79 | + print("InitConnectivity : $_connectionStatus"); |
| 80 | + if(_connectionStatus == "ConnectivityResult.mobile" || _connectionStatus == "ConnectivityResult.wifi") { |
| 81 | + getData(); |
| 82 | + } else { |
| 83 | + print("You are not connected to internet"); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + //makes the request |
| 88 | + Future<String> getData() async { |
| 89 | + http.Response response = await http.get( |
| 90 | + Uri.encodeFull("https://jsonplaceholder.typicode.com/posts"), |
| 91 | + headers: {"Accept": "application/json"}); |
| 92 | + List data = JSON.decode(response.body); |
| 93 | + print(data[1]); |
| 94 | + } |
| 95 | + |
| 96 | + final TextEditingController _controller = new TextEditingController(); |
| 97 | + String str = ""; |
| 98 | + String submitStr = ""; |
| 99 | + |
| 100 | + void _changeText(String val) { |
| 101 | + setState(() { |
| 102 | + submitStr = val; |
| 103 | + }); |
| 104 | + print("On RaisedButton : The text is $submitStr"); |
| 105 | + } |
| 106 | + |
| 107 | + void _onSubmit(String val) { |
| 108 | + print("OnSubmit : The text is $val"); |
| 109 | + setState(() { |
| 110 | + submitStr = val; |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + @override |
| 115 | + Widget build(BuildContext context) { |
| 116 | + void _onChanged(String value) { |
| 117 | + print('"OnChange : " $value'); |
| 118 | + } |
| 119 | + |
| 120 | + return new Scaffold( |
| 121 | + appBar: new AppBar( |
| 122 | + title: new Text('First Screen'), |
| 123 | + ), |
| 124 | + body: new Container( |
| 125 | + padding: const EdgeInsets.all(10.0), |
| 126 | + child: new Column( |
| 127 | + children: <Widget>[ |
| 128 | + new TextField( |
| 129 | + decoration: new InputDecoration( |
| 130 | + hintText: "Type something...", |
| 131 | + ), |
| 132 | + onChanged: (String value) { |
| 133 | + _onChanged(value); |
| 134 | + }, |
| 135 | + controller: _controller, |
| 136 | + onSubmitted: (String submittedStr) { |
| 137 | + _onSubmit(submittedStr); |
| 138 | + _controller.text = ""; |
| 139 | + }, |
| 140 | + ), |
| 141 | + new Text('$submitStr'), |
| 142 | + new RaisedButton( |
| 143 | + child: new Text("Click me"), |
| 144 | + onPressed: () { |
| 145 | + //_changeText(_controller.text); |
| 146 | + //getData(); |
| 147 | + initConnectivity(); |
| 148 | + // countT(); |
| 149 | + _controller.text = _connectionStatus; |
| 150 | + }, |
| 151 | + ) |
| 152 | + ], |
| 153 | + ), |
| 154 | + ), |
| 155 | + ); |
| 156 | + } |
| 157 | +} |
0 commit comments