Skip to content

Commit

Permalink
Merge pull request #70 from codeforjapan/feature/#44-calendar-icon
Browse files Browse the repository at this point in the history
Feature/#44 calendar
  • Loading branch information
ayuki-joto authored Jun 7, 2023
2 parents 955d390 + 87b5151 commit 7d3eb35
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 38 deletions.
Binary file added images/evaluation_default.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
228 changes: 191 additions & 37 deletions lib/calendar.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gussuri/component/title_box.dart';
import 'package:gussuri/helper/DeviceData.dart';
import 'package:gussuri/sleepyEdit.dart';
import 'package:intl/intl.dart';
import 'package:table_calendar/table_calendar.dart';
import 'input.dart';
import 'utils.dart';

class Calendar extends StatefulWidget {
Expand All @@ -21,6 +23,7 @@ class _CalendarState extends State<Calendar> {
DateTime? _selectedDay;
Icon _rightChevron = const Icon(Icons.chevron_right, color: Colors.grey);
Icon _leftChevron = const Icon(Icons.chevron_left);
Color selectedColor = const Color.fromRGBO(177, 208, 255, 1);

@override
void initState() {
Expand Down Expand Up @@ -48,29 +51,49 @@ class _CalendarState extends State<Calendar> {
_selectedDay = selectedDay;
_focusedDay = focusedDay;
});
}
final events = _getEventsForDay(selectedDay);

_selectedEvents.value = _getEventsForDay(selectedDay);
if (events.isEmpty) {
Navigator.push(
context, MaterialPageRoute(builder: (context) => const Input()));
} else {
// TODO: Edit page用にパラメータを渡す
Navigator.push(
context, MaterialPageRoute(builder: (context) => const Input()));
}
}

String _getImagePath(DateTime targetDay) {
final events = _getEventsForDay(targetDay);
if (events.isEmpty) {
return 'images/evaluation_default.jpg';
} else {
var dysfunction = events.first.dysfunction;
return 'images/evaluation_$dysfunction.jpg';
}
}

