main.dart
import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'diary_service.dart'; import 'home_page.dart'; void main() { runApp( MultiProvider( providers: [ ChangeNotifierProvider(create: (context) => DiaryService()), ], child: const MyApp(), ), ); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: HomePage(), ); } }
home_page.dart
import 'dart:collection'; import 'package:diary/diary_service.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:table_calendar/table_calendar.dart'; import 'diary_service.dart'; final _kEventSource = Map.fromIterable(List.generate(50, (index) => index), key: (item) => DateTime.utc(kFirstDay.year, kFirstDay.month, item * 5), value: (item) => List.generate( item % 4 + 1, (index) => Event('Event $item | ${index + 1}'))) ..addAll({ kToday: [ Event('Today\'s Event 1'), Event('Today\'s Event 2'), ], }); /// Using a [LinkedHashMap] is highly recommended if you decide to use a map. final kEvents = LinkedHashMap<DateTime, List<Event>>( equals: isSameDay, hashCode: getHashCode, )..addAll(_kEventSource); final kToday = DateTime.now(); final kFirstDay = DateTime(kToday.year, kToday.month - 3, kToday.day); final kLastDay = DateTime(kToday.year, kToday.month + 3, kToday.day); class HomePage extends StatefulWidget { const HomePage({Key? key}) : super(key: key); State<HomePage> createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { TextEditingController textController = TextEditingController(); late final ValueNotifier<List<Event>> _selectedEvents; DateTime _focusedDay = DateTime.now(); DateTime? _selectedDay; void initState() { super.initState(); _selectedDay = _focusedDay; _selectedEvents = ValueNotifier(_getEventsForDay(_selectedDay!)); } void dispose() { _selectedEvents.dispose(); super.dispose(); } List<Event> _getEventsForDay(DateTime day) { // Implementation example return kEvents[day] ?? []; } Widget build(BuildContext context) { return Consumer<DiaryService>( builder: (context, diaryService, child) { return Scaffold( body: Column( children: [ TableCalendar( firstDay: kFirstDay, lastDay: kLastDay, focusedDay: _focusedDay, selectedDayPredicate: (day) => isSameDay(_selectedDay, day), eventLoader: _getEventsForDay, onDaySelected: _onDaySelected, headerStyle: const HeaderStyle(headerMargin: EdgeInsets.only(top: 10)), ), const SizedBox(height: 4.0), Expanded( child: ValueListenableBuilder<List<Event>>( valueListenable: _selectedEvents, builder: (context, value, _) { return ListView.builder( itemCount: value.length, itemBuilder: (context, index) { return Container( margin: const EdgeInsets.symmetric( horizontal: 12.0, vertical: 4.0, ), decoration: BoxDecoration(), child: ListTile( onTap: () => print('${value[index]}'), title: Text('${value[index]}'), ), ); }, ); }, ), ), ], ), floatingActionButton: FloatingActionButton( child: Icon(Icons.add), onPressed: () { showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: Text("일기작성"), content: TextField( //autofocus: true, onChanged: (String value) {}, decoration: InputDecoration( labelText: '한 줄 일기를 작성해주세요.', ), onSubmitted: (String value) {}, controller: textController, ), actions: <Widget>[ TextButton( onPressed: () { Navigator.pop(context); }, child: Text("취소"), ), TextButton( onPressed: () { String diary = textController.text; < setState(() { print("onPress 작성: $diary"); textController.clear(); // BucketService 가져오기 DiaryService bucketService = context.read<DiaryService>(); bucketService.create(diary, _focusedDay); Navigator.pop(context); // 화면을 종료합니다. }); }, child: Text("작성"), ) ], ); }); }, ), ); }, ); } void _onDaySelected(DateTime selectedDay, DateTime focusedDay) { if (!isSameDay(_selectedDay, selectedDay)) { setState(() { _selectedDay = selectedDay; _focusedDay = focusedDay; }); _selectedEvents.value = _getEventsForDay(selectedDay); print("_onDaySelected"); print(_selectedEvents.value); } } } class Event { final String title; const Event(this.title); String toString() => title; } int getHashCode(DateTime key) { return key.day * 1000000 + key.month * 10000 + key.year; }
diary_service.dart
import 'package:flutter/material.dart'; class Diary { String text; // 내용 DateTime createdAt; // 작성 시간 Diary({ required this.text, required this.createdAt, }); toString() => '$createdAt $text'; } class DiaryService extends ChangeNotifier { /// Diary 목록 List<Diary> diaryList = []; /// 특정 날짜의 diary 조회 List<Diary> getByDate(DateTime date) { // TODO : 아래 내용을 지우고 구현해주세요. return diaryList; } /// Diary 작성 void create(String text, DateTime selectedDate) { diaryList.add(Diary(text: text, createdAt: selectedDate)); notifyListeners(); for (var user in diaryList) print(user); } /// Diary 수정 void update(DateTime createdAt, String newContent) { // TODO } /// Diary 삭제 void delete(DateTime createdAt) { // TODO } }