Flutter Webview Plugin with Real-time Connection feedback issue

Viewed 23

Im very new in this flutter language and i was trying to create a simple webview should display a single url website, and check on real-time internet connection. For now my app only able to show connectivity result when user first launch the application. How can I further improve my code to get real-time connection and show a pop-up box whenever user goes offline and online? I struggled to configure with connectivity plugin before this since it shows that it was discontinued. Please be gentle

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomeView(),
    );
  }
}

class HomeView extends StatefulWidget {
  const HomeView({Key? key}) : super(key: key);

  @override
  _HomeViewState createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {
  bool isError = false;

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Stack(
          children: [
            if (!isError)
              WebView(
                initialUrl: 'https://www.youtube.com/',
                javascriptMode: JavascriptMode.unrestricted,
                onWebResourceError: (error) => setState(() {
                  isError = true;
                }),
              ),
            if (isError)
              const Center(
                  child: AlertDialog(
                content: Text('No Internet Access!'),
              )),
          ],
        ),
      ),
    );
  }
}
0 Answers
Related