Future<void> setEvents() async {
for (var index = 0; index < 2; index++) {
DateTime _date = DateTime(kFirstDay.year, kFirstDay.month + index);
DateTime date = DateTime(kFirstDay.year, kFirstDay.month + index);
final orderSnap = await FirebaseFirestore.instance
.collection(await DeviceData.getDeviceUniqueId())
.doc('${_date.year}')
.collection(DateFormat('MM').format(_date))
.doc('${date.year}')
.collection(DateFormat('MM').format(date))
.get();
orderSnap.docs.map((e) => e).forEach((res) {
final data = res.data();
kEvents.addAll({
DateTime.utc(_date.year, _date.month, int.parse(res.id)):
List.generate(1, (index) {
DateTime.utc(date.year, date.month, int.parse(res.id)):
List.generate(1, (index) {
return Event(
DateFormat('MM/dd H:m').format(DateTime.parse(data['bed_time'])),
DateFormat('MM/dd H:m').format(DateTime.parse(data['get_up_time'])),
res.reference.path
);
DateFormat('MM/dd H:m')
.format(DateTime.parse(data['bed_time'])),
DateFormat('MM/dd H:m')
.format(DateTime.parse(data['get_up_time'])),
data['dysfunction'],
res.reference.path);
})
});
});
Expand All @@ -86,31 +109,110 @@ class _CalendarState extends State<Calendar> {
body: Column(
children: [
const TitleBox(text: '睡眠記録カレンダー'),
TableCalendar(
firstDay: kFirstDay,
lastDay: kToday,
focusedDay: _focusedDay,
headerStyle: HeaderStyle(
formatButtonVisible: false,
leftChevronIcon: _leftChevron,
rightChevronIcon: _rightChevron,
titleCentered: true,
SizedBox(
height: 400,
child: TableCalendar(
shouldFillViewport: true,
firstDay: kFirstDay,
lastDay: kToday,
focusedDay: _focusedDay,
headerStyle: HeaderStyle(
formatButtonVisible: false,
leftChevronIcon: _leftChevron,
rightChevronIcon: _rightChevron,
titleCentered: true,
),
calendarBuilders: CalendarBuilders(
defaultBuilder: (context, day, focusedDay) {
// NOTE: defaultの日付が出てしまうため
return const Text('');
},
outsideBuilder: (context, day, focusedDay) {
// NOTE: defaultの日付が出てしまうため
return const Text('');
},
todayBuilder: (context, day, focusedDay) {
// NOTE: defaultの日付が出てしまうため
return const Text('');
},
markerBuilder: (context, day, focusedDay) {
// NOTE: defaultの日付が出てしまうため
return const Text('');
},
singleMarkerBuilder: (context, day, focusedDay) {
// NOTE: defaultの日付が出てしまうため
return const Text('');
},
rangeHighlightBuilder: (context, day, focusedDay) {
final imgPath = _getImagePath(day);
final today = DateTime.now();
final todayDate =
DateTime(today.year, today.month, today.day);
final dayDate = DateTime(day.year, day.month, day.day);
if (dayDate.isBefore(todayDate) || dayDate == todayDate) {
return CustomCel(imgPath: imgPath, day: day.day);
}
return null;
},
selectedBuilder: (context, day, focusedDay) {
final imgPath = _getImagePath(day);
final imgOpacity =
imgPath == 'images/evaluation_default.jpg' ? 0.5 : 1.0;
// NOTE:選択された時だけレイアウトが違うので共通化してない
return SizedBox(
width: 200,
height: 250,
child: Center(
child: Container(
margin: const EdgeInsets.only(top: 2),
decoration: BoxDecoration(
color: selectedColor,
borderRadius: BorderRadius.circular(14.0),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'${day.day}',
style: const TextStyle().copyWith(
fontSize: 13.0,
fontWeight: FontWeight.bold),
),
Container(
padding: const EdgeInsets.only(top: 2),
child: ClipOval(
child: Opacity(
opacity: imgOpacity,
child: Image(
image: AssetImage(imgPath),
width: 28,
height: 28,
)),
),
)
],
),
),
),
);
},
),
locale: 'ja_JP',
selectedDayPredicate: (day) => isSameDay(_selectedDay, day),
eventLoader: _getEventsForDay,
onDaySelected: _onDaySelected,
onPageChanged: (focusedDay) {
setState(() {
_rightChevron = isSameMonth(kToday, focusedDay)
? const Icon(Icons.chevron_right, color: Colors.grey)
: const Icon(Icons.chevron_right);
_leftChevron = isSameMonth(kFirstDay, focusedDay)
? const Icon(Icons.chevron_left, color: Colors.grey)
: const Icon(Icons.chevron_left);
});
_focusedDay = focusedDay;
},
),
locale: 'ja_JP',
selectedDayPredicate: (day) => isSameDay(_selectedDay, day),
eventLoader: _getEventsForDay,
onDaySelected: _onDaySelected,
onPageChanged: (focusedDay) {
setState(() {
_rightChevron = isSameMonth(kToday, focusedDay)
? const Icon(Icons.chevron_right, color: Colors.grey)
: const Icon(Icons.chevron_right);
_leftChevron = isSameMonth(kFirstDay, focusedDay)
? const Icon(Icons.chevron_left, color: Colors.grey)
: const Icon(Icons.chevron_left);
});
_focusedDay = focusedDay;
},
),
const SizedBox(height: 8.0),
Expanded(
Expand All @@ -131,9 +233,17 @@ class _CalendarState extends State<Calendar> {
),
child: ListTile(
onTap: () => {
Navigator.push(context, MaterialPageRoute(builder: (context) => SleepyEdit({value: value}, value: value[index].documentId,)))
// NOTE: edit page新しいのできたら
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SleepyEdit(
{value: value},
value: value[index].documentId,
)))
},
title: Text('ベッドに入った時間: ${value[index].bedtime}\nベットから出た時間: ${value[index].getUpTime}'),
title: Text(
'ベッドに入った時間: ${value[index].bedtime}\nベットから出た時間: ${value[index].getUpTime}'),
),
);
},
Expand All @@ -143,10 +253,54 @@ class _CalendarState extends State<Calendar> {
),
Container(
alignment: Alignment.bottomLeft,
padding: EdgeInsets.only(left:20.w),
padding: EdgeInsets.only(left: 20.w),
child: Image.asset('images/baku-kun-1.png'),
)
],
));
}
}

class CustomCel extends StatelessWidget {
final String imgPath;
final int day;

const CustomCel({super.key, required this.imgPath, required this.day});

@override
Widget build(BuildContext context) {
final imgOpacity = imgPath == 'images/evaluation_default.jpg' ? 0.3 : 1.0;

return SizedBox(
width: 200,
height: 250,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
day.toString(),
style: const TextStyle()
.copyWith(fontSize: 13.0, fontWeight: FontWeight.bold),
),
Container(
padding: const EdgeInsets.only(top: 2),
child: ClipOval(
child: Opacity(
opacity: imgOpacity,
child: Image(
image: AssetImage(imgPath),
width: 28,
height: 28,
errorBuilder: (BuildContext context, Object exception,
StackTrace? stackTrace) {
return const Text('Image not found');
})),
),
)
],
),
),
);
}
}
3 changes: 2 additions & 1 deletion lib/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ bool isSameMonth(DateTime? a, DateTime? b) {
class Event {
final String bedtime;
final String getUpTime;
final int dysfunction;
final String documentId;

const Event(this.bedtime, this.getUpTime, this.documentId);
const Event(this.bedtime, this.getUpTime, this.dysfunction, this.documentId);
}

final kEvents = <DateTime, List<Event>>{};
Expand Down

0 comments on commit 7d3eb35

Please sign in to comment.