CommonWebPage.dart 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:webview_flutter/webview_flutter.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. @override
  17. void initState() {
  18. super.initState();
  19. }
  20. @override
  21. Widget build(BuildContext context) {
  22. WebViewController webViewController = WebViewController()
  23. ..setUserAgent("Android OSC")
  24. ..setJavaScriptMode(JavaScriptMode.unrestricted)
  25. ..setBackgroundColor(const Color(0x00000000))
  26. ..setNavigationDelegate(
  27. NavigationDelegate(
  28. onProgress: (int progress) {
  29. // Update loading bar.
  30. },
  31. onPageStarted: (String url) {},
  32. onPageFinished: (String url) {},
  33. onWebResourceError: (WebResourceError error) {},
  34. ),
  35. )
  36. ..loadRequest(Uri.parse(widget.url!));
  37. List<Widget> titleContent = [];
  38. titleContent.add(Text(
  39. widget.title!,
  40. style: TextStyle(color: Colors.white),
  41. ));
  42. if (loading) {
  43. titleContent.add(CupertinoActivityIndicator());
  44. }
  45. titleContent.add(Container(width: 50.0));
  46. return WebViewWidget(
  47. controller: webViewController,
  48. );
  49. }
  50. }