Skip to content

Commit

Permalink
添加分享功能
Browse files Browse the repository at this point in the history
  • Loading branch information
qianyue0317 committed Oct 24, 2023
1 parent b004c67 commit 12fb660
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/network/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@ class Api {

/// 我的分享
static const String sharedList = "user/lg/private_articles/";

/// 分享文章 post
static const String shareArticle = "lg/user_article/add/json";
}

93 changes: 93 additions & 0 deletions lib/pages/my_shared_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:wan_android_flutter/network/bean/article_data_entity.dart';
import 'package:wan_android_flutter/network/bean/my_shared_data_entity.dart';
import 'package:wan_android_flutter/network/request_util.dart';
import 'package:wan_android_flutter/pages/article_item_layout.dart';
import 'package:wan_android_flutter/pages/detail_page.dart';

class MySharedPage extends StatefulWidget {
const MySharedPage({Key? key}) : super(key: key);
Expand All @@ -26,6 +27,9 @@ class _MySharedPageState extends State<MySharedPage> {

late var dataObs = _data.obs;

final TextEditingController _titleController = TextEditingController();
final TextEditingController _linkController = TextEditingController();

final EasyRefreshController _refreshController = EasyRefreshController(
controlFinishRefresh: true, controlFinishLoad: true);

Expand All @@ -50,6 +54,14 @@ class _MySharedPageState extends State<MySharedPage> {
backgroundColor: Theme.of(context).primaryColor,
title: const Text("我的分享", style: TextStyle(color: Colors.white)),
iconTheme: const IconThemeData(color: Colors.white),
actions: [
Padding(
padding: const EdgeInsets.only(right: 16),
child: GestureDetector(
onTap: _showShareDialog,
child: const Icon(Icons.add, color: Colors.white),
))
],
),
body: FutureBuilder(future: (() async {
_pageIndex = 1;
Expand Down Expand Up @@ -88,6 +100,9 @@ class _MySharedPageState extends State<MySharedPage> {
itemBuilder: (context, index) {
ArticleItemEntity itemEntity = _data[index];
return GestureDetector(
onTap: () {
Get.to(()=> DetailPage(itemEntity.link, itemEntity.title));
},
behavior: HitTestBehavior.opaque,
child: ArticleItemLayout(
itemEntity: itemEntity,
Expand Down Expand Up @@ -131,4 +146,82 @@ class _MySharedPageState extends State<MySharedPage> {
(res.errorMsg ?? res.errorCode.toString()));
}
}

_showShareDialog() async {
await showModalBottomSheet<bool?>(
isScrollControlled: true,
context: context,
builder: (context) {
return AnimatedPadding(
padding: MediaQuery.of(context).viewInsets,
duration: Duration.zero,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding:
const EdgeInsets.only(left: 16, right: 16, top: 32),
child: TextField(
controller: _titleController,
decoration: const InputDecoration(
contentPadding: EdgeInsets.symmetric(
vertical: 4, horizontal: 8),
hintText: "文章标题",
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(16))))),
),
Padding(
padding:
const EdgeInsets.only(left: 16, right: 16, top: 16),
child: TextField(
controller: _linkController,
decoration: const InputDecoration(
contentPadding: EdgeInsets.symmetric(
vertical: 4, horizontal: 8),
hintText: "文章链接",
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(16)))))),
Padding(
padding: const EdgeInsets.only(top: 16, bottom: 16),
child: TextButton(
onPressed: () async {
bool result = await _onShareClick();
if (result) {
Get.back();
}
},
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: const BorderRadius.all(
Radius.circular(16))),
padding: const EdgeInsets.symmetric(
vertical: 8, horizontal: 16),
child: const Text(
"分享",
style: TextStyle(color: Colors.white),
),
)))
],
));
});
}

Future<bool> _onShareClick() async {
FocusScope.of(context).unfocus();
AppResponse<dynamic> res = await HttpGo.instance.post(Api.shareArticle,
data: {"title": _titleController.text, "link": _linkController.text});
if (res.isSuccessful) {
// 分享成功
Fluttertoast.showToast(msg: "分享成功!");
await _onRefresh();
return true;
} else {
Fluttertoast.showToast(msg: "分享失败--${res.errorMsg}");
return false;
}
}
}

0 comments on commit 12fb660

Please sign in to comment.