Use flutter_inappwebview to load remote html files in Flutter
1. Install the plugin
Configure the flutter_inappwebview plugin.
- dependencies:
- flutter:
- sdk: flutter
- flutter_localizations:
- sdk: flutter
- date_format: ^1.0.6
- flutter_cupertino_date_picker: ^1.0.26+2
- flutter_swiper: ^ 1.1 . 6
- fluttertoast: ^7.1.6
- http: ^0.12.2
- part: ^ 3.0 . 10
- flutter_html: ^1.1.0
-
- # Load remote HTML plugin
- flutter_inappwebview: ^4.0.0+4
After the configuration is saved in pubspec.yaml, the dependency package will be automatically downloaded in the VS Code environment.
If it cannot be downloaded normally, execute flutter pub get .
2. Introduce dependencies
Introduce the plug-in package in the file of the plug-in that needs to be used.
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
3. Use plugins
- InAppWebView(
- initialUrl: "https://www.baidu.com",
- initialHeaders: {},
- initialOptions: InAppWebViewGroupOptions(
- inAppWebViewOptions: InAppWebViewOptions(
- debuggingEnabled: true,
- )
- ),
- onWebViewCreated: (InAppWebViewController controller) {
-
- },
- onLoadStart: (InAppWebViewController controller, String url) {
-
- },
- onLoadStop: (InAppWebViewController controller, String url) {
-
- },
- )
4. Precautions
1. If the SDK version is too low, please upgrade as follows.
2. If you are prompted that you do not have network permissions, please add the following configuration to add network permissions.
5. Complete example
- import 'package:flutter/material.dart';
- import 'package:flutter_inappwebview/flutter_inappwebview.dart';
-
- class DetailPage extends StatefulWidget {
- final Map arguments;
- DetailPage({Key key,this.arguments}) : super(key: key);
-
- @override
- _DetailPageState createState() => _DetailPageState(this.arguments);
- }
-
- class _DetailPageState extends State<DetailPage> {
-
- // Whether to show the loading animation
- bool _flag = false;
- final Map arguments;
- _DetailPageState(this.arguments);
-
- @override
- void initState() {
- super.initState();
- }
-
- @override
- Widget build(BuildContext context) {
- return Container(
- child: Scaffold(
- appBar: AppBar(
- title: Text( "News Details" ),
- ),
- body: Column(
- children: <Widget>[
- this._flag?_getMoreWidget():Text(""),
- Expanded(
- // official code
- child:InAppWebView(
- initialUrl: "http://www.ifindbug.com/doc/id-44927/name-Implement pull-down refresh and pull-up loading in Flutter.html",
- // Load progress change event
- onProgressChanged: (InAppWebViewController controller, int progress) {
- if((progress/100)>0.999){
- setState (() {
- this._flag=false;
- });
- }
- },
- ),
- )
- ],
- )
- )
- );
- }
- // load state
- Widget _getMoreWidget() {
- return Center(
- child: Padding(
- padding: EdgeInsets.all(10.0),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.center,
- crossAxisAlignment: CrossAxisAlignment.center,
- children: <Widget>[
- Text(
- 'Loading...' ,
- style: TextStyle(fontSize: 16.0 ),
- ),
- CircularProgressIndicator(
- strokeWidth: 1.0,
- )
- ],
- ),
- ),
- );
- }
- }
-
-
-
Below is the rendering of loading the remote page.
Reference: flutter_inappwebview | Flutter Package