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

188 lines
5.1 KiB

  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:zhiying_base_widget/pages/launch_page/launch_page.dart';
  6. import 'package:zhiying_comm/util/log/let_log.dart';
  7. import 'package:zhiying_comm/util/empty_util.dart';
  8. import 'package:url_launcher/url_launcher.dart';
  9. import 'package:webview_flutter/webview_flutter.dart';
  10. import 'package:zhiying_comm/util/shared_prefe_util.dart';
  11. import 'package:zhiying_comm/util/global_config.dart';
  12. class BaseWebview extends StatefulWidget {
  13. final Map<String, dynamic> model;
  14. const BaseWebview(this.model, {Key key}) : super(key: key);
  15. @override
  16. _BaseWebviewState createState() => _BaseWebviewState();
  17. }
  18. class _BaseWebviewState extends State<BaseWebview> {
  19. String _url;
  20. String _title;
  21. WebViewController _webViewController;
  22. bool _isiOSReview = false;
  23. double progress = 0.0;
  24. Timer timer;
  25. @override
  26. void initState() {
  27. _url = widget.model['url'] ?? '';
  28. _settingIosReview();
  29. super.initState();
  30. }
  31. @override
  32. void dispose() {
  33. // TODO: implement dispose
  34. timer?.cancel();
  35. super.dispose();
  36. }
  37. loadData() {
  38. print("加载数据");
  39. progress = 0.0;
  40. if(timer?.isActive??false){
  41. timer?.cancel();
  42. }
  43. timer = Timer.periodic(Duration(milliseconds: 10), (timer) {
  44. if (progress > 0.8) {
  45. progress = progress + 0.01;
  46. } else if (progress > 0.7) {
  47. progress = progress + 0.002;
  48. } else if (progress > 0.5) {
  49. progress += 0.005;
  50. } else {
  51. progress = progress + 0.01;
  52. }
  53. if (progress > 1.1) {
  54. timer.cancel();
  55. }
  56. setState(() {});
  57. });
  58. }
  59. void _settingIosReview() async {
  60. String is_ios_review = await SharedPreferencesUtil.getStringValue(GlobalConfig.IS_IOS_REVIEW, defaultVal: '0');
  61. setState(() {
  62. if (is_ios_review == '1') {
  63. _isiOSReview = true;
  64. print(_isiOSReview);
  65. }
  66. });
  67. }
  68. @override
  69. Widget build(BuildContext context) {
  70. return Scaffold(
  71. appBar: _createNav(),
  72. backgroundColor: Colors.white,
  73. body: Stack(
  74. children: <Widget>[
  75. WebView(
  76. initialUrl: _url,
  77. javascriptMode: JavascriptMode.unrestricted,
  78. onWebViewCreated: (WebViewController webViewController) {
  79. _webViewController = webViewController;
  80. loadData();
  81. },
  82. navigationDelegate: (NavigationRequest request) async {
  83. // 解决Android的拼多多webview 转发的问题
  84. if (Platform.isAndroid) {
  85. String url = request?.url?.toString();
  86. if (!EmptyUtil.isEmpty(url) && !url.startsWith('https://') && !url.startsWith('http://')) {
  87. Logger.log('navigation url = $url');
  88. // if(await canLaunch(url)){
  89. // await launch(url);
  90. // }
  91. return NavigationDecision.prevent;
  92. }
  93. }
  94. return NavigationDecision.navigate;
  95. },
  96. onPageStarted: (String url) {
  97. print('Page started loading: $url');
  98. },
  99. onPageFinished: (String url) {
  100. if(progress<0.8){
  101. progress = 0.8;
  102. }
  103. setState(() {});
  104. print('Page finished loading: $url');
  105. _webViewController.getTitle().then((title) {
  106. _title = title;
  107. setState(() {});
  108. });
  109. },
  110. gestureNavigationEnabled: true,
  111. ),
  112. progress <= 1.0 ? _createProgress() : Container(),
  113. ],
  114. ),
  115. );
  116. }
  117. // 导航栏
  118. Widget _createNav() {
  119. return CupertinoNavigationBar(
  120. border: Border(
  121. bottom: BorderSide(
  122. width: 0.0, // One physical pixel.
  123. style: BorderStyle.none,
  124. ),
  125. ),
  126. backgroundColor: Colors.white,
  127. leading: Navigator.canPop(context)
  128. ? GestureDetector(
  129. child: Container(
  130. padding: EdgeInsets.zero,
  131. child: Icon(
  132. Icons.arrow_back_ios,
  133. size: 20,
  134. ),
  135. ),
  136. onTap: () {
  137. if (Navigator.canPop(context)) {
  138. Navigator.pop(context);
  139. }
  140. },
  141. )
  142. : Container(),
  143. middle: Text(
  144. _title ?? '',
  145. maxLines: 1,
  146. style: TextStyle(
  147. fontSize: 15,
  148. color: Color(0xff333333),
  149. ),
  150. ),
  151. trailing: GestureDetector(
  152. child: _isiOSReview
  153. ? Container()
  154. : Icon(
  155. Icons.refresh,
  156. size: 20,
  157. ),
  158. onTap: () {
  159. _webViewController.reload();
  160. loadData();
  161. },
  162. ),
  163. );
  164. }
  165. _createProgress() {
  166. return Container(
  167. constraints: BoxConstraints(maxHeight: 2),
  168. child: LinearProgressIndicator(
  169. backgroundColor: Colors.white,
  170. valueColor: AlwaysStoppedAnimation<Color>(Colors.red),
  171. value: progress,
  172. ),
  173. );
  174. }
  175. }