share_webview.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/menu_share_bottom.dart';
  9. import 'package:webview_flutter/webview_flutter.dart';
  10. class WebViewSharePage extends StatefulWidget {
  11. final String url;
  12. final String hash;
  13. WebViewSharePage(this.url, {this.hash});
  14. @override
  15. _WebViewSharePageState createState() => _WebViewSharePageState();
  16. }
  17. class _WebViewSharePageState extends State<WebViewSharePage> {
  18. final Completer<WebViewController> _controller =
  19. Completer<WebViewController>();
  20. @override
  21. Widget build(BuildContext context) {
  22. return AnnotatedRegion<SystemUiOverlayStyle>(
  23. value: SystemUiOverlayStyle.dark,
  24. child: Scaffold(
  25. backgroundColor: Color(0xff241d19),
  26. // We're using a Builder here so we have a context that is below the Scaffold
  27. // to allow calling Scaffold.of(context) so we can show a snackbar.
  28. body: SafeArea(
  29. child: Stack(
  30. children: <Widget>[
  31. Container(
  32. height: MediaQuery.of(context).size.height * 1,
  33. child: WebView(
  34. initialUrl:
  35. // 'http://shoes-web.hiyd.com/share?h=${widget.hash}&t=${DateTime.now()}',
  36. "http://172.16.14.23:9090/share?h=${widget.hash}&t=${DateTime.now()}",
  37. javascriptMode: JavascriptMode.unrestricted,
  38. onWebViewCreated: (WebViewController webViewController) {
  39. _controller.complete(webViewController);
  40. },
  41. // ignore: prefer_collection_literals
  42. javascriptChannels: <JavascriptChannel>[
  43. _toasterJavascriptChannel(context),
  44. ].toSet(),
  45. onPageStarted: (String url) {
  46. print('Page started loading: $url');
  47. },
  48. onPageFinished: (String url) {
  49. print('Page finished loading: $url');
  50. },
  51. gestureNavigationEnabled: true,
  52. ),
  53. ),
  54. Positioned(
  55. left: 12.0,
  56. top: 11.0,
  57. child: Container(
  58. child: InkWell(
  59. child:
  60. Image.asset("lib/assets/img/topbar_return_white.png"),
  61. onTap: () {
  62. Navigator.pop(context);
  63. },
  64. ),
  65. // color: Colors.red,
  66. ),
  67. ),
  68. Positioned(
  69. right: 12.0,
  70. top: 11.0,
  71. child: Container(
  72. child: InkWell(
  73. child:
  74. Image.asset("lib/assets/img/bbs_icon_share_white.png"),
  75. onTap: () {
  76. menuShareBottom(context, "Link",
  77. url: "http://shoes-web.hiyd.com/share",
  78. hasDownload: false,
  79. hash: widget.hash);
  80. },
  81. ),
  82. // color: Colors.red,
  83. ),
  84. ),
  85. ],
  86. ),
  87. )
  88. // floatingActionButton: favoriteButton(),
  89. ));
  90. }
  91. JavascriptChannel _toasterJavascriptChannel(BuildContext context) {
  92. return JavascriptChannel(
  93. name: 'Toaster',
  94. onMessageReceived: (JavascriptMessage message) {
  95. Scaffold.of(context).showSnackBar(
  96. SnackBar(content: Text(message.message)),
  97. );
  98. });
  99. }
  100. Widget favoriteButton() {
  101. return FutureBuilder<WebViewController>(
  102. future: _controller.future,
  103. builder: (BuildContext context,
  104. AsyncSnapshot<WebViewController> controller) {
  105. if (controller.hasData) {
  106. return FloatingActionButton(
  107. onPressed: () async {
  108. final String url = await controller.data.currentUrl();
  109. Scaffold.of(context).showSnackBar(
  110. SnackBar(content: Text('Favorited $url')),
  111. );
  112. },
  113. child: const Icon(Icons.favorite),
  114. );
  115. }
  116. return Container();
  117. });
  118. }
  119. }