6주차 활동 보고서
void _confirmDelete(String id) async {
final confirmed = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text('삭제 확인'),
content: const Text('이 일기를 삭제하시겠습니까?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('취소'),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('삭제'),
),
],
),
);
if (confirmed == true) {
await diaryBox.delete(id);
setState(() {}); // 리스트 갱신
}
}
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import '../models/diary_entry.dart';
import 'package:fl_chart/fl_chart.dart';
class EmotionChart extends StatelessWidget {
const EmotionChart({super.key});
@override
Widget build(BuildContext context) {
final box = Hive.box<DiaryEntry>('diaryBox');
final entries = box.values.toList();
final emotionCount = <String, int>{};
for (var entry in entries) {
emotionCount[entry.emotion] = (emotionCount[entry.emotion] ?? 0) + 1;
}
final sections = emotionCount.entries.map((e) {
final emotion = e.key;
final count = e.value;
return PieChartSectionData(
value: count.toDouble(),
title: '$emotion\\n$count',
radius: 60,
color: _colorForEmotion(emotion),
titleStyle: const TextStyle(fontSize: 14, color: Colors.white),
);
}).toList();
return PieChart(
PieChartData(
sections: sections,
sectionsSpace: 2,
centerSpaceRadius: 32,
),
);
}
Color _colorForEmotion(String emotion) {
switch (emotion.toLowerCase()) {
case 'happy':
return Colors.yellow;
case 'sad':
return Colors.blue;
case 'angry':
return Colors.red;
case 'neutral':
return Colors.grey;
default:
return Colors.green;
}
}
}
class DiaryDetailScreen extends StatefulWidget {
final DiaryEntry entry;
const DiaryDetailScreen({super.key, required this.entry});
@override
State<DiaryDetailScreen> createState() => _DiaryDetailScreenState();
}
class _DiaryDetailScreenState extends State<DiaryDetailScreen> {
late TextEditingController _summaryController;
@override
void initState() {
super.initState();
_summaryController = TextEditingController(text: widget.entry.summary);
}
void _saveSummary() async {
final box = Hive.box<DiaryEntry>('diaryBox');
final updated = widget.entry.copyWith(summary: _summaryController.text);
await box.put(widget.entry.id, updated);
Navigator.pop(context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('일기 상세')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('날짜: ${widget.entry.createdAt.toLocal().toString().split(' ')[0]}',
style: const TextStyle(fontSize: 16)),
const SizedBox(height: 16),
const Text('요약 수정:', style: TextStyle(fontSize: 16)),
const SizedBox(height: 8),
TextField(
controller: _summaryController,
maxLines: 3,
decoration: const InputDecoration(
border: OutlineInputBorder(),
),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _saveSummary,
child: const Text('저장'),
),
],
),
),
);
}
}