Skip to content

Commit

Permalink
[bulletin] display plain text on app card. display markdown in bullet…
Browse files Browse the repository at this point in the history
…in list page.
  • Loading branch information
liplum committed Aug 27, 2024
1 parent 8ca4bd3 commit 2740de4
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 33 deletions.
18 changes: 11 additions & 7 deletions lib/backend/bulletin/card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,34 @@ class _BulletinAppCardState extends ConsumerState<BulletinAppCard> {
}
}

class BulletinCardInAppCard extends StatelessWidget {
class BulletinLatestSummaryCard extends StatelessWidget {
final MimirBulletin bulletin;

const BulletinCardInAppCard(
const BulletinLatestSummaryCard(
this.bulletin, {
super.key,
});

@override
Widget build(BuildContext context) {
var title = bulletin.short;
var subtitle = bulletin.content;
if (title.isEmpty && bulletin.content.isNotEmpty) {
title = bulletin.content;
var subtitle = bulletin.text;
if (title.isEmpty && bulletin.text.isNotEmpty) {
title = bulletin.text;
}
if (title == subtitle) {
subtitle = "";
}

return ListTile(
isThreeLine: subtitle.isNotEmpty,
leading: const Icon(Icons.announcement),
title: TextScroll(title),
subtitle: subtitle.isNotEmpty ? subtitle.text(maxLines: 3) : null,
subtitle: subtitle.isNotEmpty
? subtitle.text(
maxLines: 2,
overflow: TextOverflow.ellipsis,
)
: null,
onTap: () async {
context.showSheet(
useRootNavigator: true,
Expand Down
6 changes: 5 additions & 1 deletion lib/backend/bulletin/entity/bulletin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ class MimirBulletin {
@JsonKey(fromJson: _trim)
final String short;
@JsonKey(fromJson: _trim)
final String text;
@JsonKey(fromJson: _trim)
final String content;

const MimirBulletin({
required this.id,
required this.createdAt,
required this.short,
required this.text,
required this.content,
});

Expand All @@ -32,9 +35,10 @@ class MimirBulletin {
"id": id,
"createdAt": createdAt,
"short": short,
"text": text,
"content": content,
}.toString();
}

bool get isEmpty => short.isEmpty && content.isEmpty;
bool get isEmpty => short.isEmpty && content.isEmpty && text.isEmpty;
}
8 changes: 8 additions & 0 deletions lib/backend/bulletin/entity/bulletin.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 20 additions & 18 deletions lib/backend/bulletin/page/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mimir/backend/bulletin/x.dart';
import 'package:mimir/backend/init.dart';
import 'package:mimir/l10n/extension.dart';
import 'package:mimir/widgets/markdown.dart';
import 'package:rettulf/rettulf.dart';
import 'package:text_scroll/text_scroll.dart';

import '../entity/bulletin.dart';

Expand Down Expand Up @@ -37,7 +37,7 @@ class _BulletinListPageState extends ConsumerState<BulletinListPage> {
}
}

List<MimirBulletin>? watchList(){
List<MimirBulletin>? watchList() {
var list = ref.watch(BackendInit.bulletinStorage.$list);
if (list == null) {
final latest = ref.read(BackendInit.bulletinStorage.$latest);
Expand Down Expand Up @@ -86,21 +86,23 @@ class BulletinCard extends StatelessWidget {

@override
Widget build(BuildContext context) {
var title = bulletin.short;
var subtitle = bulletin.content;
if (title.isEmpty && bulletin.content.isNotEmpty) {
title = bulletin.content;
}
if (title == subtitle) {
subtitle = "";
}

return ListTile(
title: TextScroll(title),
subtitle: [
if (subtitle.isNotEmpty) subtitle.text(maxLines: 3),
context.formatYmdhmNum(bulletin.createdAt).text(textAlign: TextAlign.end).padOnly(t: 4),
].column(caa: CrossAxisAlignment.stretch),
).inFilledCard();
return [
bulletin.short
.text(
style: context.textTheme.titleMedium,
textAlign: TextAlign.center,
)
.padOnly(b: 8),
FeaturedMarkdownWidget(
data: bulletin.content,
),
context
.formatYmdhmNum(bulletin.createdAt)
.text(
textAlign: TextAlign.end,
style: context.textTheme.labelLarge,
)
.padOnly(t: 4),
].column(caa: CrossAxisAlignment.stretch).padAll(12).inFilledCard();
}
}
14 changes: 13 additions & 1 deletion lib/backend/forum/card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:mimir/backend/bulletin/card.dart';
import 'package:mimir/backend/bulletin/page/list.dart';
import 'package:mimir/backend/bulletin/x.dart';
import 'package:mimir/design/adaptive/foundation.dart';
import 'package:mimir/life/event.dart';
import 'package:mimir/utils/async_event.dart';
import 'package:rettulf/rettulf.dart';
Expand Down Expand Up @@ -45,7 +47,7 @@ class _ForumAppCardState extends ConsumerState<ForumAppCard> {
final dev = ref.watch(Dev.$on);
return AppCard(
title: "小应社区".text(),
view: bulletin == null ? null : BulletinCardInAppCard(bulletin),
view: bulletin == null ? null : BulletinLatestSummaryCard(bulletin),
leftActions: [
FilledButton.icon(
onPressed: !dev
Expand All @@ -60,6 +62,16 @@ class _ForumAppCardState extends ConsumerState<ForumAppCard> {
label: !dev ? "敬请期待".text() : "进入".text(),
icon: Icon(context.icons.home),
),
OutlinedButton.icon(
onPressed: () async {
context.showSheet(
useRootNavigator: true,
(ctx) => const BulletinListPage(),
);
},
label: "布告栏".text(),
icon: const Icon(Icons.list_alt),
),
],
);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/widgets/html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ class RestyledHtmlWidget extends StatelessWidget {
final String html;
final RenderMode renderMode;
final TextStyle? textStyle;
final bool async;

const RestyledHtmlWidget(
this.html, {
super.key,
this.renderMode = RenderMode.column,
this.textStyle,
this.async = true,
});

@override
Widget build(BuildContext context) {
final textStyle = this.textStyle ?? context.textTheme.bodyMedium;
return HtmlWidget(
html,
buildAsync: true,
buildAsync: async,
renderMode: renderMode,
factoryBuilder: () => RestyledWidgetFactory(
textStyle: textStyle,
Expand Down
51 changes: 46 additions & 5 deletions lib/widgets/markdown.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:markdown/markdown.dart';

import 'html.dart';

class FeaturedMarkdownWidget extends StatelessWidget {
class FeaturedMarkdownWidget extends StatefulWidget {
final String data;
final bool async;
final bool restyle;

const FeaturedMarkdownWidget({
super.key,
required this.data,
this.async = false,
this.restyle = false,
});

@override
Widget build(BuildContext context) {
final html = markdownToHtml(
data,
State<FeaturedMarkdownWidget> createState() => _FeaturedMarkdownWidgetState();
}

class _FeaturedMarkdownWidgetState extends State<FeaturedMarkdownWidget> {
late String html;

@override
void initState() {
super.initState();
html = buildHtml();
}

@override
void didUpdateWidget(FeaturedMarkdownWidget oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.data != widget.data) {
setState(() {
html = buildHtml();
});
}
}

String buildHtml() {
return markdownToHtml(
widget.data,
inlineSyntaxes: [
InlineHtmlSyntax(),
StrikethroughSyntax(),
Expand All @@ -26,6 +53,20 @@ class FeaturedMarkdownWidget extends StatelessWidget {
TableSyntax(),
],
);
return RestyledHtmlWidget(html);
}

@override
Widget build(BuildContext context) {
if (widget.restyle) {
return RestyledHtmlWidget(
html,
async: widget.async,
);
} else {
return HtmlWidget(
html,
buildAsync: widget.async,
);
}
}
}

0 comments on commit 2740de4

Please sign in to comment.