Flutter WebView Plugin Load new pages with setState(){}?

2,013 views
Skip to first unread message

leo nx

unread,
May 15, 2018, 12:47:39 PM5/15/18
to Flutter Dev
Hey, I'm doing an app with the WebView Plugin from flutter, but I don't know how to change the website on Button press.

I wan't to use the same webview from my WebviewScaffold if possible.

Eugenio Tesio

unread,
May 15, 2018, 1:37:10 PM5/15/18
to leo nx, Flutter Dev
This is the WebViewScaffold Class:

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'base.dart';

class WebviewScaffold extends StatefulWidget {
final PreferredSizeWidget appBar;
final String url;
final bool withJavascript;
final bool clearCache;
final bool clearCookies;
final bool enableAppScheme;
final String userAgent;
final bool primary;
final List<Widget> persistentFooterButtons;
final Widget bottomNavigationBar;
final bool withZoom;
final bool withLocalStorage;

WebviewScaffold(
{Key key,
this.appBar,
@required this.url,
this.withJavascript,
this.clearCache,
this.clearCookies,
this.enableAppScheme,
this.userAgent,
this.primary: true,
this.persistentFooterButtons,
this.bottomNavigationBar,
this.withZoom,
this.withLocalStorage})
: super(key: key);

@override
_WebviewScaffoldState createState() => new _WebviewScaffoldState();
}

class _WebviewScaffoldState extends State<WebviewScaffold> {
final webviewReference = new FlutterWebviewPlugin();
Rect _rect;
Timer _resizeTimer;

void initState() {
super.initState();
webviewReference.close();
}

void dispose() {
super.dispose();
webviewReference.close();
webviewReference.dispose();
}

@override
Widget build(BuildContext context) {
if (_rect == null) {
_rect = _buildRect(context);
webviewReference.launch(widget.url,
withJavascript: widget.withJavascript,
clearCache: widget.clearCache,
clearCookies: widget.clearCookies,
enableAppScheme: widget.enableAppScheme,
userAgent: widget.userAgent,
rect: _rect,
withZoom: widget.withZoom,
withLocalStorage: widget.withLocalStorage);
} else {
Rect rect = _buildRect(context);
if (_rect != rect) {
_rect = rect;
_resizeTimer?.cancel();
_resizeTimer = new Timer(new Duration(milliseconds: 300), () {
// avoid resizing to fast when build is called multiple time
webviewReference.resize(_rect);
});
}
}
return new Scaffold(
appBar: widget.appBar,
persistentFooterButtons: widget.persistentFooterButtons,
bottomNavigationBar: widget.bottomNavigationBar,
body: new Center(child: new CircularProgressIndicator()));
}

Rect _buildRect(BuildContext context) {
bool fullscreen = widget.appBar == null;

final mediaQuery = MediaQuery.of(context);
final topPadding = widget.primary ? mediaQuery.padding.top : 0.0;
num top =
fullscreen ? 0.0 : widget.appBar.preferredSize.height + topPadding;

num height = mediaQuery.size.height - top;

if (widget.bottomNavigationBar != null) {
height -=
56.0; // todo(lejard_h) find a way to determine bottomNavigationBar programmatically
}

if (widget.persistentFooterButtons != null) {
height -=
53.0; // todo(lejard_h) find a way to determine persistentFooterButtons programmatically
}

return new Rect.fromLTWH(0.0, top, mediaQuery.size.width, height);
}
}
As you can see the url is final and you can't change it. 

So maybe you can create your own WebView component that allow to change the url and refresh the view.


El mar., 15 may. 2018 a las 13:47, leo nx (<leo.d...@gmail.com>) escribió:
Hey, I'm doing an app with the WebView Plugin from flutter, but I don't know how to change the website on Button press.

I wan't to use the same webview from my WebviewScaffold if possible.

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Eugenio Tesio
Zetus - Soluciones Empresariales
División Sistemas
(+549) 3564-15-599945
San Francisco - 2400
Pcia de Córdoba - Argentina



leo nx

unread,
May 15, 2018, 2:17:29 PM5/15/18
to Flutter Dev
Sorry, I'm pretty much a noob in flutter, how would i do that, without creating a route/a new webview?

Eugenio Tesio

unread,
May 15, 2018, 3:09:49 PM5/15/18
to leo nx, Flutter Dev
  1. Create a file named MyWebView.dart
  2. Copy and Paste the code i'd posted before.
  3. Replace all "WebView" text in the file for "MyWebView"
  4. Call MyWebViewScaffold instead WebViewScaffod.
  5. Modify your code as you need in the file MyWebView.dart.
Reply all
Reply to author
Forward
0 new messages