import 'dart:io'; import 'dart:ui'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/activity.dart'; import 'package:huixiang/retrofit/data/article.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/view_widget/my_appbar.dart'; import 'package:huixiang/view_widget/share_dialog.dart'; import 'package:huixiang/view_widget/tips_dialog.dart'; import 'package:huixiang/web/web_view/comment_list.dart'; import 'package:huixiang/web/web_view/input_comment.dart'; import 'package:huixiang/web/web_view/web_content.dart'; import 'package:huixiang/web/web_view/web_header.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sharesdk_plugin/sharesdk_defines.dart'; import 'package:sharesdk_plugin/sharesdk_interface.dart'; import 'package:sharesdk_plugin/sharesdk_map.dart'; import 'package:webview_flutter/webview_flutter.dart'; import 'package:flutter/cupertino.dart'; class WebPage extends StatefulWidget { final Map arguments; ///富文本 文章 活动 WebPage({this.arguments}); @override State createState() { return _WebPage(); } } class _WebPage extends State with WidgetsBindingObserver { ApiService apiService; int commentTotal = 0; String hintText = S.current.liuxianinjingcaidepinglunba; var commentFocus = FocusNode(); String parenId = "0"; Activity activity; Article article; final double fontSize = 16.sp; final GlobalKey commentKey = GlobalKey(); final GlobalKey inputKey = GlobalKey(); final ScrollController scrollController = ScrollController(); final TextEditingController commentTextController = TextEditingController(); @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView(); queryHtml(); } bool isKeyBoardShow = false; @override void didChangeMetrics() { WidgetsBinding.instance.addPostFrameCallback((_) { if (MediaQuery.of(context).viewInsets.bottom == 0) { if (isKeyBoardShow) { FocusScope.of(context).requestFocus(FocusNode()); setState(() { hintText = S.current.liuxianinjingcaidepinglunba; isKeyBoardShow = false; }); } } else { setState(() { isKeyBoardShow = true; }); } }); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); super.dispose(); } queryHtml() async { SharedPreferences value = await SharedPreferences.getInstance(); if (apiService == null) apiService = ApiService(Dio(), context: context, token: value.getString("token")); if (widget.arguments["activityId"] != null) { BaseData baseData = await apiService .activityInfo(widget.arguments["activityId"]) .catchError((onError) {}); if (baseData != null && baseData.isSuccess) { activity = baseData.data; setState(() {}); } } if (widget.arguments["articleId"] != null) { BaseData
baseData = await apiService .informationInfo(widget.arguments["articleId"]) .catchError((onError) {}); if (baseData != null && baseData.isSuccess) { article = baseData.data; setState(() {}); } } } //发布评论 _queryMemberComment(String content) async { BaseData baseData = await apiService.memberComment({ "content": content, "parentId": parenId, "relationalId": widget.arguments["activityId"] ?? widget.arguments["articleId"], "relationalType": 1 }).catchError((error) {}); if (baseData != null && baseData.isSuccess) { CommentListState _commentList = commentKey.currentState; _commentList.queryMemberCommentList(); commentTextController.text = ""; FocusScope.of(context).unfocus(); _toComment(); } } share() async { SSDKMap params = SSDKMap() ..setGeneral( activity != null ? activity.mainTitle : article != null ? article.mainTitle : "", activity != null ? activity.viceTitle : article != null ? article.viceTitle : "", [ activity != null ? activity.coverImg : article != null ? article.coverImg : "", ], activity != null ? activity.coverImg : article != null ? article.coverImg : "", "", buildShareUrl(), "", "", "", "", SSDKContentTypes.webpage, ); showModalBottomSheet( context: context, backgroundColor: Colors.transparent, builder: (context) { return ShareDialog((platform) { if (platform == ShareSDKPlatforms.line) { params.map["type"] = SSDKContentTypes.text.value; params.map["text"] = "${activity != null ? activity.viceTitle : article != null ? article.viceTitle : ""} ${buildShareUrl()}"; } SharesdkPlugin.share(platform, params, (state, userData, contentEntity, error) { print("share!$state"); }); }); }); } String buildShareUrl() { return "https://hx.lotus-wallet.com/index.html?id=${widget.arguments["activityId"] ?? widget.arguments["articleId"]}&type=${activity != null ? "activity" : article != null ? "article" : ""}"; } //给文章/活动点赞 _queryInformationLikes() async { BaseData baseData = await apiService.informationLikes( widget.arguments["activityId"] ?? widget.arguments["articleId"]).catchError((onError) {}); if (baseData != null && baseData.isSuccess) { if (article != null) { if (article.liked) { article.likes -= 1; } else { article.likes += 1; } article.liked = !article.liked; } else if (activity != null) { if (activity.liked) { activity.likes -= 1; } else { activity.likes += 1; } activity.liked = !activity.liked; } commentKey.currentState.setState(() {}); } else { // SmartDialog.showToast(baseData.msg, alignment: Alignment.center); } } @override Widget build(BuildContext context) { return Scaffold( appBar: MyAppBar( action: Container( margin: EdgeInsets.only(right: 10), child: GestureDetector( onTap: () { share(); }, child: Icon( Icons.share, size: 24, color: Colors.black, ), ), ), background: Color(0xFFFFFFFFF), leadingColor: Colors.black, title: activity != null ? activity.mainTitle : article != null ? article.mainTitle : "", titleSize: fontSize, titleColor: Colors.black, ), body: Container( child: Column( children: [ Expanded( child: GestureDetector( onTap: () { commentFocus.unfocus(); hintText = S.of(context).liuxianinjingcaidepinglunba; inputKey.currentState.setState(() {}); parenId = "0"; }, child: SingleChildScrollView( controller: scrollController, physics: BouncingScrollPhysics(), child: Column( children: [ /// 富文本的头部 WebHeader(widget.arguments, activity, article, 16), /// 富文本的内容 WebContent( activity, article, () { setState(() {}); }, ), /// 富文本的评论 CommentList( commentKey, article?.likes ?? activity?.likes ?? 0, widget.arguments["activityId"] ?? widget.arguments["articleId"], 1, isKeyBoardShow, _reply, _delCommentTips, 12.sp, requestApiFinish: (total){setState(() { commentTotal = total; });}, ), ], ), ), ), flex: 1, ), /// 富文本评论的输入框 InputComment( inputKey, hintText, isKeyBoardShow, commentFocus, commentTextController, _toComment, _queryMemberComment, _queryInformationLikes, activity: activity, article: article, ), ], ), ), ); } ///删除评论的提示 _delCommentTips() { SmartDialog.show(widget: Tips(() { delComment(); })); } ///删除评论 delComment() async { BaseData baseData = await apiService.delComment( widget.arguments["activityId"] ?? widget.arguments["articleId"]); if (baseData != null && baseData.isSuccess) { CommentListState _commentList = commentKey.currentState; _commentList.queryMemberCommentList(); } } ///评论 回复 _reply(memberComment) { FocusScope.of(context).requestFocus(commentFocus); parenId = memberComment.id; hintText = S.of(context).huifu_("${memberComment.username}"); } ///滑动到评论列表 _toComment() { if (commentKey.currentContext == null) return; RenderBox firstRenderBox = commentKey.currentContext.findRenderObject(); Offset first = firstRenderBox.localToGlobal(Offset.zero); scrollController.animateTo( first.dy + scrollController.offset - (kToolbarHeight + MediaQuery.of(context).padding.top), duration: Duration(milliseconds: 300), curve: Curves.easeIn); } }