You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
319 lines
9.5 KiB
319 lines
9.5 KiB
3 years ago
|
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<String, dynamic> arguments;
|
||
|
|
||
|
///富文本 文章 活动
|
||
|
WebPage({this.arguments});
|
||
|
|
||
|
@override
|
||
|
State<StatefulWidget> createState() {
|
||
|
return _WebPage();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class _WebPage extends State<WebPage> with WidgetsBindingObserver {
|
||
|
ApiService apiService;
|
||
|
int commentTotal = 0;
|
||
|
String hintText = S.current.liuxianinjingcaidepinglunba;
|
||
|
var commentFocus = FocusNode();
|
||
|
String parenId = "0";
|
||
|
|
||
|
Activity activity;
|
||
|
Article article;
|
||
|
final GlobalKey commentKey = GlobalKey();
|
||
|
final GlobalKey inputKey = GlobalKey();
|
||
|
final ScrollController scrollController = ScrollController();
|
||
|
|
||
|
@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<Activity> baseData = await apiService.activityInfo(widget.arguments["activityId"]).catchError((onError){});
|
||
|
if (baseData != null && baseData.isSuccess) {
|
||
|
activity = baseData.data;
|
||
|
setState(() {});
|
||
|
}
|
||
|
}
|
||
|
if (widget.arguments["articleId"] != null) {
|
||
|
BaseData<Article> 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) {
|
||
|
_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"]);
|
||
|
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;
|
||
|
}
|
||
|
} 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(0xFFF7F7F7),
|
||
|
leadingColor: Colors.black,
|
||
|
title: activity != null
|
||
|
? activity.mainTitle
|
||
|
: article != null
|
||
|
? article.mainTitle
|
||
|
: "",
|
||
|
titleSize: 18.sp,
|
||
|
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),
|
||
|
|
||
|
/// 富文本的内容
|
||
|
WebContent(activity, article),
|
||
|
|
||
|
/// 富文本的评论
|
||
|
CommentList(
|
||
|
commentKey,
|
||
|
widget.arguments,
|
||
|
activity,
|
||
|
article,
|
||
|
isKeyBoardShow,
|
||
|
_reply,
|
||
|
_delCommentTips,
|
||
|
),
|
||
|
],
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
flex: 1,
|
||
|
),
|
||
|
|
||
|
/// 富文本评论的输入框
|
||
|
InputComment(
|
||
|
inputKey,
|
||
|
hintText,
|
||
|
activity,
|
||
|
article,
|
||
|
isKeyBoardShow,
|
||
|
commentFocus,
|
||
|
_toComment,
|
||
|
_queryMemberComment,
|
||
|
_queryInformationLikes,
|
||
|
),
|
||
|
],
|
||
|
),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
_delCommentTips() {
|
||
|
SmartDialog.show(widget: Tips(() {
|
||
|
delComment();
|
||
|
}));
|
||
|
}
|
||
|
|
||
|
delComment() async {
|
||
|
BaseData baseData = await apiService.delComment(
|
||
|
widget.arguments["activityId"] ?? widget.arguments["articleId"]);
|
||
|
if (baseData != null && baseData.isSuccess) {
|
||
|
commentKey.currentState.setState(() {});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
_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: 1000),
|
||
|
curve: Curves.easeIn);
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|