CommonWebPage.dart 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
  4. //公共的WebView页面,需要标题和URL参数
  5. class CommonWebPage extends StatefulWidget {
  6. String? title;
  7. String? url;
  8. CommonWebPage({Key? key, this.title, this.url}) : super(key: key);
  9. @override
  10. State<StatefulWidget> createState() {
  11. return CommonWebPageState();
  12. }
  13. }
  14. class CommonWebPageState extends State<CommonWebPage> {
  15. bool loading = true;
  16. final flutterWebViewPlugin = FlutterWebviewPlugin();
  17. @override
  18. void initState() {
  19. super.initState();
  20. // 监听WebView的加载事件
  21. flutterWebViewPlugin.onStateChanged.listen((state) {
  22. // if (state.type == WebViewState.finishLoad) {
  23. // // 加载完成
  24. // setState(() {
  25. // loading = false;
  26. // });
  27. // } else if (state.type == WebViewState.startLoad) {
  28. // setState(() {
  29. // loading = true;
  30. // });
  31. // }
  32. });
  33. flutterWebViewPlugin.onUrlChanged.listen((url) {
  34. setState(() {
  35. loading = false;
  36. });
  37. });
  38. }
  39. @override
  40. Widget build(BuildContext context) {
  41. List<Widget> titleContent = [];
  42. titleContent.add(Text(
  43. widget.title!,
  44. style: TextStyle(color: Colors.white),
  45. ));
  46. if (loading) {
  47. titleContent.add(CupertinoActivityIndicator());
  48. }
  49. titleContent.add(Container(width: 50.0));
  50. return WebviewScaffold(
  51. url: widget.url!,
  52. appBar: AppBar(
  53. title: Row(
  54. mainAxisAlignment: MainAxisAlignment.center,
  55. children: titleContent,
  56. ),
  57. iconTheme: IconThemeData(color: Colors.white),
  58. ),
  59. withZoom: true,
  60. withLocalStorage: true,
  61. withJavascript: true,
  62. );
  63. }
  64. }