基础组件库
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

195 linhas
5.4 KiB

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