基础组件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base_webview.dart 2.5 KiB

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:webview_flutter/webview_flutter.dart';
  4. class BaseWebview extends StatefulWidget {
  5. final Map<String, dynamic> model;
  6. const BaseWebview(this.model, {Key key}) : super(key: key);
  7. @override
  8. _BaseWebviewState createState() => _BaseWebviewState();
  9. }
  10. class _BaseWebviewState extends State<BaseWebview> {
  11. String _url;
  12. String _title;
  13. WebViewController _webViewController;
  14. @override
  15. void initState() {
  16. _url = widget.model['url'] ?? '';
  17. super.initState();
  18. }
  19. @override
  20. Widget build(BuildContext context) {
  21. return Scaffold(
  22. appBar: _createNav(),
  23. backgroundColor: Colors.white,
  24. body: WebView(
  25. initialUrl: _url,
  26. javascriptMode: JavascriptMode.unrestricted,
  27. onWebViewCreated: (WebViewController webViewController) {
  28. _webViewController = webViewController;
  29. },
  30. navigationDelegate: (NavigationRequest request) {
  31. return NavigationDecision.navigate;
  32. },
  33. onPageStarted: (String url) {
  34. print('Page started loading: $url');
  35. },
  36. onPageFinished: (String url) {
  37. print('Page finished loading: $url');
  38. _webViewController.getTitle().then((title) {
  39. _title = title;
  40. setState(() {});
  41. });
  42. },
  43. gestureNavigationEnabled: true,
  44. ),
  45. );
  46. }
  47. // 导航栏
  48. Widget _createNav() {
  49. return CupertinoNavigationBar(
  50. border: Border(
  51. bottom: BorderSide(
  52. width: 0.0, // One physical pixel.
  53. style: BorderStyle.none,
  54. ),
  55. ),
  56. backgroundColor: Colors.white,
  57. leading: Navigator.canPop(context)
  58. ? GestureDetector(
  59. child: Container(
  60. padding: EdgeInsets.zero,
  61. child: Icon(
  62. Icons.arrow_back_ios,
  63. size: 20,
  64. ),
  65. ),
  66. onTap: () {
  67. if (Navigator.canPop(context)) {
  68. Navigator.pop(context);
  69. }
  70. },
  71. )
  72. : Container(),
  73. middle: Text(
  74. _title ?? '',
  75. maxLines: 1,
  76. style: TextStyle(
  77. fontSize: 15,
  78. color: Color(0xff333333),
  79. ),
  80. ),
  81. trailing: GestureDetector(
  82. child: Icon(
  83. Icons.refresh,
  84. size: 20,
  85. ),
  86. onTap: () {
  87. _webViewController.reload();
  88. },
  89. ),
  90. );
  91. }
  92. }