/
webview_scaffold.dart
111 lines (96 loc) · 3.09 KB
/
webview_scaffold.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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);
}
}