我正在尝试使用flutter_webview_plugin 0.3.11构建应用程序。不幸的是,我无法弄清楚当没有互联网连接时如何显示静态HTML。
按照下面的代码,当没有Internet连接时,AppBar标题显示为灰色,而IconButtons也显示在AppBar上,但是
url: new Uri.dataFromString('<html><body><h1 style="color: red;">Sorry, there is no connection.</h1><h2>Please check your Internet Connection and try again</h2></body></html>', mimeType: 'text/html', encoding: Encoding.getByName('utf-8')).toString(),
没有让人失望。
这是我的代码:
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:connectivity/connectivity.dart';
import 'package:flutter/services.dart' show rootBundle;
//import 'package:swipedetector/swipedetector.dart';
String nonConnectedUrl2 = new Uri.dataFromString('<html><body><h1 style="color: red;">Sorry, there is no Internet connection.</h1><h2>Please check your Internet Connection and try again</h2></body></html>', mimeType: 'text/html').toString();
//
String connectedUrl ='https://tripplanner.com.bd';
String connectedUrl2 = 'https://tripplanner.com.bd';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
//bool isInternetOn = false;
bool isInternetOn = true;
final flutterWebViewPlugin = new FlutterWebviewPlugin();
StreamSubscription _onDestroy;
// On urlChanged stream
StreamSubscription<String> _onUrlChanged;
StreamSubscription<WebViewStateChanged> _onStateChanged;
StreamSubscription<WebViewHttpError> _onHttpError;
final _scaffoldKey = GlobalKey<ScaffoldState>();
final _history = [];
@override
void initState() {
// TODO: implement initState
super.initState();
// getConnect(); // calls getconnect method to check which type if connection it
print("INTERNET STATUS 1 ${this.isInternetOn}");
flutterWebViewPlugin.close();
// Add a listener to on destroy WebView, so you can make came actions.
_onDestroy = flutterWebViewPlugin.onDestroy.listen((_) {
if (mounted) {
// Actions like show a info toast.
_scaffoldKey.currentState.showSnackBar(
const SnackBar(content: const Text('Bye Bye From TripPlanner')));
}
});
// Add a listener to on url changed
_onUrlChanged = flutterWebViewPlugin.onUrlChanged.listen((String url) {
if (mounted) {
setState(() {
_history.add('onUrlChanged: $url');
});
}
});
_onHttpError =
flutterWebViewPlugin.onHttpError.listen((WebViewHttpError error) {
if (mounted) {
setState(() {
_history.add('onHttpError: ${error.code} ${error.url}');
});
}
});
}
@override
void dispose() {
// Every listener should be canceled, the same should be done with this stream.
_onDestroy.cancel();
_onUrlChanged.cancel();
_onStateChanged.cancel();
_onHttpError.cancel();
flutterWebViewPlugin.dispose();
isInternetOn = false;
super.dispose();
}
@override
Widget build(BuildContext context) {
getConnect();
print("INTERNET STATUS 2 ${this.isInternetOn}");
//return GestureDetector(
if (isInternetOn == true) {
print("INTERNET IS FUKKU ON 1");
return WebviewScaffold(
url: connectedUrl,
withJavascript: true,
withZoom: false,
hidden: true ,
allowFileURLs: true,
ignoreSSLErrors: true,
withLocalUrl: true,
withLocalStorage: true,
appBar: AppBar(
backgroundColor: Hexcolor("#c1272d"),
leading: Image.asset(
'assets/images/logo1.png',
fit: BoxFit.cover,
height: 35.0,
),
title:Text('TripPlanner',
style: TextStyle(
color: Colors.white,
),
),
/*
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset(
'assets/images/logo1.png',
fit: BoxFit.cover,
height: 35.0,
),
Container(
padding: const EdgeInsets.all(8.0), child: Text('TripPlanner')
)
],
),
*/
//action for reloading....
actions: [
IconButton(icon: Icon(Icons.arrow_back),
onPressed: (){
flutterWebViewPlugin.goBack();
//controller.text="";
}
),
IconButton(icon: Icon(Icons.refresh),
onPressed: (){
flutterWebViewPlugin.reload();
}
),
],
),
initialChild: Container(
height: double.infinity,
width: double.infinity,
color: Colors. white,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget> [
Container(
padding: const EdgeInsets.only(bottom: 8),
child: Image(
image: ExactAssetImage ("assets/images/logo1.png")
),
// child: NetworkImage(''),
),
// FlutterLogo(size: 200),
SpinKitWave(color: Hexcolor("#c1272d"), type: SpinKitWaveType.start),
],
), ),
)
;
} else {
print("INTERNET IS OFF 2");
return WebviewScaffold(
// url: new Uri.dataFromString('<html><body>hello world</body></html>',
// mimeType: 'text/html').toString(),
withJavascript: true,
withZoom: false,
hidden: true ,
allowFileURLs: true,
ignoreSSLErrors: true,
withLocalUrl: true,
withLocalStorage: true,
// url: new Uri.dataFromString('<html><body><h1 style="color: red;">Sorry, there is no connection.</h1><h2>Please check your Internet Connection and try again</h2></body></html>', mimeType: 'text/html', encoding: Encoding.getByName('utf-8')).toString(),
url: nonConnectedUrl2,
appBar: AppBar(
//backgroundColor: Hexcolor("#c1272d"),
backgroundColor: Colors.grey,
leading: Image.asset(
'assets/images/logogrey2.png',
fit: BoxFit.cover,
height: 35.0,
),
title:Text('TripPlanner',
style: TextStyle(
color: Colors.white,
),
),
/*
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset(
'assets/images/logo1.png',
fit: BoxFit.cover,
height: 35.0,
),
Container(
padding: const EdgeInsets.all(8.0), child: Text('TripPlanner')
)
],
),
*/
//action for reloading....
actions: [
IconButton(icon: Icon(Icons.arrow_back),
onPressed: (){
flutterWebViewPlugin.goBack();
//controller.text="";
}
),
IconButton(icon: Icon(Icons.refresh),
onPressed: (){
flutterWebViewPlugin.reload();
}
),
],
),
initialChild: Container(
height: double.infinity,
width: double.infinity,
color: Colors. white,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget> [
Container(
padding: const EdgeInsets.only(bottom: 8),
child: Image(
image: ExactAssetImage ("assets/images/logo1.png")
),
// child: NetworkImage(''),
),
// FlutterLogo(size: 200),
SpinKitWave(color: Hexcolor("#c1272d"), type: SpinKitWaveType.start),
],
), ),
);
}
}
void getConnect() async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.none) {
setState(() {
isInternetOn = false;
print("Internet NOT connected");
});
} else {
setState(() {
isInternetOn = true;
print("Internet IS connected");
});
}
}
}
如果有人可以帮助我,我将非常感激。 先感谢您, 罗比