Skip to content

Instantly share code, notes, and snippets.

@edex96
Created May 3, 2019 00:50
Show Gist options
  • Save edex96/ff614993af20c1a58d20ea18ca3a19ee to your computer and use it in GitHub Desktop.
Save edex96/ff614993af20c1a58d20ea18ca3a19ee to your computer and use it in GitHub Desktop.
second screen
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