Solian/lib/widgets/posts/item_action.dart

117 lines
3.5 KiB
Dart
Raw Normal View History

2024-04-14 10:38:44 +00:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:solian/models/post.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/router.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2024-04-16 12:36:47 +00:00
import 'package:solian/screens/posts/comment_editor.dart';
2024-04-14 15:00:04 +00:00
import 'package:solian/widgets/posts/item_deletion.dart';
2024-04-14 10:38:44 +00:00
class PostItemAction extends StatelessWidget {
final Post item;
final Function? onUpdate;
2024-04-15 15:08:32 +00:00
final Function? onDelete;
2024-04-14 10:38:44 +00:00
2024-04-15 15:08:32 +00:00
const PostItemAction({
super.key,
required this.item,
this.onUpdate,
this.onDelete,
});
2024-04-14 10:38:44 +00:00
2024-04-16 12:36:47 +00:00
void viewEditor() async {
bool ok = false;
switch (item.modelType) {
case 'article':
ok = await router.pushNamed(
'posts.articles.editor',
extra: item,
) as bool;
case 'moment':
ok = await router.pushNamed(
'posts.moments.editor',
extra: item,
) as bool;
case 'comment':
ok = await router.pushNamed(
'posts.comments.editor',
extra: CommentPostArguments(editing: item),
) as bool;
}
if (ok == true && onUpdate != null) {
onUpdate!();
}
}
2024-04-14 10:38:44 +00:00
@override
Widget build(BuildContext context) {
final auth = context.read<AuthProvider>();
return SizedBox(
2024-04-14 15:00:04 +00:00
height: 320,
2024-04-14 10:38:44 +00:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: const EdgeInsets.only(left: 20, top: 20, bottom: 12),
child: Text(
AppLocalizations.of(context)!.action,
style: Theme.of(context).textTheme.headlineSmall,
),
),
Expanded(
child: FutureBuilder(
2024-04-14 15:00:04 +00:00
future: auth.getProfiles(),
builder: (context, snapshot) {
if (snapshot.hasData) {
final authorizedItems = [
ListTile(
leading: const Icon(Icons.edit),
title: Text(AppLocalizations.of(context)!.edit),
2024-04-16 12:36:47 +00:00
onTap: () => viewEditor(),
2024-04-14 15:00:04 +00:00
),
ListTile(
leading: const Icon(Icons.delete),
title: Text(AppLocalizations.of(context)!.delete),
onTap: () {
final dataset = '${item.modelType}s';
showDialog(
context: context,
builder: (context) => ItemDeletionDialog(
item: item,
dataset: dataset,
),
).then((did) {
if (did == true && onDelete != null) onDelete!();
});
2024-04-14 15:00:04 +00:00
},
)
];
return ListView(
children: [
2024-04-20 04:06:16 +00:00
...(snapshot.data['id'] == item.author.externalId
2024-04-14 15:00:04 +00:00
? authorizedItems
: List.empty()),
2024-04-14 10:38:44 +00:00
ListTile(
2024-04-14 15:00:04 +00:00
leading: const Icon(Icons.report),
title: Text(AppLocalizations.of(context)!.report),
onTap: () {},
2024-04-14 10:38:44 +00:00
)
2024-04-14 15:00:04 +00:00
],
);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
),
2024-04-14 10:38:44 +00:00
),
],
),
);
}
}