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

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