[英]WebView in Flutter
I am needing help with Web view in Flutter I have followed so many tutorials even Github and nothing is working I use Visual Studio Code.我在 Flutter 中需要有关 Web 视图的帮助我已经学习了很多教程,甚至是 Github,但没有任何工作我使用 Visual Studio Code。 Please if you can help make sure your code is working all I need is 4 buttons with an icon the phone to work and 3 social sites to link to in Web view.
请如果您能帮助确保您的代码正常工作,我只需要 4 个带有图标的按钮,电话可以工作,3 个社交网站可以在 Web 视图中链接。 If there is a better option please tell me that is all I need and this will be for IOS and Android.
如果有更好的选择,请告诉我这就是我所需要的,这将适用于 IOS 和 Android。
Thank You谢谢你
I had created app using webview_flutter with flutter 1.22.0.我使用带有颤振 1.22.0 的 webview_flutter 创建了应用程序。 Maybe this code will help you.
也许这段代码会对你有所帮助。
class _HomePageState extends State<HomeHomePage> {
final globalKey = GlobalKey<ScaffoldState>();
final Completer<WebViewController> _controller =
Completer<WebViewController>();
WebViewController controller;
AsyncSnapshot<WebViewController> controllerr;
int consumer = 0;
Map _source = {ConnectivityResult.none: false};
MyConnectivity _connectivity = MyConnectivity.instance;
@override
void initState() {
super.initState();
if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
_connectivity.initialise();
_connectivity.myStream.listen((source) {
setState(() => _source = source);
});
}
@override
Widget build(BuildContext context) {
bool connected = false;
String string;
switch (_source.keys.toList()[0]) {
case ConnectivityResult.none:
string = "Offline";
break;
case ConnectivityResult.mobile:
string = "Mobile: Online";
setState(() {
connected = false;
});
break;
case ConnectivityResult.wifi:
string = "WiFi: Online";
setState(() {
connected = false;
});
}
return !connected && _source.keys.toList()[0] != ConnectivityResult.none
? FutureBuilder<WebViewController>(
future: _controller.future,
builder: (BuildContext context,
AsyncSnapshot<WebViewController> controller) {
final bool webViewReady =
controller.connectionState == ConnectionState.done;
final WebViewController controllerSnap = controller.data;
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blueGrey,
toolbarHeight: 0,
),
key: globalKey,
body: WillPopScope(
onWillPop: !webViewReady
? null
: () async {
var status = await controllerSnap.canGoBack();
if (status) {
await controllerSnap.goBack();
return false;
} else {
SystemNavigator.pop();
return true;
}
},
child: WebView(
onWebResourceError: (WebResourceError error) {
if (error.description.toString() ==
'net::ERR_INTERNET_DISCONNECTED') {
setState(() {
consumer++;
connected = true;
});
}
setState(() {
connected = true;
});
},
javascriptMode: JavascriptMode.unrestricted,
initialUrl: 'http://...',
onWebViewCreated:
(WebViewController webViewController) async {
if (consumer > 0) {
} else {
await _controller.complete(webViewController);
}
},
navigationDelegate: (request) {
return _buildNavigationDecision(request);
},
),
));
},
)
: Scaffold(
appBar: AppBar(
toolbarHeight: 0,
),
body: WillPopScope(
onWillPop: doubleClickBack,
child: (Center(
child: Image.asset(
'assets/connecction.jpg',
fit: BoxFit.cover,
),
)),
),
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.