share_webview.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // ignore_for_file: public_member_api_docs
  5. import 'dart:async';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter/services.dart';
  8. import 'package:sport/widgets/loading.dart';
  9. import 'package:sport/widgets/menu_share_bottom.dart';
  10. import 'package:webview_flutter/webview_flutter.dart';
  11. class WebViewSharePage extends StatefulWidget {
  12. final String url;
  13. final String hash;
  14. final bool openShare;
  15. WebViewSharePage(this.url, {this.hash, this.openShare = false});
  16. @override
  17. _WebViewSharePageState createState() => _WebViewSharePageState();
  18. }
  19. class _WebViewSharePageState extends State<WebViewSharePage> {
  20. final Completer<WebViewController> _controller =
  21. Completer<WebViewController>();
  22. bool _isLoading = true;
  23. @override
  24. void initState() {
  25. super.initState();
  26. if(widget.openShare == true){
  27. WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
  28. menuShareBottom(context, "Link",
  29. url: "http://shoes-web.hiyd.com/share",
  30. hasDownload: false,
  31. hash: widget.hash);
  32. });
  33. }
  34. }
  35. @override
  36. Widget build(BuildContext context) {
  37. return AnnotatedRegion<SystemUiOverlayStyle>(
  38. value: SystemUiOverlayStyle.dark,
  39. child: Scaffold(
  40. backgroundColor: Color(0xff241d19),
  41. // We're using a Builder here so we have a context that is below the Scaffold
  42. // to allow calling Scaffold.of(context) so we can show a snackbar.
  43. body: SafeArea(
  44. child: LoadingWidget(
  45. loading: _isLoading,
  46. child: Stack(
  47. children: <Widget>[
  48. Container(
  49. height: MediaQuery.of(context).size.height * 1,
  50. child: WebView(
  51. initialUrl:
  52. 'http://shoes-web.hiyd.com/share?h=${widget.hash}&t=${DateTime.now()}',
  53. // "http://172.16.14.23:9090/share?h=${widget.hash}&t=${DateTime.now()}",
  54. javascriptMode: JavascriptMode.unrestricted,
  55. onWebViewCreated: (WebViewController webViewController) {
  56. _controller.complete(webViewController);
  57. },
  58. // ignore: prefer_collection_literals
  59. javascriptChannels: <JavascriptChannel>[
  60. _toasterJavascriptChannel(context),
  61. ].toSet(),
  62. onPageStarted: (String url) {
  63. print('Page started loading: $url');
  64. setState(() {
  65. _isLoading = true;
  66. });
  67. },
  68. onPageFinished: (String url) {
  69. print('Page finished loading: $url');
  70. setState(() {
  71. _isLoading = false;
  72. });
  73. },
  74. gestureNavigationEnabled: true,
  75. ),
  76. ),
  77. Positioned(
  78. left: 12.0,
  79. top: 11.0,
  80. child: Container(
  81. child: InkWell(
  82. child:
  83. Image.asset("lib/assets/img/topbar_return_white.png"),
  84. onTap: () {
  85. Navigator.pop(context);
  86. },
  87. ),
  88. // color: Colors.red,
  89. ),
  90. ),
  91. Positioned(
  92. right: 12.0,
  93. top: 11.0,
  94. child: Container(
  95. child: InkWell(
  96. child:
  97. Image.asset("lib/assets/img/bbs_icon_share_white.png"),
  98. onTap: () {
  99. menuShareBottom(context, "Link",
  100. url: "http://shoes-web.hiyd.com/share",
  101. hasDownload: false,
  102. hash: widget.hash);
  103. },
  104. ),
  105. // color: Colors.red,
  106. ),
  107. ),
  108. ],
  109. ),
  110. ),
  111. )
  112. // floatingActionButton: favoriteButton(),
  113. ));
  114. }
  115. JavascriptChannel _toasterJavascriptChannel(BuildContext context) {
  116. return JavascriptChannel(
  117. name: 'Toaster',
  118. onMessageReceived: (JavascriptMessage message) {
  119. Scaffold.of(context).showSnackBar(
  120. SnackBar(content: Text(message.message)),
  121. );
  122. });
  123. }
  124. Widget favoriteButton() {
  125. return FutureBuilder<WebViewController>(
  126. future: _controller.future,
  127. builder: (BuildContext context,
  128. AsyncSnapshot<WebViewController> controller) {
  129. if (controller.hasData) {
  130. return FloatingActionButton(
  131. onPressed: () async {
  132. final String url = await controller.data.currentUrl();
  133. Scaffold.of(context).showSnackBar(
  134. SnackBar(content: Text('Favorited $url')),
  135. );
  136. },
  137. child: const Icon(Icons.favorite),
  138. );
  139. }
  140. return Container();
  141. });
  142. }
  143. }