Created
June 4, 2020 17:58
-
-
Save pichillilorenzo/44f4f8921754d046dfbda982f2e681b7 to your computer and use it in GitHub Desktop.
InAppWebView Simple Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_inappwebview/flutter_inappwebview.dart'; | |
Future main() async { | |
WidgetsFlutterBinding.ensureInitialized(); | |
runApp(new MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => new _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
InAppWebViewController _webViewController; | |
String url = ""; | |
double progress = 0; | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar( | |
title: const Text('InAppWebView Example'), | |
), | |
body: Container( | |
child: Column(children: <Widget>[ | |
Container( | |
padding: EdgeInsets.all(20.0), | |
child: Text( | |
"CURRENT URL\n${(url.length > 50) ? url.substring(0, 50) + "..." : url}"), | |
), | |
Container( | |
padding: EdgeInsets.all(10.0), | |
child: progress < 1.0 | |
? LinearProgressIndicator(value: progress) | |
: Container()), | |
Expanded( | |
child: Container( | |
margin: const EdgeInsets.all(10.0), | |
decoration: | |
BoxDecoration(border: Border.all(color: Colors.blueAccent)), | |
child: InAppWebView( | |
initialUrl: "https://flutter.dev/", | |
initialOptions: InAppWebViewGroupOptions( | |
crossPlatform: InAppWebViewOptions( | |
debuggingEnabled: true, | |
) | |
), | |
onWebViewCreated: (InAppWebViewController controller) { | |
_webViewController = controller; | |
}, | |
onLoadStart: (InAppWebViewController controller, String url) { | |
setState(() { | |
this.url = url; | |
}); | |
}, | |
onLoadStop: (InAppWebViewController controller, String url) async { | |
setState(() { | |
this.url = url; | |
}); | |
}, | |
onProgressChanged: (InAppWebViewController controller, int progress) { | |
setState(() { | |
this.progress = progress / 100; | |
}); | |
}, | |
), | |
), | |
), | |
ButtonBar( | |
alignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
RaisedButton( | |
child: Icon(Icons.arrow_back), | |
onPressed: () { | |
if (_webViewController != null) { | |
_webViewController.goBack(); | |
} | |
}, | |
), | |
RaisedButton( | |
child: Icon(Icons.arrow_forward), | |
onPressed: () { | |
if (_webViewController != null) { | |
_webViewController.goForward(); | |
} | |
}, | |
), | |
RaisedButton( | |
child: Icon(Icons.refresh), | |
onPressed: () { | |
if (_webViewController != null) { | |
_webViewController.reload(); | |
} | |
}, | |
), | |
], | |
), | |
])), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment