我有以下代码,可以在Android上完美运行。但是在iOS上,它似乎不像Android那样对在线/离线做出反应。 我已经在代码中修改了网址:
class HomePage extends StatefulWidget {
_State createState() => _State();
}
class _State extends State<HomePage> {
FlutterWebviewPlugin fwp;
final String initialUrl = 'https://example.com/link/';
@override
void initState() {
super.initState();
fwp = FlutterWebviewPlugin();
fwp.onStateChanged.listen(onStateChange);
}
@override
void dispose() {
fwp.dispose();
super.dispose();
}
void onStateChange(WebViewStateChanged state) {
List<String> doNotSave = <String>[
initialUrl,
];
var uri = Uri.parse(state.url);
if (state.type == WebViewState.finishLoad && !doNotSave.contains(state.url) && (uri.queryParameters["link"] == "true")) {
storage.setString('url', state.url);
print("Saved!");
}
}
Widget build(BuildContext context) {
// Uncomment to reset URL
// storage.setString('url', '');
String savedUrl = storage.getString('url') ?? '';
String urlToLoad = savedUrl != '' ? savedUrl : initialUrl;
return new Scaffold(
body: OfflineBuilder(
connectivityBuilder: (
BuildContext context,
ConnectivityResult connectivity,
Widget child,
) {
final bool connected = connectivity != ConnectivityResult.none;
if (connected == true) { // Show link-page or URL from database
fwp.reloadUrl(urlToLoad);
}
else { // Offline 404
fwp.reloadUrl(Uri.dataFromString('<html><body>404</body></html>', mimeType: 'text/html').toString());
}
// Loading dots
return WebviewScaffold(
url: Uri.dataFromString('<html><body>Loading...</body></html>', mimeType: 'text/html').toString(),
withJavascript: true,
useWideViewPort: false,
withZoom: false,
scrollBar: false
);
},
child: Column(
),
),
);
}
}
您可能会看到该应用已启动,并且显示了Loading...
,如果该应用在线,则应显示urlToLoad
,如果在线,则显示一个带有{{1}的页面}。并且如果连接状态随时更改,它应该做出反应。
为什么这在iOS上不起作用?
它只显示了404
部分,而没有其他内容。