✨ Post draft
This commit is contained in:
247
lib/screens/articles/article_publish.dart
Normal file
247
lib/screens/articles/article_publish.dart
Normal file
@ -0,0 +1,247 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_animate/flutter_animate.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:solian/exts.dart';
|
||||
import 'package:solian/models/post.dart';
|
||||
import 'package:solian/models/realm.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:solian/router.dart';
|
||||
import 'package:solian/theme.dart';
|
||||
import 'package:solian/widgets/app_bar_title.dart';
|
||||
import 'package:solian/widgets/attachments/attachment_publish.dart';
|
||||
import 'package:solian/widgets/posts/tags_field.dart';
|
||||
import 'package:solian/widgets/prev_page.dart';
|
||||
import 'package:textfield_tags/textfield_tags.dart';
|
||||
|
||||
class ArticlePublishArguments {
|
||||
final Post? edit;
|
||||
final Realm? realm;
|
||||
|
||||
ArticlePublishArguments({this.edit, this.realm});
|
||||
}
|
||||
|
||||
class ArticlePublishScreen extends StatefulWidget {
|
||||
final Post? edit;
|
||||
final Realm? realm;
|
||||
|
||||
const ArticlePublishScreen({
|
||||
super.key,
|
||||
this.edit,
|
||||
this.realm,
|
||||
});
|
||||
|
||||
@override
|
||||
State<ArticlePublishScreen> createState() => _ArticlePublishScreenState();
|
||||
}
|
||||
|
||||
class _ArticlePublishScreenState extends State<ArticlePublishScreen> {
|
||||
final _titleController = TextEditingController();
|
||||
final _descriptionController = TextEditingController();
|
||||
final _contentController = TextEditingController();
|
||||
final _tagsController = StringTagController();
|
||||
|
||||
bool _isBusy = false;
|
||||
|
||||
List<int> _attachments = List.empty();
|
||||
|
||||
void showAttachments() {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
builder: (context) => AttachmentPublishPopup(
|
||||
usage: 'i.attachment',
|
||||
current: _attachments,
|
||||
onUpdate: (value) => _attachments = value,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void applyPost() async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) return;
|
||||
if (_contentController.value.text.isEmpty) return;
|
||||
|
||||
setState(() => _isBusy = true);
|
||||
|
||||
final client = auth.configureClient('interactive');
|
||||
|
||||
final payload = {
|
||||
'title': _titleController.value.text,
|
||||
'description': _descriptionController.value.text,
|
||||
'content': _contentController.value.text,
|
||||
'tags': _tagsController.getTags?.map((x) => {'alias': x}).toList() ??
|
||||
List.empty(),
|
||||
'attachments': _attachments,
|
||||
if (widget.edit != null) 'alias': widget.edit!.alias,
|
||||
if (widget.realm != null) 'realm': widget.realm!.alias,
|
||||
};
|
||||
|
||||
Response resp;
|
||||
if (widget.edit != null) {
|
||||
resp = await client.put('/api/articles/${widget.edit!.id}', payload);
|
||||
} else {
|
||||
resp = await client.post('/api/articles', payload);
|
||||
}
|
||||
if (resp.statusCode != 200) {
|
||||
context.showErrorDialog(resp.bodyString);
|
||||
} else {
|
||||
AppRouter.instance.pop(resp.body);
|
||||
}
|
||||
|
||||
setState(() => _isBusy = false);
|
||||
}
|
||||
|
||||
void syncWidget() {
|
||||
if (widget.edit != null) {
|
||||
_contentController.text = widget.edit!.content;
|
||||
_attachments = widget.edit!.attachments ?? List.empty();
|
||||
}
|
||||
}
|
||||
|
||||
void cancelAction() {
|
||||
AppRouter.instance.pop();
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
syncWidget();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final notifyBannerActions = [
|
||||
TextButton(
|
||||
onPressed: cancelAction,
|
||||
child: Text('cancel'.tr),
|
||||
)
|
||||
];
|
||||
|
||||
return Material(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: AppBarTitle('articlePublish'.tr),
|
||||
centerTitle: false,
|
||||
toolbarHeight: SolianTheme.toolbarHeight(context),
|
||||
leading: const PrevPageButton(),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: _isBusy ? null : () => applyPost(),
|
||||
child: Text('postAction'.tr.toUpperCase()),
|
||||
)
|
||||
],
|
||||
),
|
||||
body: SafeArea(
|
||||
top: false,
|
||||
child: Stack(
|
||||
children: [
|
||||
ListView(
|
||||
children: [
|
||||
if (_isBusy)
|
||||
const LinearProgressIndicator().animate().scaleX(),
|
||||
if (widget.edit != null)
|
||||
MaterialBanner(
|
||||
leading: const Icon(Icons.edit),
|
||||
leadingPadding:
|
||||
const EdgeInsets.only(left: 10, right: 20),
|
||||
dividerColor: Colors.transparent,
|
||||
content: Text('postEditingNotify'.tr),
|
||||
actions: notifyBannerActions,
|
||||
),
|
||||
if (widget.realm != null)
|
||||
MaterialBanner(
|
||||
leading: const Icon(Icons.group),
|
||||
leadingPadding:
|
||||
const EdgeInsets.only(left: 10, right: 20),
|
||||
dividerColor: Colors.transparent,
|
||||
content: Text(
|
||||
'postInRealmNotify'
|
||||
.trParams({'realm': '#${widget.realm!.alias}'}),
|
||||
),
|
||||
actions: notifyBannerActions,
|
||||
),
|
||||
const Divider(thickness: 0.3, height: 0.3)
|
||||
.paddingOnly(bottom: 6),
|
||||
Container(
|
||||
padding:
|
||||
const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child: TextField(
|
||||
maxLines: null,
|
||||
autofocus: true,
|
||||
autocorrect: true,
|
||||
keyboardType: TextInputType.multiline,
|
||||
controller: _titleController,
|
||||
decoration: InputDecoration.collapsed(
|
||||
hintText: 'articleTitlePlaceholder'.tr,
|
||||
),
|
||||
onTapOutside: (_) =>
|
||||
FocusManager.instance.primaryFocus?.unfocus(),
|
||||
),
|
||||
),
|
||||
const Divider(thickness: 0.3, height: 0.3)
|
||||
.paddingSymmetric(vertical: 6),
|
||||
Container(
|
||||
padding:
|
||||
const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child: TextField(
|
||||
maxLines: null,
|
||||
autofocus: true,
|
||||
autocorrect: true,
|
||||
keyboardType: TextInputType.multiline,
|
||||
controller: _contentController,
|
||||
decoration: InputDecoration.collapsed(
|
||||
hintText: 'articleContentPlaceholder'.tr,
|
||||
),
|
||||
onTapOutside: (_) =>
|
||||
FocusManager.instance.primaryFocus?.unfocus(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
Positioned(
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
child: Column(
|
||||
children: [
|
||||
TagsField(
|
||||
initialTags:
|
||||
widget.edit?.tags?.map((x) => x.alias).toList(),
|
||||
tagsController: _tagsController,
|
||||
hintText: 'postTagsPlaceholder'.tr,
|
||||
),
|
||||
const Divider(thickness: 0.3, height: 0.3),
|
||||
SizedBox(
|
||||
height: 56,
|
||||
child: Row(
|
||||
children: [
|
||||
TextButton(
|
||||
style: TextButton.styleFrom(
|
||||
shape: const CircleBorder(),
|
||||
),
|
||||
child: const Icon(Icons.camera_alt),
|
||||
onPressed: () => showAttachments(),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_titleController.dispose();
|
||||
_descriptionController.dispose();
|
||||
_contentController.dispose();
|
||||
_tagsController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
@ -101,7 +101,7 @@ class FeedCreationButton extends StatelessWidget {
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData && snapshot.data == true) {
|
||||
return PopupMenuButton(
|
||||
icon: const Icon(Icons.add_circle),
|
||||
icon: const Icon(Icons.edit_square),
|
||||
itemBuilder: (BuildContext context) => [
|
||||
PopupMenuItem(
|
||||
child: ListTile(
|
||||
@ -110,7 +110,7 @@ class FeedCreationButton extends StatelessWidget {
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
),
|
||||
onTap: () {
|
||||
AppRouter.instance.pushNamed('postPublishing').then((val) {
|
||||
AppRouter.instance.pushNamed('postCreate').then((val) {
|
||||
if (val != null && onCreated != null) {
|
||||
onCreated!();
|
||||
}
|
||||
@ -123,7 +123,13 @@ class FeedCreationButton extends StatelessWidget {
|
||||
leading: const Icon(Icons.newspaper),
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
),
|
||||
onTap: () {},
|
||||
onTap: () {
|
||||
AppRouter.instance.pushNamed('articleCreate').then((val) {
|
||||
if (val != null && onCreated != null) {
|
||||
onCreated!();
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
PopupMenuItem(
|
||||
child: ListTile(
|
||||
@ -131,7 +137,9 @@ class FeedCreationButton extends StatelessWidget {
|
||||
leading: const Icon(Icons.drafts),
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
),
|
||||
onTap: () {},
|
||||
onTap: () {
|
||||
AppRouter.instance.goNamed('draftBox');
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
|
33
lib/screens/feed/draft_box.dart
Normal file
33
lib/screens/feed/draft_box.dart
Normal file
@ -0,0 +1,33 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:solian/screens/feed.dart';
|
||||
import 'package:solian/theme.dart';
|
||||
import 'package:solian/widgets/app_bar_title.dart';
|
||||
import 'package:solian/widgets/prev_page.dart';
|
||||
|
||||
class DraftBoxScreen extends StatelessWidget {
|
||||
const DraftBoxScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Material(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: AppBarTitle('draftBox'.tr),
|
||||
centerTitle: false,
|
||||
toolbarHeight: SolianTheme.toolbarHeight(context),
|
||||
leading: const PrevPageButton(),
|
||||
actions: [
|
||||
FeedCreationButton(
|
||||
onCreated: () {},
|
||||
),
|
||||
SizedBox(
|
||||
width: SolianTheme.isLargeScreen(context) ? 8 : 16,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -8,30 +8,30 @@ import 'package:solian/models/realm.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:solian/router.dart';
|
||||
import 'package:solian/theme.dart';
|
||||
import 'package:solian/widgets/account/account_avatar.dart';
|
||||
import 'package:solian/widgets/app_bar_title.dart';
|
||||
import 'package:solian/widgets/attachments/attachment_publish.dart';
|
||||
import 'package:solian/widgets/posts/post_item.dart';
|
||||
import 'package:solian/widgets/posts/tags_field.dart';
|
||||
import 'package:solian/widgets/prev_page.dart';
|
||||
import 'package:textfield_tags/textfield_tags.dart';
|
||||
import 'package:badges/badges.dart' as badges;
|
||||
|
||||
class PostPublishingArguments {
|
||||
class PostPublishArguments {
|
||||
final Post? edit;
|
||||
final Post? reply;
|
||||
final Post? repost;
|
||||
final Realm? realm;
|
||||
|
||||
PostPublishingArguments({this.edit, this.reply, this.repost, this.realm});
|
||||
PostPublishArguments({this.edit, this.reply, this.repost, this.realm});
|
||||
}
|
||||
|
||||
class PostPublishingScreen extends StatefulWidget {
|
||||
class PostPublishScreen extends StatefulWidget {
|
||||
final Post? edit;
|
||||
final Post? reply;
|
||||
final Post? repost;
|
||||
final Realm? realm;
|
||||
|
||||
const PostPublishingScreen({
|
||||
const PostPublishScreen({
|
||||
super.key,
|
||||
this.edit,
|
||||
this.reply,
|
||||
@ -40,10 +40,10 @@ class PostPublishingScreen extends StatefulWidget {
|
||||
});
|
||||
|
||||
@override
|
||||
State<PostPublishingScreen> createState() => _PostPublishingScreenState();
|
||||
State<PostPublishScreen> createState() => _PostPublishScreenState();
|
||||
}
|
||||
|
||||
class _PostPublishingScreenState extends State<PostPublishingScreen> {
|
||||
class _PostPublishScreenState extends State<PostPublishScreen> {
|
||||
final _contentController = TextEditingController();
|
||||
final _tagsController = StringTagController();
|
||||
|
||||
@ -51,6 +51,8 @@ class _PostPublishingScreenState extends State<PostPublishingScreen> {
|
||||
|
||||
List<int> _attachments = List.empty();
|
||||
|
||||
bool _isDraft = false;
|
||||
|
||||
void showAttachments() {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
@ -77,6 +79,7 @@ class _PostPublishingScreenState extends State<PostPublishingScreen> {
|
||||
'tags': _tagsController.getTags?.map((x) => {'alias': x}).toList() ??
|
||||
List.empty(),
|
||||
'attachments': _attachments,
|
||||
'is_draft': _isDraft,
|
||||
if (widget.edit != null) 'alias': widget.edit!.alias,
|
||||
if (widget.reply != null) 'reply_to': widget.reply!.id,
|
||||
if (widget.repost != null) 'repost_to': widget.repost!.id,
|
||||
@ -102,6 +105,7 @@ class _PostPublishingScreenState extends State<PostPublishingScreen> {
|
||||
if (widget.edit != null) {
|
||||
_contentController.text = widget.edit!.content;
|
||||
_attachments = widget.edit!.attachments ?? List.empty();
|
||||
_isDraft = widget.edit!.isDraft ?? false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -117,8 +121,6 @@ class _PostPublishingScreenState extends State<PostPublishingScreen> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final AuthProvider auth = Get.find();
|
||||
|
||||
final notifyBannerActions = [
|
||||
TextButton(
|
||||
onPressed: cancelAction,
|
||||
@ -130,14 +132,18 @@ class _PostPublishingScreenState extends State<PostPublishingScreen> {
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: AppBarTitle('postPublishing'.tr),
|
||||
title: AppBarTitle('postPublish'.tr),
|
||||
centerTitle: false,
|
||||
toolbarHeight: SolianTheme.toolbarHeight(context),
|
||||
leading: const PrevPageButton(),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: _isBusy ? null : () => applyPost(),
|
||||
child: Text('postAction'.tr.toUpperCase()),
|
||||
child: Text(
|
||||
_isDraft
|
||||
? 'draftSave'.tr.toUpperCase()
|
||||
: 'postAction'.tr.toUpperCase(),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
@ -194,22 +200,6 @@ class _PostPublishingScreenState extends State<PostPublishingScreen> {
|
||||
).paddingOnly(bottom: 8),
|
||||
],
|
||||
),
|
||||
FutureBuilder(
|
||||
future: auth.getProfile(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return ListTile(
|
||||
leading: AccountAvatar(
|
||||
content: snapshot.data?.body!['avatar'],
|
||||
radius: 22),
|
||||
title: Text(snapshot.data?.body!['nick']),
|
||||
subtitle: Text('postIdentityNotify'.tr),
|
||||
);
|
||||
} else {
|
||||
return Container();
|
||||
}
|
||||
},
|
||||
),
|
||||
if (widget.realm != null)
|
||||
MaterialBanner(
|
||||
leading: const Icon(Icons.group),
|
||||
@ -247,6 +237,7 @@ class _PostPublishingScreenState extends State<PostPublishingScreen> {
|
||||
left: 0,
|
||||
right: 0,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
TagsField(
|
||||
initialTags:
|
||||
@ -257,17 +248,30 @@ class _PostPublishingScreenState extends State<PostPublishingScreen> {
|
||||
const Divider(thickness: 0.3, height: 0.3),
|
||||
SizedBox(
|
||||
height: 56,
|
||||
child: Row(
|
||||
child: ListView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
children: [
|
||||
TextButton(
|
||||
style: TextButton.styleFrom(
|
||||
shape: const CircleBorder(),
|
||||
IconButton(
|
||||
icon: _isDraft
|
||||
? const Icon(Icons.drive_file_rename_outline)
|
||||
: const Icon(Icons.public),
|
||||
color: _isDraft
|
||||
? Colors.grey.shade600
|
||||
: Colors.green.shade700,
|
||||
onPressed: () {
|
||||
setState(() => _isDraft = !_isDraft);
|
||||
},
|
||||
),
|
||||
badges.Badge(
|
||||
badgeContent: Text(_attachments.length.toString()),
|
||||
showBadge: _attachments.isNotEmpty,
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.camera_alt),
|
||||
onPressed: () => showAttachments(),
|
||||
),
|
||||
child: const Icon(Icons.camera_alt),
|
||||
onPressed: () => showAttachments(),
|
||||
)
|
||||
],
|
||||
),
|
||||
).paddingSymmetric(horizontal: 6, vertical: 8),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
@ -210,8 +210,8 @@ class _RealmPostListWidgetState extends State<RealmPostListWidget> {
|
||||
onTap: () {
|
||||
AppRouter.instance
|
||||
.pushNamed(
|
||||
'postPublishing',
|
||||
extra: PostPublishingArguments(realm: widget.realm),
|
||||
'postCreate',
|
||||
extra: PostPublishArguments(realm: widget.realm),
|
||||
)
|
||||
.then((value) {
|
||||
if (value != null) _pagingController.refresh();
|
||||
|
Reference in New Issue
Block a user