YSUrlPage.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_webview_pro/webview_flutter.dart';
  4. import '../base/YSNetWorking.dart';
  5. import '../base/YSTools.dart';
  6. class YSUrlPage extends StatefulWidget {
  7. final String url;
  8. final String title;
  9. final Map wiki;
  10. final bool isCollection;
  11. const YSUrlPage({Key key, this.url, this.title = '', this.wiki, this.isCollection}) : super(key: key);
  12. @override
  13. _YSUrlPageState createState() => _YSUrlPageState();
  14. }
  15. class _YSUrlPageState extends State<YSUrlPage> {
  16. bool _collection = false;
  17. WebViewController _webViewController;
  18. @override
  19. void initState() {
  20. if(widget.isCollection!=null){
  21. _collection = widget.isCollection;
  22. }
  23. super.initState();
  24. }
  25. @override
  26. Widget build(BuildContext context) {
  27. return WillPopScope(
  28. onWillPop: () async{
  29. bool canBack = await _webViewController.canGoBack();
  30. if(canBack){
  31. _webViewController.goBack();
  32. }else{
  33. Navigator.pop(context);
  34. }
  35. return false;
  36. },
  37. child: Scaffold(
  38. backgroundColor: Colors.white,
  39. body: Stack(
  40. children: [
  41. Container(
  42. height: MediaQuery.of(context).padding.top+70,
  43. width: MediaQuery.of(context).size.width,
  44. color: Color(0xFFDB5278),
  45. ),
  46. Container(
  47. margin: EdgeInsets.only(top: 20),
  48. padding: EdgeInsets.only(left: 15,right: 15),
  49. height: 30+MediaQuery.of(context).padding.top,
  50. width: MediaQuery.of(context).size.width,
  51. child: Row(
  52. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  53. children: [
  54. Container(
  55. height: 30,
  56. width: 20,
  57. margin: EdgeInsets.only(right: 5),
  58. child: CupertinoButton(
  59. padding: EdgeInsets.all(0),
  60. child: Icon(Icons.arrow_back_ios,size: 20,color: Colors.white,),
  61. onPressed: () async{
  62. bool canBack = await _webViewController.canGoBack();
  63. if(canBack){
  64. _webViewController.goBack();
  65. }else{
  66. Navigator.pop(context);
  67. }
  68. },
  69. ),
  70. ),
  71. // Container(
  72. // height: 30,
  73. // width: 20,
  74. // child: CupertinoButton(
  75. // padding: EdgeInsets.all(0),
  76. // child: Icon(Icons.close,size: 20,color: Colors.white,),
  77. // onPressed: (){
  78. // Navigator.pop(context);
  79. // },
  80. // ),
  81. // ),
  82. Container(
  83. width: ysWidth(context)-150,
  84. child: Text(widget.title,style: TextStyle(color: Colors.white,fontSize: 16,decoration: TextDecoration.none,fontWeight: FontWeight.w600),
  85. textAlign: TextAlign.center,maxLines: 1,),
  86. ),
  87. Container(
  88. width: 55,
  89. height: 30,
  90. alignment: Alignment.centerRight,
  91. child: widget.isCollection!=null?GestureDetector(
  92. child: Image(height: 15,width: 15,image: AssetImage(_collection==false?'lib/images/uncollection.png':'lib/images/collection.png'),),
  93. onTap: (){
  94. _postCollectionData();
  95. },
  96. ):Container(),
  97. ),
  98. ],
  99. ),
  100. ),
  101. Container(
  102. margin: EdgeInsets.only(top: 50+MediaQuery.of(context).padding.top),
  103. height: MediaQuery.of(context).size.height-MediaQuery.of(context).padding.top-50,
  104. width: MediaQuery.of(context).size.width,
  105. decoration: BoxDecoration(
  106. color: Color(0xFFFAF8F5),
  107. borderRadius: BorderRadius.only(topLeft: Radius.circular(20),topRight: Radius.circular(20))
  108. ),
  109. child: Container(
  110. width: MediaQuery.of(context).size.width,
  111. height: MediaQuery.of(context).size.height,
  112. child: WebView(
  113. initialUrl: widget.url,
  114. javascriptMode: JavascriptMode.unrestricted,
  115. onWebViewCreated: (value){
  116. _webViewController = value;
  117. },
  118. ),
  119. ),
  120. )
  121. ],
  122. ),
  123. ),
  124. );
  125. }
  126. _postCollectionData() async{
  127. Map dict = await ysRequestHttpNoLoading(context, requestType.put, 'wike/favorite', {'wike_id':widget.wiki['id']});
  128. if(dict!=null){
  129. _collection = dict['data']['is_favorite'];
  130. setState(() {});
  131. }
  132. }
  133. }