Created
September 30, 2019 05:39
-
-
Save fredriccliver/29d40815d31e6d1e8534864dfbb4ab5e to your computer and use it in GitHub Desktop.
getting Dom Element from flutter webview
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 'dart:async'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:html/parser.dart'; | |
import 'package:webview_flutter/webview_flutter.dart'; | |
import 'package:provider/provider.dart'; | |
import 'package:html/dom_parsing.dart'; | |
void main() => runApp(MyApp()); | |
class UrlManager extends ChangeNotifier { | |
String _url = ''; | |
String get url => _url; | |
void updateUrl(String newUrl) { | |
_url = newUrl; | |
print('new url is : ' + newUrl); | |
} | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
@override | |
Widget build(BuildContext context) { | |
// WebViewController _webviewController; | |
final Completer<WebViewController> webviewController = | |
Completer<WebViewController>(); | |
String HOME = 'https://google.com'; | |
final urlBarController = TextEditingController(text: ''); | |
String CurrentUrl = ''; | |
return ChangeNotifierProvider( | |
builder: (_) => UrlManager(), | |
child: MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: SafeArea( | |
child: Center( | |
child: Column( | |
children: <Widget>[ | |
Container( | |
padding: const EdgeInsets.all(8.0), | |
child: CupertinoTextField( | |
style: TextStyle(color: CupertinoColors.inactiveGray), | |
placeholder: "URL", | |
enabled: true, | |
controller: urlBarController, | |
), | |
), | |
Consumer<UrlManager>(builder: (context, urlmanager, _) { | |
return Expanded( | |
child: WebView( | |
initialUrl: HOME, | |
javascriptMode: JavascriptMode.unrestricted, | |
onWebViewCreated: (WebViewController cont) { | |
print('webview was created.'); | |
webviewController.complete(cont); | |
urlBarController.text = cont.currentUrl().toString(); | |
}, | |
onPageFinished: (String url) { | |
print('url changed : ' + url); | |
urlBarController.text = url; | |
// Scaffold.of(context).showSnackBar(SnackBar(content: Text(url),)); | |
}, | |
), | |
); | |
}), | |
new NavigationControls(webviewController.future), | |
], | |
), | |
), | |
), | |
), | |
), | |
); | |
} | |
} | |
class NavigationControls extends StatelessWidget { | |
const NavigationControls(this._webViewControllerFuture) | |
: assert(_webViewControllerFuture != null); | |
final Future<WebViewController> _webViewControllerFuture; | |
@override | |
Widget build(BuildContext context) { | |
return FutureBuilder<WebViewController>( | |
future: _webViewControllerFuture, | |
builder: | |
(BuildContext context, AsyncSnapshot<WebViewController> snapshot) { | |
final bool webViewReady = | |
snapshot.connectionState == ConnectionState.done; | |
final WebViewController controller = snapshot.data; | |
return Row( | |
children: <Widget>[ | |
CupertinoButton( | |
child: Text('Back'), | |
onPressed: () { | |
controller.goBack(); | |
}, | |
), | |
CupertinoButton( | |
child: Text('Forward'), | |
onPressed: () { | |
controller.goForward(); | |
}, | |
), | |
CupertinoButton( | |
child: Text('Refresh'), | |
onPressed: () { | |
controller.reload(); | |
}, | |
), | |
Spacer(), | |
CupertinoButton( | |
child: Text('Scrap'), | |
onPressed: () async { | |
String docu = await controller.evaluateJavascript('document.documentElement.innerHTML') as String; | |
// print(docu); | |
var dom = parse(docu); | |
print(dom.getElementsByTagName('title')[0].['text']); | |
}, | |
), | |
], | |
); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment