flutter - Flutter 在片段内加载 webview
问题描述
// 这是我的颤振代码
import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
class HairtipsPage extends StatefulWidget {
@override
_HairtipsPageState createState() => _HairtipsPageState();
}
class _HairtipsPageState extends State<HairtipsPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child : WebviewScaffold(
url: "https://www.google.com",
appBar: new AppBar(
// title: new Text('Hairtips'),
),
withZoom: true,
withLocalStorage: true,
)
),
);
}
}
我在我的应用程序中使用底部导航并尝试在片段中实现 webview。我知道如何在 android 中实现同样的效果,我也不希望 webview 应该在浏览器中打开。我希望 webview 应该在应用程序内部和内部加载片段。
解决方案
你可以使用 Flutter webview 插件。这是插件的 URL https://pub.dartlang.org/packages/webview_flutter
webview 将使用 CircularProgressIndicator 在应用程序中加载。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class WebView extends StatefulWidget {
@override
_WebViewState createState() => _WebViewState();
}
class _WebViewState extends State<WebView> {
final Completer<WebViewController> _controller =
Completer<WebViewController>();
num _stackToView = 1;
void _handleLoad(String value) {
setState(() {
_stackToView = 0;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: Builder(builder: (BuildContext context) {
return IconButton(
icon: Icon(Icons.volume_up, color: Colors.black,),
onPressed: () {
Navigator.pop(context);
},
);
}),
backgroundColor: Colors.white10,
elevation: 0,
),
body: IndexedStack(
index: _stackToView,
children: [
Column(
children: <Widget>[
Expanded(
child: WebView(
initialUrl: "https://www.google.co.in/",
javascriptMode: JavascriptMode.unrestricted,
onPageFinished: _handleLoad,
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
)),
],
),
Container(
child: Center(child: CircularProgressIndicator(),)
),
],
));
}
}
推荐阅读
- javascript - 在按钮单击中创建下拉菜单并获取所选值
- intellij-idea - Intellij 和通过鼠标解决参考,而不是 Alt-Enter
- swift - 带有 WebView iOS 的 AR.JS
- mysql - How to keep rows with NULL values that don't fulfill the condition in the WHERE clause after LEFT JOIN-ing two tables
- excel - VBA - SaveCopyAs hidden
- python - 绘制文本时枕头图像的隐含像素密度是多少?
- .net - 如何将 Azure Service Fabric 可靠服务与一个写入存储的服务和另一个从存储读取的服务一起使用
- nativescript - Nativescript Ios 垂直对齐
- vuepress - .vuepress/components 文件夹中的组件不呈现
- reactjs - 如何在 ReactJS 中切换多个类