Untitled
unknown
plain_text
4 years ago
4.5 kB
3
Indexable
import 'dart:io'; import 'package:flutter/material.dart'; import 'dart:async'; import 'package:webview_flutter/webview_flutter.dart'; import 'package:fluttertoast/fluttertoast.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp(debugShowCheckedModeBanner: false, home: BackButton()); } } class BackButton extends StatefulWidget { @override _BackButtonState createState() => _BackButtonState(); } class _BackButtonState extends State<BackButton> { @override void initState() { super.initState(); // Enable hybrid composition. if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView(); } DateTime backbuttonpressedTime; WebViewController webView; bool showErrorPage = false; bool showErrorGoogle = false; bool _isLoadingPage = true; @override Widget build(BuildContext context) { new Timer(new Duration(seconds: 5), () { setState(() { _isLoadingPage = false; }); }); return Scaffold( appBar: PreferredSize( preferredSize: Size(double.infinity, 0), child: AppBar( title: Text(""), backgroundColor: Colors.deepPurple, ), ), body: WillPopScope( onWillPop: onWillPop, child: Stack( children: <Widget>[ Opacity( opacity: _isLoadingPage ? 1.0 : 0.0, child: Center( child: CircularProgressIndicator( valueColor: AlwaysStoppedAnimation<Color>(Colors.deepPurple), ), ), ), Opacity( opacity: _isLoadingPage ? 0.0 : 1.0, child: WebView( initialUrl: 'https://www.theonlineindia.co.in', javascriptMode: JavascriptMode.unrestricted, onWebViewCreated: (webViewController) { webView = webViewController; }, onPageFinished: (String url) { if (showErrorGoogle == true) { hideError(); showErrorGoogle = false; } }, onWebResourceError: (WebResourceError) { showError(); }, ), ), showErrorPage ? Center( child: Container( color: Colors.white, alignment: Alignment.center, height: double.infinity, width: double.infinity, child: Text('No internet connection!!!', style: TextStyle( fontWeight: FontWeight.bold, fontSize: 20)), ), ) : SizedBox(height: 0, width: 0), ], ), ), ); } void showError() { setState(() { showErrorPage = true; check(); }); } void hideError() { setState(() { showErrorPage = false; }); } Future check() async { var timer = new Timer.periodic(new Duration(seconds: 10), (time) async { if (showErrorPage == true) { try { final result = await InternetAddress.lookup('google.com'); if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) { showErrorGoogle = true; webView.reload(); } } on SocketException catch (_) { Fluttertoast.showToast( msg: "Auto refresh failed, no internet.", backgroundColor: Colors.deepPurple, textColor: Colors.white, toastLength: Toast.LENGTH_LONG); } } }); if (showErrorPage == false) timer.cancel(); } Future<bool> onWillPop() async { DateTime currentTime = DateTime.now(); bool backButton = backbuttonpressedTime == null || currentTime.difference(backbuttonpressedTime) > Duration(seconds: 2); if (webView != null) { if (await webView.canGoBack()) { webView.goBack(); return false; } else { if (backButton) { backbuttonpressedTime = currentTime; Fluttertoast.showToast( msg: "Tap again to exit the app", backgroundColor: Colors.deepPurple, textColor: Colors.white); return false; } } } return true; } }
Editor is loading...