要想在Flutter中浏览网页可以使用webview
插件。
添加依赖和配置
在pubspec.yaml
的依赖中添加:
dependencies:
flutter:
sdk: flutter
webview_flutter: ^0.3.5
复制代码
在ios/Runner/Info.plist中添加:
<dict>
...
<key>io.flutter.embedded_views_preview</key>
<true/>
....
</dict>
复制代码
简单使用
可以使用WebViewController
来控制webview
的网页切换等操作。
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class WebViewApp extends StatelessWidget{
@override
Widget build(BuildContext context) => MaterialApp(
home: _HomePage(),
);
}
class _HomePage extends StatelessWidget{
WebViewController _controller;
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text("WebView"),),
body: Center(
child: _buildWebView(),
),
);
Widget _buildWebView() => WebView(onWebViewCreated: (c){
_controller =c;
_controller.loadUrl("https://pub.dartlang.org/packages/webview_flutter");
},);
}
复制代码