|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/services.dart'; |
| 3 | +import 'package:intl/intl.dart'; |
| 4 | +import 'dart:html' as html; |
| 5 | +import 'package:markdown_widget/markdown_widget.dart'; |
| 6 | +import 'package:sando_diary/PostMeta.dart'; |
| 7 | + |
| 8 | +class PostListPage extends StatefulWidget { |
| 9 | + const PostListPage({super.key}); |
| 10 | + @override |
| 11 | + State<PostListPage> createState() => _PostListPageState(); |
| 12 | +} |
| 13 | + |
| 14 | +class _PostListPageState extends State<PostListPage> { |
| 15 | + late Future<List<MdDoc>> future; |
| 16 | + late DateFormat df; |
| 17 | + bool isShowPostDetail = false; |
| 18 | + late MdDoc currentPost; |
| 19 | + |
| 20 | + @override |
| 21 | + void initState() { |
| 22 | + super.initState(); |
| 23 | + future = loadMarkdownDocs(context); |
| 24 | + df = DateFormat('yyyy-MM-dd'); // 한국어 로케일이면 intl 초기화 추가 가능 |
| 25 | + } |
| 26 | + |
| 27 | + void _launchURLInNewTab(String url) { |
| 28 | + html.window.open(url, '_blank'); |
| 29 | + } |
| 30 | + |
| 31 | + void _copyCurrentPostUrlToClipboard() { |
| 32 | + final currentUrl = html.window.location.href; // 현재 페이지의 URL 가져오기 |
| 33 | + Clipboard.setData(ClipboardData(text: currentUrl)); // URL을 클립보드에 복사 |
| 34 | + |
| 35 | + ScaffoldMessenger.of(context).showSnackBar( |
| 36 | + SnackBar(content: Text('URL copied to clipboard!')), |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + Widget _postDetail(MdDoc currentDoc) { |
| 41 | + return Padding( |
| 42 | + padding: const EdgeInsets.all(16.0), |
| 43 | + child: Stack( |
| 44 | + children: [ |
| 45 | + Align( |
| 46 | + alignment: Alignment.topRight, |
| 47 | + child: Text(currentDoc.meta.date != null |
| 48 | + ? DateFormat('yyyy-MM-dd').format(currentDoc.meta.date!) |
| 49 | + : '날짜 정보 없음'), |
| 50 | + ), |
| 51 | + MarkdownWidget( |
| 52 | + data: currentDoc.body, |
| 53 | + config: MarkdownConfig(configs: [ |
| 54 | + LinkConfig( |
| 55 | + style: TextStyle( |
| 56 | + color: Colors.cyan, |
| 57 | + decoration: TextDecoration.underline, |
| 58 | + ), |
| 59 | + onTap: (url) { |
| 60 | + _launchURLInNewTab(url); |
| 61 | + }, |
| 62 | + ) |
| 63 | + ]), |
| 64 | + ), |
| 65 | + ], |
| 66 | + ), |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + @override |
| 71 | + Widget build(BuildContext context) { |
| 72 | + return Scaffold( |
| 73 | + appBar: !isShowPostDetail |
| 74 | + ? AppBar( |
| 75 | + title: Text('Blog Posts'), |
| 76 | + ) |
| 77 | + : AppBar( |
| 78 | + elevation: 0, |
| 79 | + scrolledUnderElevation: 0, |
| 80 | + shadowColor: Colors.transparent, |
| 81 | + surfaceTintColor: Colors.transparent, |
| 82 | + forceMaterialTransparency: true, |
| 83 | + title: Text(currentPost.meta.title), |
| 84 | + leading: BackButton( |
| 85 | + onPressed: () => setState(() { |
| 86 | + isShowPostDetail = false; |
| 87 | + html.window.history.pushState(null, 'Posts', '/'); |
| 88 | + }), |
| 89 | + ), |
| 90 | + actions: [ |
| 91 | + IconButton( |
| 92 | + icon: const Icon(Icons.share), |
| 93 | + tooltip: 'Share this Post', |
| 94 | + onPressed: _copyCurrentPostUrlToClipboard, |
| 95 | + ), |
| 96 | + ], |
| 97 | + ), |
| 98 | + body: !isShowPostDetail |
| 99 | + ? FutureBuilder<List<MdDoc>>( |
| 100 | + future: future, |
| 101 | + builder: (context, snap) { |
| 102 | + if (snap.connectionState != ConnectionState.done) { |
| 103 | + return const Center(child: CircularProgressIndicator()); |
| 104 | + } |
| 105 | + if (snap.hasError) { |
| 106 | + return Center(child: Text('로드 실패: ${snap.error}')); |
| 107 | + } |
| 108 | + final docs = snap.data ?? []; |
| 109 | + if (docs.isEmpty) { |
| 110 | + return const Center(child: Text('문서가 없습니다.')); |
| 111 | + } |
| 112 | + return ListView.separated( |
| 113 | + itemCount: docs.length, |
| 114 | + separatorBuilder: (_, __) => const Divider(height: 1), |
| 115 | + itemBuilder: (context, i) { |
| 116 | + final d = docs[i]; |
| 117 | + final dateStr = d.meta.date != null |
| 118 | + ? df.format(d.meta.date!) |
| 119 | + : '날짜 정보 없음'; |
| 120 | + return ListTile( |
| 121 | + title: Text(d.meta.title, |
| 122 | + maxLines: 1, overflow: TextOverflow.ellipsis), |
| 123 | + subtitle: Text('${d.meta.category} · $dateStr'), |
| 124 | + trailing: const Icon(Icons.chevron_right), |
| 125 | + onTap: () { |
| 126 | + setState(() { |
| 127 | + isShowPostDetail = true; |
| 128 | + currentPost = d; |
| 129 | + }); |
| 130 | + }, |
| 131 | + ); |
| 132 | + }, |
| 133 | + ); |
| 134 | + }, |
| 135 | + ) |
| 136 | + : _postDetail(currentPost), |
| 137 | + ); |
| 138 | + } |
| 139 | +} |
0 commit comments