loris/lib/partials/thread.dart

145 lines
3.9 KiB
Dart
Raw Normal View History

2022-07-03 13:47:24 +00:00
import 'package:flutter/material.dart';
2022-08-28 18:55:23 +00:00
import 'package:localization/localization.dart';
import 'package:loris/partials/post.dart';
2022-08-01 08:29:43 +00:00
import '../business_logic/timeline/timeline.dart' as logic;
2022-08-14 18:02:42 +00:00
import '../global.dart' as global;
2022-09-26 14:30:02 +00:00
import 'package:loris/themes/themes.dart' as themes;
2022-07-03 13:47:24 +00:00
2022-08-28 18:55:23 +00:00
class Thread extends StatefulWidget {
2022-09-05 21:09:49 +00:00
const Thread({
required this.model,
Key? key,
this.constrained = true,
}) : super(key: key);
2022-08-01 08:29:43 +00:00
final logic.ThreadModel model;
2022-09-05 21:09:49 +00:00
final bool constrained;
2022-07-03 13:47:24 +00:00
2022-08-28 18:55:23 +00:00
@override
State<Thread> createState() => _ThreadState();
}
class _ThreadState extends State<Thread> {
Set<String> contentWarnings = {};
int sensitivePosts = 0;
2022-08-28 18:55:23 +00:00
bool showSensitive = false;
2022-09-30 15:45:07 +00:00
bool collapsed = false;
2022-08-28 18:55:23 +00:00
2022-07-03 13:47:24 +00:00
@override
Widget build(BuildContext context) {
2022-09-30 15:45:07 +00:00
List<Widget> c = [
Align(
alignment: Alignment.centerRight,
child: TextButton.icon(
onPressed: () {
setState(() {
collapsed = !collapsed;
});
},
icon: Icon(collapsed ? Icons.fullscreen : Icons.fullscreen_exit),
label: Text(collapsed ? "expand".i18n() : "collapse".i18n())),
)
];
2022-08-28 18:55:23 +00:00
for (var element in widget.model.posts) {
2022-09-30 15:45:07 +00:00
c.add(Visibility(
visible: !collapsed,
child: Post(
model: element,
hideSensitive: !showSensitive,
),
2022-08-28 18:55:23 +00:00
));
if (element.sensitive) {
sensitivePosts += 1;
contentWarnings.add(element.spoilerText);
2022-08-28 18:55:23 +00:00
}
}
2022-09-04 14:20:42 +00:00
contentWarnings.map(
(e) => e.trim(),
);
contentWarnings.removeWhere((element) => element == "");
if (sensitivePosts > 1 && c.length > 1) {
String s = "";
int i = 0;
for (var element in contentWarnings) {
2022-09-04 14:20:42 +00:00
if (i == 0 && i < contentWarnings.length - 1) {
s = "$element;";
} else if (i < contentWarnings.length - 1) {
s = "$s $element;";
} else {
s = "$s $element";
}
i++;
}
2022-09-30 15:45:07 +00:00
2022-08-28 18:55:23 +00:00
c.insert(
0,
2022-09-30 15:45:07 +00:00
Visibility(
visible: !collapsed,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Expanded(child: SelectableText(s)),
ElevatedButton.icon(
onPressed: () {
setState(() {
showSensitive = !showSensitive;
});
},
icon: Icon(
showSensitive ? Icons.visibility_off : Icons.visibility),
label: Text(showSensitive ? "hide".i18n() : "show".i18n()),
),
],
),
2022-08-28 18:55:23 +00:00
),
);
2022-08-01 08:29:43 +00:00
}
2022-09-05 21:09:49 +00:00
if (!widget.constrained) {
2022-09-23 21:47:30 +00:00
return Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
border: Border.all(
color: Theme.of(context).colorScheme.secondary,
width: 2,
2022-09-05 21:09:49 +00:00
),
2022-09-23 21:47:30 +00:00
borderRadius: BorderRadius.circular(8),
),
child: Column(
children: c,
2022-09-05 21:09:49 +00:00
),
);
}
2022-07-03 13:47:24 +00:00
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
2022-09-23 17:08:05 +00:00
Container(
clipBehavior: Clip.none,
foregroundDecoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(8)),
border: Border.all(
width: 2,
color: Theme.of(context).colorScheme.secondary,
2022-09-23 13:03:28 +00:00
),
2022-09-23 17:08:05 +00:00
),
2022-09-23 21:47:30 +00:00
width: global.getWidth(context),
2022-09-23 17:08:05 +00:00
constraints: global.getConstraints(context),
child: Material(
borderRadius: const BorderRadius.all(Radius.circular(8)),
child: Padding(
2022-09-26 14:30:02 +00:00
padding: themes.defaultInsideMargins,
2022-09-23 17:08:05 +00:00
child: Column(
2022-09-30 15:45:07 +00:00
crossAxisAlignment: CrossAxisAlignment.stretch,
2022-09-23 17:08:05 +00:00
children: c,
2022-09-07 08:06:11 +00:00
),
2022-07-03 20:02:57 +00:00
),
2022-07-03 13:47:24 +00:00
),
),
],
);
}
}