home_page.dart 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import 'package:flutter/material.dart';
  2. import 'package:webview_flutter/webview_flutter.dart';
  3. class HomePage extends StatefulWidget {
  4. String url;
  5. HomePage({Key key, this.url}) : super(key: key);
  6. @override
  7. _HomePageState createState() => _HomePageState();
  8. }
  9. class _HomePageState extends State<HomePage> {
  10. bool _isLoading = true;
  11. @override
  12. Widget build(BuildContext context) {
  13. return Stack(
  14. children: <Widget>[
  15. WebView(
  16. initialUrl: widget.url,
  17. javascriptMode: JavascriptMode.unrestricted,
  18. onWebViewCreated: (WebViewController web) {
  19. web.canGoBack().then((res) {
  20. // print(res); // 是否能返回上一级
  21. });
  22. web.currentUrl().then((url) {
  23. // print(url); // 返回当前url
  24. });
  25. web.canGoForward().then((res) {
  26. // print(res); //是否能前进
  27. });
  28. },
  29. onPageFinished: (String value) {
  30. // 返回当前url
  31. // print(value);
  32. setState(() {
  33. _isLoading = false;
  34. });
  35. },
  36. ),
  37. _loading()
  38. ],
  39. );
  40. }
  41. _loading() {
  42. return _isLoading == true
  43. ? Container(
  44. decoration: const BoxDecoration(color: Colors.white),
  45. child: const Center(
  46. child: CircularProgressIndicator(),
  47. ),
  48. )
  49. : const Text('');
  50. }
  51. }