Created
May 3, 2019 00:50
-
-
Save edex96/ff614993af20c1a58d20ea18ca3a19ee to your computer and use it in GitHub Desktop.
second screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:webview_flutter/webview_flutter.dart'; | |
import 'dart:async'; | |
class Web extends StatefulWidget { | |
final String url,title; | |
final bool isAppBarVisible; | |
Web({Key key, this.url, this.title, this.isAppBarVisible}) : super(key: key); | |
@override | |
_WebState createState() => _WebState(); | |
} | |
class _WebState extends State<Web> { | |
Completer<WebViewController> _controller = Completer<WebViewController>(); | |
/// ---------------------------------------------- | |
/// Linear Progression Indicator | |
/// ---------------------------------------------- | |
Widget _lpi = PreferredSize( | |
preferredSize: Size(double.infinity, 7), | |
child: LinearProgressIndicator( | |
backgroundColor: Colors.black, | |
valueColor: AlwaysStoppedAnimation<Color>(Colors.yellow) | |
), | |
); | |
navigationControls({Future<WebViewController> webViewControllerFuture, Icon icon, bool goback}){ | |
navigate(BuildContext context, WebViewController controller, {bool goBack}) async { | |
bool canGoForward = await controller.canGoForward(); | |
bool canGoBack = await controller.canGoBack(); | |
bool canNavigate = goBack ? canGoBack : canGoForward; | |
if (canNavigate) { | |
goBack ? controller.goBack() : controller.goForward(); | |
} | |
else { | |
if(!canGoBack && goback){ | |
Navigator.of(context).pop(); | |
} | |
else if(!canGoForward){ | |
Scaffold.of(context).showSnackBar( | |
SnackBar(content: Text("No forward history!", textAlign: TextAlign.center,)) | |
); | |
} | |
} | |
} | |
return FutureBuilder<WebViewController>( | |
future: webViewControllerFuture, | |
builder: (BuildContext context, AsyncSnapshot<WebViewController> snapshot) { | |
final bool webViewReady = snapshot.connectionState == ConnectionState.done; | |
final WebViewController controller = snapshot.data; | |
return IconButton( | |
icon: icon, | |
onPressed: webViewReady | |
? () => navigate(context, controller, goBack: goback) | |
: null, | |
); | |
}, | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: (widget.isAppBarVisible == null) | |
? null | |
: AppBar( | |
title: Text(widget.title), | |
centerTitle: true, | |
leading: navigationControls( | |
webViewControllerFuture: _controller.future, | |
icon: const Icon(Icons.arrow_back_ios), | |
goback: true, | |
), | |
actions: <Widget>[ | |
navigationControls( | |
webViewControllerFuture: _controller.future, | |
icon: const Icon(Icons.arrow_forward_ios), | |
goback: false, | |
), | |
], | |
bottom: _lpi, | |
), | |
body: WebView( | |
initialUrl: widget.url, | |
onWebViewCreated: (WebViewController webViewController) { | |
_controller.complete(webViewController); | |
}, | |
onPageFinished: (val){ | |
setState(() { | |
_lpi = null; | |
}); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment