Skip to content

Commit b34d6bd

Browse files
committed
Add hidden and initialChild parameters to WebviewScaffold to allow the underlying Scaffold to be shown while waiting for page load
1 parent 7038b22 commit b34d6bd

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
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/

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
@@ -30,6 +30,13 @@ class MyApp extends StatelessWidget {
3030
),
3131
withZoom: true,
3232
withLocalStorage: true,
33+
hidden: true,
34+
initialChild: Container(
35+
color: Colors.redAccent,
36+
child: const Center(
37+
child: Text('Waiting.....'),
38+
),
39+
),
3340
)
3441
},
3542
);
@@ -121,6 +128,7 @@ class _MyHomePageState extends State<MyHomePage> {
121128

122129
_onStateChanged =
123130
flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) {
131+
124132
if (mounted) {
125133
setState(() {
126134
_history.add('onStateChanged: ${state.type} ${state.url}');

lib/src/webview_scaffold.dart

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class WebviewScaffold extends StatefulWidget {
2020
final bool withLocalStorage;
2121
final bool withLocalUrl;
2222
final bool scrollBar;
23+
final bool hidden;
24+
final Widget initialChild;
2325

2426
final Map<String, String> headers;
2527

@@ -39,7 +41,9 @@ class WebviewScaffold extends StatefulWidget {
3941
this.withZoom,
4042
this.withLocalStorage,
4143
this.withLocalUrl,
42-
this.scrollBar})
44+
this.scrollBar,
45+
this.hidden : false,
46+
this.initialChild})
4347
: super(key: key);
4448

4549
@override
@@ -50,17 +54,25 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
5054
final webviewReference = new FlutterWebviewPlugin();
5155
Rect _rect;
5256
Timer _resizeTimer;
57+
StreamSubscription<WebViewStateChanged> _onStateChanged;
5358

5459
@override
5560
void initState() {
5661
super.initState();
5762
webviewReference.close();
63+
64+
_onStateChanged = webviewReference.onStateChanged.listen((WebViewStateChanged state) {
65+
if (state.type == WebViewState.finishLoad) {
66+
webviewReference.show();
67+
}
68+
});
5869
}
5970

6071
@override
6172
void dispose() {
6273
super.dispose();
6374
webviewReference.close();
75+
_onStateChanged.cancel();
6476
webviewReference.dispose();
6577
}
6678

@@ -79,7 +91,8 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
7991
withZoom: widget.withZoom,
8092
withLocalStorage: widget.withLocalStorage,
8193
withLocalUrl: widget.withLocalUrl,
82-
scrollBar: widget.scrollBar);
94+
scrollBar: widget.scrollBar,
95+
hidden: widget.hidden);
8396
} else {
8497
final rect = _buildRect(context);
8598
if (_rect != rect) {
@@ -95,7 +108,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
95108
appBar: widget.appBar,
96109
persistentFooterButtons: widget.persistentFooterButtons,
97110
bottomNavigationBar: widget.bottomNavigationBar,
98-
body: const Center(child: const CircularProgressIndicator()));
111+
body: widget.initialChild ?? const Center(child: const CircularProgressIndicator()));
99112
}
100113

101114
Rect _buildRect(BuildContext context) {

0 commit comments

Comments
 (0)