|
|
@@ -9,28 +9,61 @@ const kAndroidUserAgent =
|
|
|
|
|
|
String selectedUrl = 'https://flutter.io';
|
|
|
|
|
|
-void main() {
|
|
|
- runApp(new MyApp());
|
|
|
-}
|
|
|
+void main() => runApp(MyApp());
|
|
|
|
|
|
class MyApp extends StatelessWidget {
|
|
|
+ final flutterWebViewPlugin = FlutterWebviewPlugin();
|
|
|
+
|
|
|
@override
|
|
|
Widget build(BuildContext context) {
|
|
|
- return new MaterialApp(
|
|
|
+ return MaterialApp(
|
|
|
title: 'Flutter WebView Demo',
|
|
|
- theme: new ThemeData(
|
|
|
+ theme: ThemeData(
|
|
|
primarySwatch: Colors.blue,
|
|
|
),
|
|
|
routes: {
|
|
|
'/': (_) => const MyHomePage(title: 'Flutter WebView Demo'),
|
|
|
- '/widget': (_) => new WebviewScaffold(
|
|
|
- url: selectedUrl,
|
|
|
- appBar: new AppBar(
|
|
|
- title: const Text('Widget webview'),
|
|
|
+ '/widget': (_) {
|
|
|
+ return WebviewScaffold(
|
|
|
+ url: selectedUrl,
|
|
|
+ appBar: AppBar(
|
|
|
+ title: const Text('Widget WebView'),
|
|
|
+ ),
|
|
|
+ withZoom: true,
|
|
|
+ withLocalStorage: true,
|
|
|
+ hidden: true,
|
|
|
+ initialChild: Container(
|
|
|
+ color: Colors.redAccent,
|
|
|
+ child: const Center(
|
|
|
+ child: Text('Waiting.....'),
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ bottomNavigationBar: BottomAppBar(
|
|
|
+ child: Row(
|
|
|
+ children: <Widget>[
|
|
|
+ IconButton(
|
|
|
+ icon: const Icon(Icons.arrow_back_ios),
|
|
|
+ onPressed: () {
|
|
|
+ flutterWebViewPlugin.goBack();
|
|
|
+ },
|
|
|
+ ),
|
|
|
+ IconButton(
|
|
|
+ icon: const Icon(Icons.arrow_forward_ios),
|
|
|
+ onPressed: () {
|
|
|
+ flutterWebViewPlugin.goForward();
|
|
|
+ },
|
|
|
+ ),
|
|
|
+ IconButton(
|
|
|
+ icon: const Icon(Icons.autorenew),
|
|
|
+ onPressed: () {
|
|
|
+ flutterWebViewPlugin.reload();
|
|
|
+ },
|
|
|
+ ),
|
|
|
+ ],
|
|
|
),
|
|
|
- withZoom: true,
|
|
|
- withLocalStorage: true,
|
|
|
- )
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ },
|
|
|
},
|
|
|
);
|
|
|
}
|
|
|
@@ -42,12 +75,12 @@ class MyHomePage extends StatefulWidget {
|
|
|
final String title;
|
|
|
|
|
|
@override
|
|
|
- _MyHomePageState createState() => new _MyHomePageState();
|
|
|
+ _MyHomePageState createState() => _MyHomePageState();
|
|
|
}
|
|
|
|
|
|
class _MyHomePageState extends State<MyHomePage> {
|
|
|
// Instance of WebView plugin
|
|
|
- final flutterWebviewPlugin = new FlutterWebviewPlugin();
|
|
|
+ final flutterWebViewPlugin = FlutterWebviewPlugin();
|
|
|
|
|
|
// On destroy stream
|
|
|
StreamSubscription _onDestroy;
|
|
|
@@ -64,12 +97,11 @@ class _MyHomePageState extends State<MyHomePage> {
|
|
|
|
|
|
StreamSubscription<double> _onScrollXChanged;
|
|
|
|
|
|
- final _urlCtrl = new TextEditingController(text: selectedUrl);
|
|
|
+ final _urlCtrl = TextEditingController(text: selectedUrl);
|
|
|
|
|
|
- final _codeCtrl =
|
|
|
- new TextEditingController(text: 'window.navigator.userAgent');
|
|
|
+ final _codeCtrl = TextEditingController(text: 'window.navigator.userAgent');
|
|
|
|
|
|
- final _scaffoldKey = new GlobalKey<ScaffoldState>();
|
|
|
+ final _scaffoldKey = GlobalKey<ScaffoldState>();
|
|
|
|
|
|
final _history = [];
|
|
|
|
|
|
@@ -77,23 +109,22 @@ class _MyHomePageState extends State<MyHomePage> {
|
|
|
void initState() {
|
|
|
super.initState();
|
|
|
|
|
|
- flutterWebviewPlugin.close();
|
|
|
+ flutterWebViewPlugin.close();
|
|
|
|
|
|
_urlCtrl.addListener(() {
|
|
|
selectedUrl = _urlCtrl.text;
|
|
|
});
|
|
|
|
|
|
// Add a listener to on destroy WebView, so you can make came actions.
|
|
|
- _onDestroy = flutterWebviewPlugin.onDestroy.listen((_) {
|
|
|
+ _onDestroy = flutterWebViewPlugin.onDestroy.listen((_) {
|
|
|
if (mounted) {
|
|
|
// Actions like show a info toast.
|
|
|
- _scaffoldKey.currentState.showSnackBar(
|
|
|
- const SnackBar(content: const Text('Webview Destroyed')));
|
|
|
+ _scaffoldKey.currentState.showSnackBar(const SnackBar(content: const Text('Webview Destroyed')));
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// Add a listener to on url changed
|
|
|
- _onUrlChanged = flutterWebviewPlugin.onUrlChanged.listen((String url) {
|
|
|
+ _onUrlChanged = flutterWebViewPlugin.onUrlChanged.listen((String url) {
|
|
|
if (mounted) {
|
|
|
setState(() {
|
|
|
_history.add('onUrlChanged: $url');
|
|
|
@@ -101,26 +132,23 @@ class _MyHomePageState extends State<MyHomePage> {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- _onScrollYChanged =
|
|
|
- flutterWebviewPlugin.onScrollYChanged.listen((double y) {
|
|
|
+ _onScrollYChanged = flutterWebViewPlugin.onScrollYChanged.listen((double y) {
|
|
|
if (mounted) {
|
|
|
setState(() {
|
|
|
- _history.add("Scroll in Y Direction: $y");
|
|
|
+ _history.add('Scroll in Y Direction: $y');
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- _onScrollXChanged =
|
|
|
- flutterWebviewPlugin.onScrollXChanged.listen((double x) {
|
|
|
+ _onScrollXChanged = flutterWebViewPlugin.onScrollXChanged.listen((double x) {
|
|
|
if (mounted) {
|
|
|
setState(() {
|
|
|
- _history.add("Scroll in X Direction: $x");
|
|
|
+ _history.add('Scroll in X Direction: $x');
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- _onStateChanged =
|
|
|
- flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) {
|
|
|
+ _onStateChanged = flutterWebViewPlugin.onStateChanged.listen((WebViewStateChanged state) {
|
|
|
if (mounted) {
|
|
|
setState(() {
|
|
|
_history.add('onStateChanged: ${state.type} ${state.url}');
|
|
|
@@ -128,8 +156,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- _onHttpError =
|
|
|
- flutterWebviewPlugin.onHttpError.listen((WebViewHttpError error) {
|
|
|
+ _onHttpError = flutterWebViewPlugin.onHttpError.listen((WebViewHttpError error) {
|
|
|
if (mounted) {
|
|
|
setState(() {
|
|
|
_history.add('onHttpError: ${error.code} ${error.url}');
|
|
|
@@ -148,89 +175,91 @@ class _MyHomePageState extends State<MyHomePage> {
|
|
|
_onScrollXChanged.cancel();
|
|
|
_onScrollYChanged.cancel();
|
|
|
|
|
|
- flutterWebviewPlugin.dispose();
|
|
|
+ flutterWebViewPlugin.dispose();
|
|
|
|
|
|
super.dispose();
|
|
|
}
|
|
|
|
|
|
@override
|
|
|
Widget build(BuildContext context) {
|
|
|
- return new Scaffold(
|
|
|
+ return Scaffold(
|
|
|
key: _scaffoldKey,
|
|
|
- appBar: new AppBar(
|
|
|
+ appBar: AppBar(
|
|
|
title: const Text('Plugin example app'),
|
|
|
),
|
|
|
- body: new Column(
|
|
|
- mainAxisAlignment: MainAxisAlignment.center,
|
|
|
- children: [
|
|
|
- new Container(
|
|
|
- padding: const EdgeInsets.all(24.0),
|
|
|
- child: new TextField(controller: _urlCtrl),
|
|
|
- ),
|
|
|
- new RaisedButton(
|
|
|
- onPressed: () {
|
|
|
- flutterWebviewPlugin.launch(selectedUrl,
|
|
|
- rect: new Rect.fromLTWH(
|
|
|
- 0.0, 0.0, MediaQuery.of(context).size.width, 300.0),
|
|
|
- userAgent: kAndroidUserAgent);
|
|
|
- },
|
|
|
- child: const Text('Open Webview (rect)'),
|
|
|
- ),
|
|
|
- new RaisedButton(
|
|
|
- onPressed: () {
|
|
|
- flutterWebviewPlugin.launch(selectedUrl, hidden: true);
|
|
|
- },
|
|
|
- child: const Text('Open "hidden" Webview'),
|
|
|
- ),
|
|
|
- new RaisedButton(
|
|
|
- onPressed: () {
|
|
|
- flutterWebviewPlugin.launch(selectedUrl);
|
|
|
- },
|
|
|
- child: const Text('Open Fullscreen Webview'),
|
|
|
- ),
|
|
|
- new RaisedButton(
|
|
|
- onPressed: () {
|
|
|
- Navigator.of(context).pushNamed('/widget');
|
|
|
- },
|
|
|
- child: const Text('Open widget webview'),
|
|
|
- ),
|
|
|
- new Container(
|
|
|
- padding: const EdgeInsets.all(24.0),
|
|
|
- child: new TextField(controller: _codeCtrl),
|
|
|
- ),
|
|
|
- new RaisedButton(
|
|
|
- onPressed: () {
|
|
|
- final future =
|
|
|
- flutterWebviewPlugin.evalJavascript(_codeCtrl.text);
|
|
|
- future.then((String result) {
|
|
|
- setState(() {
|
|
|
- _history.add('eval: $result');
|
|
|
+ body: SingleChildScrollView(
|
|
|
+ child: Column(
|
|
|
+ mainAxisAlignment: MainAxisAlignment.center,
|
|
|
+ children: [
|
|
|
+ Container(
|
|
|
+ padding: const EdgeInsets.all(24.0),
|
|
|
+ child: TextField(controller: _urlCtrl),
|
|
|
+ ),
|
|
|
+ RaisedButton(
|
|
|
+ onPressed: () {
|
|
|
+ flutterWebViewPlugin.launch(
|
|
|
+ selectedUrl,
|
|
|
+ rect: Rect.fromLTWH(0.0, 0.0, MediaQuery.of(context).size.width, 300.0),
|
|
|
+ userAgent: kAndroidUserAgent,
|
|
|
+ );
|
|
|
+ },
|
|
|
+ child: const Text('Open Webview (rect)'),
|
|
|
+ ),
|
|
|
+ RaisedButton(
|
|
|
+ onPressed: () {
|
|
|
+ flutterWebViewPlugin.launch(selectedUrl, hidden: true);
|
|
|
+ },
|
|
|
+ child: const Text('Open "hidden" Webview'),
|
|
|
+ ),
|
|
|
+ RaisedButton(
|
|
|
+ onPressed: () {
|
|
|
+ flutterWebViewPlugin.launch(selectedUrl);
|
|
|
+ },
|
|
|
+ child: const Text('Open Fullscreen Webview'),
|
|
|
+ ),
|
|
|
+ RaisedButton(
|
|
|
+ onPressed: () {
|
|
|
+ Navigator.of(context).pushNamed('/widget');
|
|
|
+ },
|
|
|
+ child: const Text('Open widget webview'),
|
|
|
+ ),
|
|
|
+ Container(
|
|
|
+ padding: const EdgeInsets.all(24.0),
|
|
|
+ child: TextField(controller: _codeCtrl),
|
|
|
+ ),
|
|
|
+ RaisedButton(
|
|
|
+ onPressed: () {
|
|
|
+ final future = flutterWebViewPlugin.evalJavascript(_codeCtrl.text);
|
|
|
+ future.then((String result) {
|
|
|
+ setState(() {
|
|
|
+ _history.add('eval: $result');
|
|
|
+ });
|
|
|
});
|
|
|
- });
|
|
|
- },
|
|
|
- child: const Text('Eval some javascript'),
|
|
|
- ),
|
|
|
- new RaisedButton(
|
|
|
- onPressed: () {
|
|
|
- setState(() {
|
|
|
- _history.clear();
|
|
|
- });
|
|
|
- flutterWebviewPlugin.close();
|
|
|
- },
|
|
|
- child: const Text('Close'),
|
|
|
- ),
|
|
|
- new RaisedButton(
|
|
|
- onPressed: () {
|
|
|
- flutterWebviewPlugin.getCookies().then((m) {
|
|
|
+ },
|
|
|
+ child: const Text('Eval some javascript'),
|
|
|
+ ),
|
|
|
+ RaisedButton(
|
|
|
+ onPressed: () {
|
|
|
setState(() {
|
|
|
- _history.add('cookies: $m');
|
|
|
+ _history.clear();
|
|
|
+ });
|
|
|
+ flutterWebViewPlugin.close();
|
|
|
+ },
|
|
|
+ child: const Text('Close'),
|
|
|
+ ),
|
|
|
+ RaisedButton(
|
|
|
+ onPressed: () {
|
|
|
+ flutterWebViewPlugin.getCookies().then((m) {
|
|
|
+ setState(() {
|
|
|
+ _history.add('cookies: $m');
|
|
|
+ });
|
|
|
});
|
|
|
- });
|
|
|
- },
|
|
|
- child: const Text('Cookies'),
|
|
|
- ),
|
|
|
- new Text(_history.join('\n'))
|
|
|
- ],
|
|
|
+ },
|
|
|
+ child: const Text('Cookies'),
|
|
|
+ ),
|
|
|
+ Text(_history.join('\n'))
|
|
|
+ ],
|
|
|
+ ),
|
|
|
),
|
|
|
);
|
|
|
}
|