基础组件库
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 3.2 KiB

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