Skip to content

Commit dcff178

Browse files
authored
Merge pull request #160 from js1972/progress
Enable progress indicator for page load with WebviewScaffold
2 parents 08ca8f0 + 5b3c3e1 commit dcff178

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
22
.atom/
33
.idea
4+
.vscode
45
.packages
56
.pub/
67
build/

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# flutter_webview_plugin
55

6-
Plugin that allow Flutter to communicate with a native WebView.
6+
Plugin that allows Flutter to communicate with a native WebView.
77

88
***Warning:***
99
The webview is not integrated in the widget tree, it is a native view on top of the flutter view.
@@ -30,6 +30,38 @@ new MaterialApp(
3030
);
3131
```
3232

33+
Optional parameters `hidden` and `initialChild` are available so that you can show something else while waiting for the page to load.
34+
If you set `hidden` to true it will show a default CircularProgressIndicator. If you additionally specify a Widget for initialChild
35+
you can have it display whatever you like till page-load.
36+
37+
e.g. The following will show a read screen with the text 'waiting.....'.
38+
```dart
39+
return new MaterialApp(
40+
title: 'Flutter WebView Demo',
41+
theme: new ThemeData(
42+
primarySwatch: Colors.blue,
43+
),
44+
routes: {
45+
'/': (_) => const MyHomePage(title: 'Flutter WebView Demo'),
46+
'/widget': (_) => new WebviewScaffold(
47+
url: selectedUrl,
48+
appBar: new AppBar(
49+
title: const Text('Widget webview'),
50+
),
51+
withZoom: true,
52+
withLocalStorage: true,
53+
hidden: true,
54+
initialChild: Container(
55+
color: Colors.redAccent,
56+
child: const Center(
57+
child: Text('Waiting.....'),
58+
),
59+
),
60+
)
61+
},
62+
);
63+
```
64+
3365
`FlutterWebviewPlugin` provide a singleton instance linked to one unique webview,
3466
so you can take control of the webview from anywhere in the app
3567

example/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@
242242
);
243243
inputPaths = (
244244
"${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
245-
"${PODS_ROOT}/../../../../../development/flutter/bin/cache/artifacts/engine/ios/Flutter.framework",
245+
"${PODS_ROOT}/../../../../../flutter/bin/cache/artifacts/engine/ios/Flutter.framework",
246246
"${BUILT_PRODUCTS_DIR}/flutter_webview_plugin/flutter_webview_plugin.framework",
247247
);
248248
name = "[CP] Embed Pods Frameworks";

example/lib/main.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ class MyApp extends StatelessWidget {
3232
),
3333
withZoom: true,
3434
withLocalStorage: true,
35+
hidden: true,
36+
initialChild: Container(
37+
color: Colors.redAccent,
38+
child: const Center(
39+
child: Text('Waiting.....'),
40+
),
41+
),
3542
bottomNavigationBar: BottomAppBar(
3643
child: Row(
3744
children: <Widget>[
@@ -146,6 +153,7 @@ class _MyHomePageState extends State<MyHomePage> {
146153

147154
_onStateChanged =
148155
flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) {
156+
149157
if (mounted) {
150158
setState(() {
151159
_history.add('onStateChanged: ${state.type} ${state.url}');

lib/src/webview_scaffold.dart

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class WebviewScaffold extends StatefulWidget {
2121
final bool withLocalStorage;
2222
final bool withLocalUrl;
2323
final bool scrollBar;
24+
final bool hidden;
25+
final Widget initialChild;
2426

2527
final Map<String, String> headers;
2628

@@ -40,7 +42,9 @@ class WebviewScaffold extends StatefulWidget {
4042
this.withZoom,
4143
this.withLocalStorage,
4244
this.withLocalUrl,
43-
this.scrollBar})
45+
this.scrollBar,
46+
this.hidden = false,
47+
this.initialChild})
4448
: super(key: key);
4549

4650
@override
@@ -51,18 +55,30 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
5155
final webviewReference = new FlutterWebviewPlugin();
5256
Rect _rect;
5357
Timer _resizeTimer;
58+
StreamSubscription<WebViewStateChanged> _onStateChanged;
5459

5560
@override
5661
void initState() {
5762
super.initState();
5863
webviewReference.close();
64+
65+
if (widget.hidden) {
66+
_onStateChanged = webviewReference.onStateChanged.listen((WebViewStateChanged state) {
67+
if (state.type == WebViewState.finishLoad) {
68+
webviewReference.show();
69+
}
70+
});
71+
}
5972
}
6073

6174
@override
6275
void dispose() {
6376
super.dispose();
6477
_resizeTimer?.cancel();
6578
webviewReference.close();
79+
if (widget.hidden) {
80+
_onStateChanged.cancel();
81+
}
6682
webviewReference.dispose();
6783
}
6884

@@ -101,9 +117,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
101117
}
102118
}
103119
},
104-
child: const Center(
105-
child: CircularProgressIndicator(),
106-
),
120+
child: widget.initialChild ?? const Center(child: const CircularProgressIndicator()),
107121
),
108122
);
109123
}
@@ -115,7 +129,7 @@ class _WebviewPlaceholder extends SingleChildRenderObjectWidget {
115129
@required this.onRectChanged,
116130
Widget child,
117131
}) : super(key: key, child: child);
118-
132+
119133
final ValueChanged<Rect> onRectChanged;
120134

121135
@override
@@ -124,7 +138,7 @@ class _WebviewPlaceholder extends SingleChildRenderObjectWidget {
124138
onRectChanged: onRectChanged,
125139
);
126140
}
127-
141+
128142
@override
129143
void updateRenderObject(BuildContext context, _WebviewPlaceholderRender renderObject) {
130144
renderObject..onRectChanged = onRectChanged;

0 commit comments

Comments
 (0)