dev/flutter
[Flutter] Calendar, DateTime, Intl, TimeOfDay
donguran
2024. 1. 20. 18:40
728x90
반응형
flutter pub add calendar
import
import 'package:table_calendar/table_calendar.dart';
code
body: SafeArea(
top: true,
child: TableCalendar(
focusedDay: DateTime.now(), // focus date
firstDay: DateTime(2019, 12, 5), // first date
lastDay: DateTime(2024, 1, 30), // last date
// --- required
selectedDayPredicate: (day) { // selected date predicate identify
final now = DateTime.now();
return DateTime(day.year, day.month, day.day).isAtSameMomentAs(
DateTime(now.year, now.month, now.day),
);
},
onDaySelected: (selectedDay, focusedDay) { // selected Date
},
onPageChanged: (focusedDay) { // change date page
},
rangeSelectionMode: RangeSelectionMode.toggledOff, // range mode
onRangeSelected: (start, end, focusedDay) { // selected range
},
),
),
DateTime값 알아보기
DateTime time = DateTime.now();
debugPrint("initState... time:$time");
debugPrint("initState... time:${time.year}");
debugPrint("initState... time:${time.month}");
debugPrint("initState... time:${time.day}");
debugPrint("initState... time:${time.hour}");
debugPrint("initState... time:${time.minute}");
debugPrint("initState... time:${time.second}");
결과
I/flutter ( 4086): initState... time:2024-03-13 16:42:33.123502
I/flutter ( 4086): initState... time:2024
I/flutter ( 4086): initState... time:3
I/flutter ( 4086): initState... time:13
I/flutter ( 4086): initState... time:16
I/flutter ( 4086): initState... time:42
I/flutter ( 4086): initState... time:33
final moonLanding = DateTime.parse('1969-07-20 20:18:02Z');
debugPrint("moonLanding : $moonLanding");
// 결과
I/flutter ( 4086): moonLanding : 1969-07-20 20:18:02.000Z
DB에는 일의 자리에 0을 넣어주어야 하니
'padLeft' 방식이 필요할 것이다.
var number = 1;
var paddedNumber = number.toString().padLeft(2, '0');
print(paddedNumber); // 출력: 01
그럼 시간을 AM, PM으로 표출할 방법은 없을까?
intl library사용
DB에 넣을 때 TEXT형식으로 넣었다가
: Iso8601은 연-월-일 순으로 표현되고, 시간은 시간:분:초 로 표현된다.
var test = DateTime.now().toIso8601String();
var result = DateTime.parse(test);
꺼낼 때는 다음과 같이 꺼낸다.
calendar deco
@override
Widget build(BuildContext context) {
return TableCalendar(
firstDay: DateTime(1800, 1, 1),
lastDay: DateTime(3000, 1, 1),
focusedDay: DateTime.now(),
headerStyle: HeaderStyle(
titleCentered: true,
formatButtonVisible: false,
titleTextStyle: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 16.0
),
),
calendarStyle: CalendarStyle(
isTodayHighlighted: false,
defaultDecoration: BoxDecoration(
borderRadius: BorderRadius.circular(6.0),
color: LIGHT_GREY_COLOR,
),
weekendDecoration: BoxDecoration(
borderRadius: BorderRadius.circular(6.0),
color: LIGHT_GREY_COLOR,
),
selectedDecoration: BoxDecoration(
borderRadius: BorderRadius.circular(6.0),
border: Border.all(color: PRIMARY_COLOR, width: 1.0),
),
defaultTextStyle: TextStyle(
fontWeight: FontWeight.w600,
color: DARK_GREY_COLOR,
),
weekendTextStyle: TextStyle(
fontWeight: FontWeight.w600,
color: DARK_GREY_COLOR,
),
selectedTextStyle: TextStyle(
fontWeight: FontWeight.w600,
color: PRIMARY_COLOR,
),
),
);
}
DateFormat
계속해서 외면했지만 해야한다.
String으로 24/01/16 형태를 DateTime으로 만들고 싶었다.
add
flutter pub add intl
import
import 'package:intl/intl.dart';
String dateString = "24/01/16" → DateTime형태로 바꾸기
DateFormat클래스 사용
String dateString = "24/01/16";
DateFormat dateFormat = DateFormat("yy/MM/dd");
DateTime date = dateFormat.parse(dateString);
debugPrint(date);
parse date:2024-01-16 00:00:00.000
2024-04-26-금 : TimeOfDay - showTimePicker를 사용하면서.
현재시간 : TimeOfDay.now()
String으로 포맷 : TimeOfDay.format(context)
String → TimeOfDay 포맷 :
[참조]
https://api.flutter.dev/flutter/material/TimeOfDay-class.html
728x90
반응형