|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter/services.dart'; |
| 5 | +import 'package:google_api_availability_android/google_api_availability_android.dart'; |
| 6 | +import 'package:google_api_availability_platform_interface/google_api_availability_platform_interface.dart'; |
| 7 | + |
| 8 | +void main() => runApp(const MyApp()); |
| 9 | + |
| 10 | +///Creates the mutable state for this widget. |
| 11 | +class MyApp extends StatefulWidget { |
| 12 | + /// Named [key] parameter to identify a widget. |
| 13 | + const MyApp({Key? key}) : super(key: key); |
| 14 | + |
| 15 | + @override |
| 16 | + // ignore: library_private_types_in_public_api |
| 17 | + _MyAppState createState() => _MyAppState(); |
| 18 | +} |
| 19 | + |
| 20 | +class _MyAppState extends State<MyApp> { |
| 21 | + GooglePlayServicesAvailability _playStoreAvailability = |
| 22 | + GooglePlayServicesAvailability.unknown; |
| 23 | + String _errorString = 'unknown'; |
| 24 | + bool _isUserResolvable = false; |
| 25 | + bool _errorDialogFragmentShown = false; |
| 26 | + |
| 27 | + // Platform messages are asynchronous, so we initialize in an async method. |
| 28 | + Future<void> checkPlayServices([bool showDialog = false]) async { |
| 29 | + GooglePlayServicesAvailability playStoreAvailability; |
| 30 | + // Platform messages may fail, so we use a try/catch PlatformException. |
| 31 | + try { |
| 32 | + playStoreAvailability = await GoogleApiAvailabilityAndroid() |
| 33 | + .checkGooglePlayServicesAvailability(showDialog); |
| 34 | + } on PlatformException { |
| 35 | + playStoreAvailability = GooglePlayServicesAvailability.unknown; |
| 36 | + } |
| 37 | + |
| 38 | + // If the widget was removed from the tree while the asynchronous platform |
| 39 | + // message was in flight, we want to discard the reply rather than calling |
| 40 | + // setState to update our non-existent appearance. |
| 41 | + if (!mounted) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + setState(() { |
| 46 | + _playStoreAvailability = playStoreAvailability; |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + Future<void> makeGooglePlayServicesAvailable() async { |
| 51 | + try { |
| 52 | + await GoogleApiAvailabilityAndroid().makeGooglePlayServicesAvailable(); |
| 53 | + } on PlatformException { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (!mounted) { |
| 58 | + return; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + Future<void> getErrorString() async { |
| 63 | + String errorString; |
| 64 | + |
| 65 | + try { |
| 66 | + errorString = await GoogleApiAvailabilityAndroid().getErrorString(); |
| 67 | + } on PlatformException { |
| 68 | + errorString = 'Not available on non Android devices'; |
| 69 | + } |
| 70 | + |
| 71 | + if (!mounted) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + setState(() { |
| 76 | + _errorString = errorString; |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + Future<void> isUserResolvable() async { |
| 81 | + bool isUserResolvable; |
| 82 | + |
| 83 | + try { |
| 84 | + isUserResolvable = |
| 85 | + await GoogleApiAvailabilityAndroid().isUserResolvable(); |
| 86 | + } on PlatformException { |
| 87 | + isUserResolvable = false; |
| 88 | + } |
| 89 | + |
| 90 | + if (!mounted) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + setState(() { |
| 95 | + _isUserResolvable = isUserResolvable; |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + Future<void> showErrorNotification() async { |
| 100 | + try { |
| 101 | + await GoogleApiAvailabilityAndroid().showErrorNotification(); |
| 102 | + } on PlatformException { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + if (!mounted) { |
| 107 | + return; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + Future<void> showErrorDialogFragment() async { |
| 112 | + bool errorDialogFragmentShown; |
| 113 | + |
| 114 | + try { |
| 115 | + errorDialogFragmentShown = |
| 116 | + await GoogleApiAvailabilityAndroid().showErrorDialogFragment(); |
| 117 | + } on PlatformException { |
| 118 | + errorDialogFragmentShown = false; |
| 119 | + } |
| 120 | + |
| 121 | + if (!mounted) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + setState(() { |
| 126 | + _errorDialogFragmentShown = errorDialogFragmentShown; |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + @override |
| 131 | + Widget build(BuildContext context) { |
| 132 | + return MaterialApp( |
| 133 | + home: Scaffold( |
| 134 | + appBar: AppBar( |
| 135 | + title: const Text('Plugin example app'), |
| 136 | + ), |
| 137 | + body: ListView( |
| 138 | + children: <Widget>[ |
| 139 | + MaterialButton( |
| 140 | + onPressed: () => checkPlayServices(), |
| 141 | + color: Colors.red, |
| 142 | + child: const Text('Get PlayServices availability'), |
| 143 | + ), |
| 144 | + Center( |
| 145 | + child: Text( |
| 146 | + 'Google Play Store status: ${_playStoreAvailability.toString().split('.').last}\n')), |
| 147 | + MaterialButton( |
| 148 | + onPressed: () => checkPlayServices(true), |
| 149 | + color: Colors.redAccent, |
| 150 | + child: |
| 151 | + const Text('Get PlayServices availability with fix dialog'), |
| 152 | + ), |
| 153 | + Center( |
| 154 | + child: Text( |
| 155 | + 'Google Play Store status: ${_playStoreAvailability.toString().split('.').last}\n')), |
| 156 | + MaterialButton( |
| 157 | + onPressed: () => makeGooglePlayServicesAvailable(), |
| 158 | + color: Colors.red, |
| 159 | + child: const Text('Make Google Play Service available'), |
| 160 | + ), |
| 161 | + const SizedBox(height: 30), |
| 162 | + MaterialButton( |
| 163 | + onPressed: () => getErrorString(), |
| 164 | + color: Colors.red, |
| 165 | + child: const Text('Get string of the error code'), |
| 166 | + ), |
| 167 | + Center(child: Text('Error string: $_errorString\n')), |
| 168 | + MaterialButton( |
| 169 | + onPressed: () => isUserResolvable(), |
| 170 | + color: Colors.red, |
| 171 | + child: const Text('Error resolvable by user'), |
| 172 | + ), |
| 173 | + Center( |
| 174 | + child: |
| 175 | + Text('Error resolvable by user: $_isUserResolvable\n')), |
| 176 | + MaterialButton( |
| 177 | + onPressed: () => showErrorNotification(), |
| 178 | + color: Colors.red, |
| 179 | + child: const Text('Show error notification'), |
| 180 | + ), |
| 181 | + const SizedBox(height: 30), |
| 182 | + MaterialButton( |
| 183 | + onPressed: () => showErrorDialogFragment(), |
| 184 | + color: Colors.red, |
| 185 | + child: const Text('Show error dialog fragment'), |
| 186 | + ), |
| 187 | + Center( |
| 188 | + child: |
| 189 | + Text('Error dialog shown: $_errorDialogFragmentShown\n')), |
| 190 | + ], |
| 191 | + )), |
| 192 | + ); |
| 193 | + } |
| 194 | +} |
0 commit comments