説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CheckInternetConnection.dart 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import 'dart:async';
  2. import 'dart:developer' as developer;
  3. import 'package:connectivity_plus/connectivity_plus.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:google_fonts/google_fonts.dart';
  7. import 'package:lottie/lottie.dart';
  8. class MyHomePage extends StatefulWidget {
  9. @override
  10. State<MyHomePage> createState() => _MyHomePageState();
  11. }
  12. class _MyHomePageState extends State<MyHomePage> {
  13. ConnectivityResult _connectionStatus = ConnectivityResult.none;
  14. final Connectivity _connectivity = Connectivity();
  15. late StreamSubscription<ConnectivityResult> _connectivitySubscription;
  16. @override
  17. void initState() {
  18. super.initState();
  19. initConnectivity();
  20. _connectivitySubscription = _connectivity.onConnectivityChanged.listen(_updateConnectionStatus);
  21. }
  22. @override
  23. void dispose() {
  24. _connectivitySubscription.cancel();
  25. super.dispose();
  26. }
  27. // Platform messages are asynchronous, so we initialize in an async method.
  28. Future<void> initConnectivity() async {
  29. late ConnectivityResult result;
  30. // Platform messages may fail, so we use a try/catch PlatformException.
  31. try {
  32. result = await _connectivity.checkConnectivity();
  33. } on PlatformException catch (e) {
  34. developer.log('Couldn\'t check connectivity status', error: e);
  35. return;
  36. }
  37. // If the widget was removed from the tree while the asynchronous platform
  38. // message was in flight, we want to discard the reply rather than calling
  39. // setState to update our non-existent appearance.
  40. if (!mounted) {
  41. return Future.value(null);
  42. }
  43. return _updateConnectionStatus(result);
  44. }
  45. Future<void> _updateConnectionStatus(ConnectivityResult result) async {
  46. setState(() {
  47. _connectionStatus = result;
  48. });
  49. }
  50. @override
  51. Widget build(BuildContext context) {
  52. return Container(
  53. child: Column(
  54. children: [
  55. SizedBox(
  56. width: 250,
  57. height: 250,
  58. child: LottieBuilder.asset(
  59. 'assets/animation/animation_no_internet.json',
  60. repeat: true),
  61. ),
  62. Text(
  63. "No Internet Connection",
  64. style: GoogleFonts.josefinSans(fontSize: 18),
  65. ),
  66. Padding(
  67. padding: EdgeInsets.all(15),
  68. child: Text(
  69. 'You Are Not Connected to The Internet. Please check your Wifi or Mobile Data!',
  70. style: GoogleFonts.zillaSlab(fontSize: 16),
  71. ),
  72. ),
  73. ElevatedButton(
  74. onPressed: (){
  75. },
  76. child: Text("Retry",
  77. textAlign: TextAlign.center,
  78. style: TextStyle(
  79. color: Colors.white,
  80. fontSize: 17,
  81. fontWeight: FontWeight.w500)
  82. )
  83. )
  84. ],
  85. ),
  86. );
  87. }
  88. }