11import 'package:flutter/cupertino.dart' ;
22import 'package:flutter/material.dart' ;
33import 'package:flutter_spinkit/flutter_spinkit.dart' ;
4+ import 'package:flutter_webview_plugin/flutter_webview_plugin.dart' ;
45import 'package:gsy_github_app_flutter/common/localization/default_localizations.dart' ;
56import 'package:gsy_github_app_flutter/common/style/gsy_style.dart' ;
6- import 'package:gsy_github_app_flutter/widget/gsy_common_option_widget.dart' ;
7- import 'package:webview_flutter/webview_flutter.dart' ;
8-
97
108class LoginWebView extends StatefulWidget {
119 final String url;
@@ -18,6 +16,8 @@ class LoginWebView extends StatefulWidget {
1816}
1917
2018class _LoginWebViewState extends State <LoginWebView > {
19+ final flutterWebViewPlugin = new FlutterWebviewPlugin ();
20+
2121 _renderTitle () {
2222 if (widget.url == null || widget.url.length == 0 ) {
2323 return new Text (widget.title);
@@ -31,66 +31,69 @@ class _LoginWebViewState extends State<LoginWebView> {
3131 overflow: TextOverflow .ellipsis,
3232 ),
3333 )),
34- GSYCommonOptionWidget (url: widget.url),
3534 ]);
3635 }
3736
38- final FocusNode focusNode = new FocusNode ();
37+ renderLoading () {
38+ return new Center (
39+ child: new Container (
40+ width: 200.0 ,
41+ height: 200.0 ,
42+ padding: new EdgeInsets .all (4.0 ),
43+ child: new Row (
44+ mainAxisAlignment: MainAxisAlignment .center,
45+ children: < Widget > [
46+ new SpinKitDoubleBounce (color: Theme .of (context).primaryColor),
47+ new Container (width: 10.0 ),
48+ new Container (
49+ child: new Text (GSYLocalizations .i18n (context).loading_text,
50+ style: GSYConstant .middleText)),
51+ ],
52+ ),
53+ ),
54+ );
55+ }
3956
40- bool isLoading = true ;
57+ @override
58+ void initState () {
59+ super .initState ();
60+ flutterWebViewPlugin.onStateChanged.listen ((WebViewStateChanged state) {
61+ if (mounted) {
62+ if (state.type == WebViewState .shouldStart) {
63+ print ("shouldStart ${state .url }" );
64+ if (state.url != null &&
65+ state.url.startsWith ("gsygithubapp://authed" )) {
66+ var code = Uri .parse (state.url).queryParameters["code" ];
67+ print ("code ${code }" );
68+ flutterWebViewPlugin.reloadUrl ("about:blank" );
69+ Navigator .of (context).pop (code);
70+ }
71+ }
72+ }
73+ });
74+ }
75+
76+ @override
77+ void dispose () {
78+ flutterWebViewPlugin.dispose ();
79+ super .dispose ();
80+ }
4181
4282 @override
4383 Widget build (BuildContext context) {
4484 return Scaffold (
45- appBar: new AppBar (
46- title: _renderTitle (),
47- ),
4885 body: new Stack (
4986 children: < Widget > [
50- TextField (
51- focusNode: focusNode,
87+ WebviewScaffold (
88+ appBar: new AppBar (
89+ title: _renderTitle (),
90+ ),
91+ //invalidUrlRegex: "gsygithubapp://authed",
92+ initialChild: renderLoading (),
93+ url: widget.url,
5294 ),
53- WebView (
54- initialUrl: widget.url,
55- javascriptMode: JavascriptMode .unrestricted,
56- initialMediaPlaybackPolicy: AutoMediaPlaybackPolicy .always_allow,
57- navigationDelegate: (NavigationRequest navigation) {
58- if (navigation.url != null &&
59- navigation.url.startsWith ("gsygithubapp://authed" )) {
60- var code = Uri .parse (navigation.url).queryParameters["code" ];
61- print ("code ${code }" );
62- Navigator .of (context).pop (code);
63- return NavigationDecision .prevent;
64- }
65- return NavigationDecision .navigate;
66- },
67- onPageFinished: (_) {
68- setState (() {
69- isLoading = false ;
70- });
71- }),
72- if (isLoading)
73- new Center (
74- child: new Container (
75- width: 200.0 ,
76- height: 200.0 ,
77- padding: new EdgeInsets .all (4.0 ),
78- child: new Row (
79- mainAxisAlignment: MainAxisAlignment .center,
80- children: < Widget > [
81- new SpinKitDoubleBounce (
82- color: Theme .of (context).primaryColor),
83- new Container (width: 10.0 ),
84- new Container (
85- child: new Text (
86- GSYLocalizations .i18n (context).loading_text,
87- style: GSYConstant .middleText)),
88- ],
89- ),
90- ),
91- )
9295 ],
9396 ),
9497 );
9598 }
96- }
99+ }
0 commit comments