แสดงปฏิทินด้วย Table calendar ใน Flutter

บทความใหม่ ยังไม่ถึงปี โดย Ninenik Narkdee
flutter table calendar ปฏิทิน

คำสั่ง การ กำหนด รูปแบบ ตัวอย่าง เทคนิค ลูกเล่น การประยุกต์ การใช้งาน เกี่ยวกับ flutter table_calendar ปฏิทิน

ดูแล้ว 663 ครั้ง


เนื้อหาตอนต่อไปนี้ เรามาดูเกี่ยวกับการใช้งานปฏิทินใน Flutter
เป็นรูปแบบการแสดงปฏิทินใน app ที่สามารถทำได้ง่าย รองรับการ
นำไปปรับประยุกต์ต่างๆมากมาย เช่น การทำปฏิทินวันสำคัญ การทำ
ปฏิทินกิจกรรม หรืออื่นๆ เป็นต้น โดยในที่นี้เราจะใช้ตัวแพ็กเก็จที่ชื่อว่า
table_calendar มาช่วยจัดการ
 

ติดตั้ง package ที่จำเป็นเพิ่มเติม ตามรายการด้านล่าง

    แพ็กเก็จที่จำเป็นต้องติดตั้งเพิ่มเติม สำหรับการทำงานมีดังนี้
 
  intl: ^0.19.0
  table_calendar: ^3.1.2  
 
สำหรับการใช้งาน table_calendar ที่มีเรื่องของการจัดการวันที่และเวลามาเกี่ยวข้อง ดังนั้นรูปแบบ
วันที่จึงมีความสำคัญ table_calendar จึงจำเป็นจะต้องใช้งานร่วมกับ intl ที่เป็นแพ็กเก็จเกี่ยวกับ
การจัดรูปแบบข้อความ ซึ่งรองรับวันที่ในภาษาไทยด้วย  โดยแพ็กเก็จ table_calendar แต่ละเวอร์ชั่น
อาจจะใช้ได้กับ intl เวอร์ชั่นที่รองรับ ดังนั้นในขั้นตอนการเพิ่มแพ็กเก็จ อาจจะมีแจ้งว่าต้องใช้คู่กับเวอร์ชั่น
ไหน ให้เราทำตามเงื่อนไข ที่แจ้งนั้นๆ ในตัวอย่างด้านบน สามารถใช้ร่วมกันได้
 
 

การเตรียมการกำหนดรูปแบบวันทีในแอป

    ในการจัดการการใช้งานรูปแบบวันที่ในแอป เราจะต้องกำหนดส่วนนี้เพิ่มเติม คือ  เราจะใช้ 
initializeDateFormatting('th_TH', null) เพื่อทำการ เตรียมการกำหนดรูปแบบวันที่ 
(date formatting) สำหรับภาษาไทยและการตั้งค่าภูมิภาคของไทย (th_TH) ก่อนที่แอปพลิเคชัน
จะเริ่มทำงาน (runApp)
 
1
2
3
4
5
6
7
8
void main() async { 
  // ใช้เพื่อให้แน่ใจว่า Flutter framework ได้รับการเริ่มต้น (initialized)
  // ก่อนที่จะมีการเรียกใช้หรือดำเนินการใด ๆ ที่ต้องการการเข้าถึงส่วนประกอบของ framework
  WidgetsFlutterBinding.ensureInitialized();
  // เตรียมการกำหนดรูปแบบวันที่
  initializeDateFormatting('th_TH', null).then((_) => runApp(const MyApp())); 
 // runApp(const MyApp());
}
 
 
 

วิธีการแสดงปฏิทินด้วย Table calendar

    ต่อไปก็เป็นส่วนของโค้ดตัวอย่างเบื้องต้น ที่เราปรับแต่งเล็กน้อย ยังไม่ได้ใส่อะไรลงไปมาก คำอธิบาย
แสดงในโค้ด จะเป็นการแสดงปฏิทินธรรมดา
 

ไฟล์ calendar.dart

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import 'package:flutter/material.dart';
import 'package:table_calendar/table_calendar.dart';
import 'package:intl/intl.dart';
 
class Calendars extends StatefulWidget {
  static const routeName = '/calendar';
 
  const Calendars({Key? key}) : super(key: key);
 
  @override
  State<StatefulWidget> createState() {
    return _CalendarsState();
  }
}
 
class _CalendarsState extends State<Calendars> {
 
  @override
  void initState() {
    super.initState();
 
  }
 
  @override
  void setState(VoidCallback fn) {
    if (mounted) {
      super.setState(fn);
    }
  }
 
  // ฟังก์ชั่น การแปลงปี ค.ศ. (Gregorian year) เป็นพุทธศักราช (Buddhist year)
  String _getThaiYear(int year) {
    return '${year + 543}';
  }
 
  // ฟังก์ชั่นสำหรับแปลงเป้ฯเดือนไทย
  String _getThaiMonth(int month) {
    const thaiMonths = [
      'มกราคม','กุมภาพันธ์','มีนาคม','เมษายน','พฤษภาคม','มิถุนายน',
      'กรกฎาคม','สิงหาคม','กันยายน','ตุลาคม','พฤศจิกายน','ธันวาคม'
    ];
    return thaiMonths[month - 1];
  }
 
  // ฟังก์ชั่นสำหรับเปลี่ยนวัน เดือน ปี เป็นภาษาไทย
  String _getThaiDateShow(String dateString) {
    const List<String> thaiDays = [
      'อาทิตย์','จันทร์','อังคาร','พุธ','พฤหัสบดี','ศุกร์','เสาร์'
    ];
 
    // กำหนดชื่อเดือนในภาษาไทย
    const List<String> thaiMonths = [
      'มกราคม','กุมภาพันธ์','มีนาคม','เมษายน','พฤษภาคม','มิถุนายน',
      'กรกฎาคม','สิงหาคม','กันยายน','ตุลาคม','พฤศจิกายน','ธันวาคม'
    ];
 
    // แปลงรูปแบบวันที่จากข้อมูลมาเป็น DateTime
    DateTime date = DateTime.parse(dateString);
 
    // แปลงปี ค.ศ. เป็นปี พ.ศ.
    int thaiYear = date.year + 543;
 
    // กำหนดชื่อวันในภาษาไทย
    String dayName =
        thaiDays[date.weekday - 1]; // Date.weekday returns 1-7 (Mon-Sun)
 
    // กำหนดชื่อเดือนในภาษาไทย
    String monthName = thaiMonths[date.month - 1]; // Date.month returns 1-12
 
    // จัดรูปแบบแสดงผลวัน เดือน ปี เป็นภาษาไทย เช่น
    // เสาร์ 12 ตุลาคม 2567
    String formattedDate = '$dayName ${date.day} $monthName $thaiYear';
 
    return formattedDate;
  }
 
  @override
  void dispose() {
    super.dispose();
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Calendar'),
      ),
      body: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              TableCalendar(
                // rowHeight: 70.0,
                // กำหนดความสูงของวันต่างๆ ในสัปดาห์
                daysOfWeekHeight: 30.0,
                headerStyle: HeaderStyle(
                  // ส่วนจัดรูปแบบแถบ เดือนปี ด้านบน
                  titleTextFormatter: (date, locale) {
                    // ในที่นี้เราจะเอาเดือนและปีในปฏิทิน ไปใช้เพื่อสร้างรูปแบบ เดือนและปี
                    // เป็นภาษาไทย ในที่นี้จะได้เป็น ตุลาคม 2567
                    final year = date.year;
                    final month = date.month;
                    final formattedYear = _getThaiYear(year);
                    final formattedMonth = _getThaiMonth(month);
                    // ส่งกลับผลลัพธ์รูปแบบเดือน ปี ตรงแถบหัวข้อด้านบน  ตุลาคม 2567
                    return '$formattedMonth $formattedYear';
                  },
                  headerPadding: const EdgeInsets.symmetric(vertical: 0.0),
                  decoration: const BoxDecoration(
                    // color: Color(0xFFffefbf), // กำหนดสีพื้นหลัง
                  ),
                  // ปิดปุ่มเลือกรูปแบบ ถ้าเปิดต้องกำหนดค่า onFormatChanged เพิ่มเติม
                  formatButtonVisible: false, // ปิดไว้
                  titleCentered: true, // แสดง เดือน ปี ตรงกลาง
                  titleTextStyle: const TextStyle( // สีและขนาด
                    color: Colors.black54,
                    fontWeight: FontWeight.bold,
                    fontSize: 20.0,
                  ),
                ),
                // กำหนดสี ขนาด ของชื่อวัน 7 วัน
                daysOfWeekStyle: const DaysOfWeekStyle(
                  weekdayStyle: TextStyle(
                    color: Colors.black26,
                    fontWeight: FontWeight.bold,
                    fontSize: 20.0,
                  ),
                ),
                // กำหนดรูปแบบสีต่างๆ และรูปแบบการแสดงค่าหลัก
                calendarStyle: const CalendarStyle(
                  // วันปัจจุบันสีข้อความ ขนาดข้อความ
                  todayTextStyle: TextStyle(
                    color: Color.fromARGB(255, 178, 16, 22),
                    fontSize: 20.0,
                  ),
                  // วันที่ปัจจุบันพื้นหลัง รูปแบบรูปร่างที่แสดง
                  todayDecoration: BoxDecoration(
                    color: Color.fromARGB(200, 255, 229, 212),
                    shape: BoxShape.circle, // แสดงแบบวงกลม
                  ),
                  outsideDaysVisible: false, // แสดงวันที่ที่ไม่ใช่ของเดือนหรือไม่
                  outsideTextStyle: TextStyle(// รูปแบบวันที่ที่ไม่ใช่ของเดือน ถ้าแสดง
                    color: Colors.black26,
                    fontSize: 20.0,
                  ),
                  defaultTextStyle: TextStyle( // วันที่ปกติ
                    color: Colors.black87,
                    fontSize: 20.0,
                  ),
                  weekendTextStyle: TextStyle( // สีของวันหยุด ถ้ากำหนดรายการวันหยุด
                    color: Colors.red,
                    fontSize: 20.0,
                  ),
                ),
                locale: 'th_TH', // ใช้รูปแบบวันที่เป็นภาษาไทย
                // กำหนดวันเริ่มต้น และวันสุดท้ายในปฏิทิน  ที่สามารถเลื่อนได้
                // focusedDay ต้องอยู่ระหว่าง ช่วงของ firstDay และ lastDay
                // ไม่เช่นนั้นจะเกิด error ได้
                // DateTime.utc(2010, 10, 16) จะได้เป็น 2010-10-16 00:00:00.000Z
                // DateTime.utc(2010, 10, 16).toLocal() จะได้เป็น 2010-10-16 07:00:00.000
                firstDay: DateTime.utc(2010, 10, 16),
                lastDay: DateTime.utc(2030, 3, 14),
                focusedDay: DateTime.now(), // วันที่จะเน้น ในที่นี้ใช้เป็นวันที่ปัจจุบัน
                calendarFormat: CalendarFormat.month, // แสดงปฏิทินแบบเดือน
                calendarBuilders: CalendarBuilders(
                  // ส่วนจัดรูปแบบของวันที่ต่างๆ ในปฏิทิน
                  defaultBuilder: (context, day, focusedDay) {
                        return Container(
                          margin: const EdgeInsets.all(6.0),
                          alignment: Alignment.center,
                          child: Stack(
                            alignment: Alignment.center,
                            children: [
                              Center(
                                child: Text(
                                  '${day.day}',
                                  style: const TextStyle(
                                    fontSize: 20,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        );
                  },
                  // ส่วนจัดรูปแบบชื่อวันแบบย่อในปฏิทิน ในที่นี้ ตรวจว่าเป็นวัน
                  // เสาร์ หรืออาทิตย์ แล้วให้แสดงสีข้อความเป็นสีแดง
                  dowBuilder: (context, day) {
                    if (day.weekday == DateTime.sunday ||
                        day.weekday == DateTime.saturday) {
                      // ใช้แพ็กเก็จ intl จัดรูปแบบเอาวันของข้อมูลวันที่ มาแสดง
                      final textDay = DateFormat.E('th').format(day);
                      return Center(
                        child: Text(
                          textDay,
                          style: const TextStyle(
                            color: Colors.red,
                            fontSize: 20.0,
                          ),
                        ),
                      );
                    }
                    return null; // ถ้าเป็นวันอื่นๆ ไม่ต้องจัดรูปแบบเพิ่มเติม
                  },
                ),
                // เมื่อเลื่อนเปลี่ยนหน้าข้อมูล เช่น เปลี่ยนเดือน
                onPageChanged: (day) {
 
                },
                // เมื่อกดเลือกวันในปฏิทิน
                onDaySelected: (day, day2) {
             
                },
                // ส่วนสำหรับกำหนด event กิจกรรมในปฏิทิน
                eventLoader: (day) {
                  return [];
                },
              ),
            ],
          )
        ),
    );
  }
}
 

ผลลัพธ์ที่ได้

 



 
 
ในตัวอย่าง เราปรับแต่งเล็กน้อยให้รองรับ เดือนและปี เป็นภาษาไทยและใช้ปี พ.ศ. แทน ค.ศ. มีส่วนปรับ
แต่งเพิ่มเติม ที่เราจะประยุกต์ได้ เช่น การเลือกวันที่แล้วทำงานอะไร การเปลี่ยนหน้า เช่นเปลียนเดือน
แล้วอยากให้ทำหรือแสดงอะไร รวมทั้งการปรับแต่งรูปแบบต่างๆ ไม่ว่าจะเป็นสีพื้นหลัง สีข้อความ เหล่านี้
ก็สามารถทำได้ง่ายตามคำอธิบายแต่ละจุดในโค้ด
 
 

ประยุกต์ให้สามารถเลือกวันที่ได้

    ต่อไปเราจะประยุกต์ต่อ ซึ่งถ้าหากต้องการแสดงแบบเรียบง่ายก็ใช้โค้ดด้านบน แต่ถ้าจะปรับแต่งเพิ่ม
ก็ให้ใช้โค้ดตัวนี้ ส่วนนี้จะมีการเพิ่มของ วันที่เลือกเข้ามา นั่นคือ กดเลือกวันที่ใดๆ ในปฏิทิน ก็จะแสดง
ไฮไลท์วันนที่นั้น และเพิ่มส่วนของการ เลือกวันจากหน้าต่างตัวเลือกวันที่ เพื่อไปยังเดือนที่ต้องการ ซึ่งเดิม
เราจะใช้วิธีเลื่อนไปมาซ้ายขวา ไปยังเดือนต่างๆ แต่เราสามารถเลือกไปยังเดือนหรือไปได้ง่ายด้วยตัวเลือก
ที่เพิ่มเข้ามา
 

ไฟล์ calendar.dart (ปรับปรุงมีเลือกวันที่)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import 'package:flutter/material.dart';
import 'package:table_calendar/table_calendar.dart';
import 'package:intl/intl.dart';
 
class Calendars extends StatefulWidget {
  static const routeName = '/calendar';
 
  const Calendars({Key? key}) : super(key: key);
 
  @override
  State<StatefulWidget> createState() {
    return _CalendarsState();
  }
}
 
class _CalendarsState extends State<Calendars> {
 
  // กำหนดวันเริ่มต้น และวันสุดท้ายในปฏิทิน  ที่สามารถเลื่อนได้
  // focusedDay ต้องอยู่ระหว่าง ช่วงของ firstDay และ lastDay
  // ไม่เช่นนั้นจะเกิด error ได้
  // DateTime.utc(2010, 10, 16) จะได้เป็น 2010-10-16 00:00:00.000Z
  // DateTime.utc(2010, 10, 16).toLocal() จะได้เป็น 2010-10-16 07:00:00.000
  final DateTime _firstDay = DateTime.utc(2010, 10, 16);
  final DateTime _lastDay = DateTime.utc(2030, 3, 14);
  DateTime _focusedDay = DateTime.now(); // วันที่ที่เลือก เพื่อแสดงใช้สำหรับแสดงปฏิทิน
  DateTime? _selectedDay; // ตัวแปรเก็บวันที่ที่เลือก (ที่กดในปฏิทิน เพื่อเลือกวันที่ไปใช้งาน)
 
  @override
  void initState() {
    super.initState();
    _selectedDay = _focusedDay; // กำหนดค่าเริ่มต้น เป็นค่าเดียวกับวันปัจจุบัน
  }
 
  @override
  void setState(VoidCallback fn) {
    if (mounted) {
      super.setState(fn);
    }
  }
 
  // ฟังก์ชั่นตรวจสอบถ้าเป็นวันที่เดียวกันไหม
  bool isSameDay(DateTime? a, DateTime? b) {
    if (a == null || b == null) {
      return false;
    }
    return a.year == b.year && a.month == b.month && a.day == b.day;
  }
 
  // ฟังก์ชั่นสำหรับเลือกวันที่
  void _selectDate() async {
       
    final DateTime now = _focusedDay; // ใช้จากวันที่เลือก หรือ DateTime.now();
    final DateTime firstDate = _firstDay; //  ช่วงเริ่มต้น
    final DateTime lastDate = _lastDay; //  ช่วงสิ้นสิน
    final DateTime initialDate = now.isAfter(lastDate) ? lastDate : now;
 
    // รอการเลือกวันที่ในปฏิทิน ได้วันที่แล้วส่งกลับไปใช้งาน
    final DateTime? newDate = await showDatePicker(
      context: context,
      initialDate: initialDate,
      firstDate: firstDate,
      lastDate: lastDate,
      helpText: 'Select a date',
    );
    if (newDate != null) {
      setState(() {
        // กำหนดค่าวันที่โฟกัส เป็นค่าใหม่ที่เลือก
        _focusedDay = newDate;
        // กำหนดวันที่เลือกในปฏิทิน เป็นค่าใหม่ที่เลือก
        _selectedDay = newDate;
      });
    }
  }   
 
  // ฟังก์ชั่น การแปลงปี ค.ศ. (Gregorian year) เป็นพุทธศักราช (Buddhist year)
  String _getThaiYear(int year) {
    return '${year + 543}';
  }
 
  // ฟังก์ชั่นสำหรับแปลงเป้ฯเดือนไทย
  String _getThaiMonth(int month) {
    const thaiMonths = [
      'มกราคม','กุมภาพันธ์','มีนาคม','เมษายน','พฤษภาคม','มิถุนายน',
      'กรกฎาคม','สิงหาคม','กันยายน','ตุลาคม','พฤศจิกายน','ธันวาคม'
    ];
    return thaiMonths[month - 1];
  }
 
  // ฟังก์ชั่นสำหรับเปลี่ยนวัน เดือน ปี เป็นภาษาไทย
  String _getThaiDateShow(String dateString) {
    const List<String> thaiDays = [
      'อาทิตย์','จันทร์','อังคาร','พุธ','พฤหัสบดี','ศุกร์','เสาร์'
    ];
 
    // กำหนดชื่อเดือนในภาษาไทย
    const List<String> thaiMonths = [
      'มกราคม','กุมภาพันธ์','มีนาคม','เมษายน','พฤษภาคม','มิถุนายน',
      'กรกฎาคม','สิงหาคม','กันยายน','ตุลาคม','พฤศจิกายน','ธันวาคม'
    ];
 
    // แปลงรูปแบบวันที่จากข้อมูลมาเป็น DateTime
    DateTime date = DateTime.parse(dateString);
 
    // แปลงปี ค.ศ. เป็นปี พ.ศ.
    int thaiYear = date.year + 543;
 
    // กำหนดชื่อวันในภาษาไทย
    String dayName =
        thaiDays[date.weekday - 1]; // Date.weekday returns 1-7 (Mon-Sun)
 
    // กำหนดชื่อเดือนในภาษาไทย
    String monthName = thaiMonths[date.month - 1]; // Date.month returns 1-12
 
    // จัดรูปแบบแสดงผลวัน เดือน ปี เป็นภาษาไทย เช่น
    // เสาร์ 12 ตุลาคม 2567
    String formattedDate = '$dayName ${date.day} $monthName $thaiYear';
 
    return formattedDate;
  }
 
  @override
  void dispose() {
    super.dispose();
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Calendar'),
        actions: [
          TextButton(
            onPressed: (){
              setState(() {
                //  กำหนดค่าวันที่โฟกัส กลับมาวันที่ปัจจุบัน
                _focusedDay = DateTime.now();
                // กำหนดวันที่เลือกในปฏิทิน กลับมาวันที่ปัจจุบัน
                _selectedDay = DateTime.now();            
              });          
            },
            child: const Text('Today'),
          ),        
          IconButton(
            onPressed: _selectDate, // เลือกวันที่จากปฏิทิน
            icon: const Icon(Icons.calendar_month), // ใช้ไอคอนปฏิทิน
          ),
        ],
      ),
      body: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              TableCalendar(
                // rowHeight: 70.0,
                // กำหนดความสูงของวันต่างๆ ในสัปดาห์
                daysOfWeekHeight: 30.0,
                headerStyle: HeaderStyle(
                  // ส่วนจัดรูปแบบแถบ เดือนปี ด้านบน
                  titleTextFormatter: (date, locale) {
                    // ในที่นี้เราจะเอาเดือนและปีในปฏิทิน ไปใช้เพื่อสร้างรูปแบบ เดือนและปี
                    // เป็นภาษาไทย ในที่นี้จะได้เป็น ตุลาคม 2567
                    final year = date.year;
                    final month = date.month;
                    final formattedYear = _getThaiYear(year);
                    final formattedMonth = _getThaiMonth(month);
                    // ส่งกลับผลลัพธ์รูปแบบเดือน ปี ตรงแถบหัวข้อด้านบน  ตุลาคม 2567
                    return '$formattedMonth $formattedYear';
                  },
                  headerPadding: const EdgeInsets.symmetric(vertical: 0.0),
                  decoration: const BoxDecoration(
                    color: Color(0xFFffefbf), // กำหนดสีพื้นหลัง
                  ),
                  // ปิดปุ่มเลือกรูปแบบ ถ้าเปิดต้องกำหนดค่า onFormatChanged เพิ่มเติม
                  formatButtonVisible: false, // ปิดไว้
                  titleCentered: true, // แสดง เดือน ปี ตรงกลาง
                  titleTextStyle: const TextStyle( // สีและขนาด
                    color: Colors.black54,
                    fontWeight: FontWeight.bold,
                    fontSize: 20.0,
                  ),
                ),
                // กำหนดสี ขนาด ของชื่อวัน 7 วัน
                daysOfWeekStyle: const DaysOfWeekStyle(
                  weekdayStyle: TextStyle(
                    color: Colors.black26,
                    fontWeight: FontWeight.bold,
                    fontSize: 20.0,
                  ),
                ),
                // กำหนดรูปแบบสีต่างๆ และรูปแบบการแสดงค่าหลัก
                calendarStyle: const CalendarStyle(
                  // วันปัจจุบันสีข้อความ ขนาดข้อความ
                  todayTextStyle: TextStyle(
                    color: Color.fromARGB(255, 178, 16, 22),
                    fontSize: 20.0,
                  ),
                  // วันที่ปัจจุบันพื้นหลัง รูปแบบรูปร่างที่แสดง
                  todayDecoration: BoxDecoration(
                    color: Color.fromARGB(200, 255, 229, 212),
                    shape: BoxShape.circle, // แสดงแบบวงกลม
                  ),
                  // วันที่กดเลือก สีข้อความ ขนาดข้อความ
                  selectedTextStyle: TextStyle(
                    color: Color.fromARGB(200, 255, 229, 212),
                    fontSize: 20.0,
                  ),
                  // วันที่กดเลือก พื้นหลัง รูปแบบรูปร่างที่แสดง
                  selectedDecoration: BoxDecoration(
                    color: Color.fromARGB(255, 178, 16, 22),
                    shape: BoxShape.circle, // แสดงแบบวงกลม
                  ),                 
                  outsideDaysVisible: false, // แสดงวันที่ที่ไม่ใช่ของเดือนหรือไม่
                  outsideTextStyle: TextStyle(// รูปแบบวันที่ที่ไม่ใช่ของเดือน ถ้าแสดง
                    color: Colors.black26,
                    fontSize: 20.0,
                  ),
                  defaultTextStyle: TextStyle( // วันที่ปกติ
                    color: Colors.black87,
                    fontSize: 20.0,
                  ),
                  weekendTextStyle: TextStyle( // สีของวันหยุด ถ้ากำหนดรายการวันหยุด
                    color: Colors.red,
                    fontSize: 20.0,
                  ),
                ),
                locale: 'th_TH', // ใช้รูปแบบวันที่เป็นภาษาไทย
                // กำหนดวันเริ่มต้น และวันสุดท้ายในปฏิทิน  ที่สามารถเลื่อนได้
                // focusedDay ต้องอยู่ระหว่าง ช่วงของ firstDay และ lastDay
                // ไม่เช่นนั้นจะเกิด error ได้
                // DateTime.utc(2010, 10, 16) จะได้เป็น 2010-10-16 00:00:00.000Z
                // DateTime.utc(2010, 10, 16).toLocal() จะได้เป็น 2010-10-16 07:00:00.000
                firstDay: _firstDay,
                lastDay: _lastDay,
                focusedDay: _focusedDay, // วันที่จะเน้น ในที่นี้ใช้เป็นวันที่จากที่เลือกหรือกำหนด
                // กำหนดถ้าเป็นวันที่ตรงกับวันที่เลือก ให้ทำสัญลักษณ์ว่าถูกเลือกในวันนั้นๆ
                selectedDayPredicate: (day) => isSameDay(_selectedDay, day),
                calendarFormat: CalendarFormat.month, // แสดงปฏิทินแบบเดือน
                calendarBuilders: CalendarBuilders(
                  // ส่วนจัดรูปแบบของวันที่ต่างๆ ในปฏิทิน
                  defaultBuilder: (context, day, focusedDay) {
                        return Container(
                          margin: const EdgeInsets.all(6.0),
                          alignment: Alignment.center,
                          child: Stack(
                            alignment: Alignment.center,
                            children: [
                              Center(
                                child: Text(
                                  '${day.day}',
                                  style: const TextStyle(
                                    fontSize: 20,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        );
                  },
                  // ส่วนจัดรูปแบบชื่อวันแบบย่อในปฏิทิน ในที่นี้ ตรวจว่าเป็นวัน
                  // เสาร์ หรืออาทิตย์ แล้วให้แสดงสีข้อความเป็นสีแดง
                  dowBuilder: (context, day) {
                    if (day.weekday == DateTime.sunday ||
                        day.weekday == DateTime.saturday) {
                      // ใช้แพ็กเก็จ intl จัดรูปแบบเอาวันของข้อมูลวันที่ มาแสดง
                      final textDay = DateFormat.E('th').format(day);
                      return Center(
                        child: Text(
                          textDay,
                          style: const TextStyle(
                            color: Colors.red,
                            fontSize: 20.0,
                          ),
                        ),
                      );
                    }
                    return null; // ถ้าเป็นวันอื่นๆ ไม่ต้องจัดรูปแบบเพิ่มเติม
                  },
                ),
                // เมื่อเลื่อนเปลี่ยนหน้าข้อมูล เช่น เปลี่ยนเดือน
                onPageChanged: (day) {
                
                },
                // เมื่อกดเลือกวันในปฏิทิน
                onDaySelected: (selectedDay, focusedDay) {
                  // ถ้าไม่ใช่วันที่เดียวกัน
                  if (!isSameDay(_selectedDay, selectedDay)) {
                    setState(() {
                      _selectedDay = selectedDay;
                      _focusedDay = focusedDay;
                    });
                  }
                },
                // ส่วนสำหรับกำหนด event กิจกรรมในปฏิทิน
                eventLoader: (day) {       
                  return [];
                },
              ),
            ],
          )
        ),
    );
  }
}
 
 

ผลลัพธ์ที่ได้


 

 
ในโค้ดใหม่มีการปรับแต่งจากเติมเล็กน้อย เพิ่มปุ่มที่สามารถไปเลือกวันที่ต่างๆ ได้ง่าย รวมถึงปุ่มที่จะ
สามารถกลับมายังวันที่ปัจจุบันได้ ถ้าเลือกวันที่ใดๆ ก็จะไฮไลท์วันที่นั้นๆ
 
 

ประยุกต์แสดงวันพระในปฏิทิน

    เนื้อหานี้เราจะประยุกต์เพิ่มอีกเล็กน้อย ด้วยการเพิ่มวันพระเข้าไปในปฏิทิน โดยจะใช้การประยุกต์จาก
ฟังก์ชั่น php ในบทความเกี่ยวกับข้างขึ้นข้างแรม 
    แนวทางฟังก์ชั่น php อย่างง่ายกับการหาวันข้างขึ้นข้างแรม http://niik.in/1021
 
    โดยเราจะทำการโหลดรายการวันพระของปีๆ นั้นขึ้นมาเมื่อเปิดปฏิทินขึ้นมาจากนั้นก็นำไปแสดงเป็น
จุดสีเหลืองทองมุมบนซ้ายของวันที่ที่เป็นวันพระของแต่ละเดือน สามารถไปปรับแต่งเพิ่มเติมได้
 

ไฟล์ calendar.dart (เพิ่มวันพระเข้าไป)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import 'package:flutter/material.dart';
import 'package:table_calendar/table_calendar.dart';
import 'package:intl/intl.dart';
 
class Calendars extends StatefulWidget {
  static const routeName = '/calendar';
 
  const Calendars({Key? key}) : super(key: key);
 
  @override
  State<StatefulWidget> createState() {
    return _CalendarsState();
  }
}
 
class _CalendarsState extends State<Calendars> {
 
  // กำหนดวันเริ่มต้น และวันสุดท้ายในปฏิทิน  ที่สามารถเลื่อนได้
  // focusedDay ต้องอยู่ระหว่าง ช่วงของ firstDay และ lastDay
  // ไม่เช่นนั้นจะเกิด error ได้
  // DateTime.utc(2010, 10, 16) จะได้เป็น 2010-10-16 00:00:00.000Z
  // DateTime.utc(2010, 10, 16).toLocal() จะได้เป็น 2010-10-16 07:00:00.000
  final DateTime _firstDay = DateTime.utc(2010, 10, 16);
  final DateTime _lastDay = DateTime.utc(2030, 3, 14);
  DateTime _focusedDay = DateTime.now(); // วันที่ที่เลือก เพื่อแสดงใช้สำหรับแสดงปฏิทิน
  DateTime? _selectedDay; // ตัวแปรเก็บวันที่ที่เลือก (ที่กดในปฏิทิน เพื่อเลือกวันที่ไปใช้งาน)
 
  // สร้างตัวแปรสำหรับเก็บวันพระ
  Map<String, String> _BuddhaDay = {};
 
  @override
  void initState() {
    super.initState();
    _selectedDay = _focusedDay; // กำหนดค่าเริ่มต้น เป็นค่าเดียวกับวันปัจจุบัน
    // ใช้วันที่ปัจจุบันเพื่อส่งไปดึงข้อมูลวันพระของปีที่กำลังแสดง
    String _inputDate = DateFormat('yyyy-MM-01').format(_focusedDay); 
    // ดึงข้อมูลวันพระมาเก็บไว้ในตัวแปรไว้ใช้งาน
    _BuddhaDay = getWaxingCrescentInfo(_inputDate,getlistBuddshDay: true);
  }
 
  @override
  void setState(VoidCallback fn) {
    if (mounted) {
      super.setState(fn);
    }
  }
 
  // ฟังก์ชั่นตรวจสอบถ้าเป็นวันที่เดียวกันไหม
  bool isSameDay(DateTime? a, DateTime? b) {
    if (a == null || b == null) {
      return false;
    }
    return a.year == b.year && a.month == b.month && a.day == b.day;
  }
 
  // ฟังก์ชั่นสำหรับเลือกวันที่
  void _selectDate() async {
       
    final DateTime now = _focusedDay; // ใช้จากวันที่เลือก หรือ DateTime.now();
    final DateTime firstDate = _firstDay; //  ช่วงเริ่มต้น
    final DateTime lastDate = _lastDay; //  ช่วงสิ้นสิน
    final DateTime initialDate = now.isAfter(lastDate) ? lastDate : now;
 
    // รอการเลือกวันที่ในปฏิทิน ได้วันที่แล้วส่งกลับไปใช้งาน
    final DateTime? newDate = await showDatePicker(
      context: context,
      initialDate: initialDate,
      firstDate: firstDate,
      lastDate: lastDate,
      helpText: 'Select a date',
    );
    if (newDate != null) {
      setState(() {
        // กำหนดค่าวันที่โฟกัส เป็นค่าใหม่ที่เลือก
        _focusedDay = newDate;
        // กำหนดวันที่เลือกในปฏิทิน เป็นค่าใหม่ที่เลือก
        _selectedDay = newDate;
      });
    }
  }   
 
  // ฟังก์ชั่น การแปลงปี ค.ศ. (Gregorian year) เป็นพุทธศักราช (Buddhist year)
  String _getThaiYear(int year) {
    return '${year + 543}';
  }
 
  // ฟังก์ชั่นสำหรับแปลงเป้ฯเดือนไทย
  String _getThaiMonth(int month) {
    const thaiMonths = [
      'มกราคม','กุมภาพันธ์','มีนาคม','เมษายน','พฤษภาคม','มิถุนายน',
      'กรกฎาคม','สิงหาคม','กันยายน','ตุลาคม','พฤศจิกายน','ธันวาคม'
    ];
    return thaiMonths[month - 1];
  }
 
  // ฟังก์ชั่นสำหรับเปลี่ยนวัน เดือน ปี เป็นภาษาไทย
  String _getThaiDateShow(String dateString) {
    const List<String> thaiDays = [
      'อาทิตย์','จันทร์','อังคาร','พุธ','พฤหัสบดี','ศุกร์','เสาร์'
    ];
 
    // กำหนดชื่อเดือนในภาษาไทย
    const List<String> thaiMonths = [
      'มกราคม','กุมภาพันธ์','มีนาคม','เมษายน','พฤษภาคม','มิถุนายน',
      'กรกฎาคม','สิงหาคม','กันยายน','ตุลาคม','พฤศจิกายน','ธันวาคม'
    ];
 
    // แปลงรูปแบบวันที่จากข้อมูลมาเป็น DateTime
    DateTime date = DateTime.parse(dateString);
 
    // แปลงปี ค.ศ. เป็นปี พ.ศ.
    int thaiYear = date.year + 543;
 
    // กำหนดชื่อวันในภาษาไทย
    String dayName =
        thaiDays[date.weekday - 1]; // Date.weekday returns 1-7 (Mon-Sun)
 
    // กำหนดชื่อเดือนในภาษาไทย
    String monthName = thaiMonths[date.month - 1]; // Date.month returns 1-12
 
    // จัดรูปแบบแสดงผลวัน เดือน ปี เป็นภาษาไทย เช่น
    // เสาร์ 12 ตุลาคม 2567
    String formattedDate = '$dayName ${date.day} $monthName $thaiYear';
 
    return formattedDate;
  }
 
  @override
  void dispose() {
    super.dispose();
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Calendar'),
        actions: [
          TextButton(
            onPressed: (){
              setState(() {
                //  กำหนดค่าวันที่โฟกัส กลับมาวันที่ปัจจุบัน
                _focusedDay = DateTime.now();
                // กำหนดวันที่เลือกในปฏิทิน กลับมาวันที่ปัจจุบัน
                _selectedDay = DateTime.now();            
              });          
            },
            child: const Text('Today'),
          ),        
          IconButton(
            onPressed: _selectDate, // เลือกวันที่จากปฏิทิน
            icon: const Icon(Icons.calendar_month), // ใช้ไอคอนปฏิทิน
          ),
        ],
      ),
      body: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              TableCalendar(
                // rowHeight: 70.0,
                // กำหนดความสูงของวันต่างๆ ในสัปดาห์
                daysOfWeekHeight: 30.0,
                headerStyle: HeaderStyle(
                  // ส่วนจัดรูปแบบแถบ เดือนปี ด้านบน
                  titleTextFormatter: (date, locale) {
                    // ในที่นี้เราจะเอาเดือนและปีในปฏิทิน ไปใช้เพื่อสร้างรูปแบบ เดือนและปี
                    // เป็นภาษาไทย ในที่นี้จะได้เป็น ตุลาคม 2567
                    final year = date.year;
                    final month = date.month;
                    final formattedYear = _getThaiYear(year);
                    final formattedMonth = _getThaiMonth(month);
                    // ส่งกลับผลลัพธ์รูปแบบเดือน ปี ตรงแถบหัวข้อด้านบน  ตุลาคม 2567
                    return '$formattedMonth $formattedYear';
                  },
                  headerPadding: const EdgeInsets.symmetric(vertical: 0.0),
                  decoration: const BoxDecoration(
                    color: Color(0xFFffefbf), // กำหนดสีพื้นหลัง
                  ),
                  // ปิดปุ่มเลือกรูปแบบ ถ้าเปิดต้องกำหนดค่า onFormatChanged เพิ่มเติม
                  formatButtonVisible: false, // ปิดไว้
                  titleCentered: true, // แสดง เดือน ปี ตรงกลาง
                  titleTextStyle: const TextStyle( // สีและขนาด
                    color: Colors.black54,
                    fontWeight: FontWeight.bold,
                    fontSize: 20.0,
                  ),
                ),
                // กำหนดสี ขนาด ของชื่อวัน 7 วัน
                daysOfWeekStyle: const DaysOfWeekStyle(
                  weekdayStyle: TextStyle(
                    color: Colors.black26,
                    fontWeight: FontWeight.bold,
                    fontSize: 20.0,
                  ),
                ),
                // กำหนดรูปแบบสีต่างๆ และรูปแบบการแสดงค่าหลัก
                calendarStyle: const CalendarStyle(
                  // วันปัจจุบันสีข้อความ ขนาดข้อความ
                  todayTextStyle: TextStyle(
                    color: Color.fromARGB(255, 178, 16, 22),
                    fontSize: 20.0,
                  ),
                  // วันที่ปัจจุบันพื้นหลัง รูปแบบรูปร่างที่แสดง
                  todayDecoration: BoxDecoration(
                    color: Color.fromARGB(200, 255, 229, 212),
                    shape: BoxShape.circle, // แสดงแบบวงกลม
                  ),
                  // วันที่กดเลือก สีข้อความ ขนาดข้อความ
                  selectedTextStyle: TextStyle(
                    color: Color.fromARGB(200, 255, 229, 212),
                    fontSize: 20.0,
                  ),
                  // วันที่กดเลือก พื้นหลัง รูปแบบรูปร่างที่แสดง
                  selectedDecoration: BoxDecoration(
                    color: Color.fromARGB(255, 178, 16, 22),
                    shape: BoxShape.circle, // แสดงแบบวงกลม
                  ),                 
                  outsideDaysVisible: false, // แสดงวันที่ที่ไม่ใช่ของเดือนหรือไม่
                  outsideTextStyle: TextStyle(// รูปแบบวันที่ที่ไม่ใช่ของเดือน ถ้าแสดง
                    color: Colors.black26,
                    fontSize: 20.0,
                  ),
                  defaultTextStyle: TextStyle( // วันที่ปกติ
                    color: Colors.black87,
                    fontSize: 20.0,
                  ),
                  weekendTextStyle: TextStyle( // สีของวันหยุด ถ้ากำหนดรายการวันหยุด
                    color: Colors.red,
                    fontSize: 20.0,
                  ),
                ),
                locale: 'th_TH', // ใช้รูปแบบวันที่เป็นภาษาไทย
                // กำหนดวันเริ่มต้น และวันสุดท้ายในปฏิทิน  ที่สามารถเลื่อนได้
                // focusedDay ต้องอยู่ระหว่าง ช่วงของ firstDay และ lastDay
                // ไม่เช่นนั้นจะเกิด error ได้
                // DateTime.utc(2010, 10, 16) จะได้เป็น 2010-10-16 00:00:00.000Z
                // DateTime.utc(2010, 10, 16).toLocal() จะได้เป็น 2010-10-16 07:00:00.000
                firstDay: _firstDay,
                lastDay: _lastDay,
                focusedDay: _focusedDay, // วันที่จะเน้น ในที่นี้ใช้เป็นวันที่จากที่เลือกหรือกำหนด
                // กำหนดถ้าเป็นวันที่ตรงกับวันที่เลือก ให้ทำสัญลักษณ์ว่าถูกเลือกในวันนั้นๆ
                selectedDayPredicate: (day) => isSameDay(_selectedDay, day),
                calendarFormat: CalendarFormat.month, // แสดงปฏิทินแบบเดือน
                calendarBuilders: CalendarBuilders(
                  // ส่วนจัดรูปแบบของวันที่ต่างๆ ในปฏิทิน
                  defaultBuilder: (context, day, focusedDay) {
                        // กำหนดตัวแปรรับ วันที่เลือกในรูปแบบ 2024-10-10 เพื่อส่งค่าไปตรวจสอบว่า
                        // เป็นวันพระหรือไม่ ถ้าเป็น 1 2 3 และ 4 เป็นวันพระ ถ้าเป็น null ไม่ใช่วันพระ
                        String _checkDate = DateFormat('yyyy-MM-dd').format(day);             
                        return Container(
                          margin: const EdgeInsets.all(6.0),
                          alignment: Alignment.center,
                          child: Stack(
                            alignment: Alignment.center,
                            children: [
                              // ตรวจสอบว่าเป็นวันพระหรือไม่ ถ้าเป็นวันพระ แสดงจุดสีเหลืองตรงมุมซ้ายวันที่
                              if (_BuddhaDay[_checkDate] != null)
                              Positioned(
                                top: 0,
                                left: 0,
                                child: Container(
                                  width: 10,
                                  height: 10,
                                  decoration: BoxDecoration(
                                    shape: BoxShape.circle,
                                    color: const Color.fromARGB(255, 247, 211, 6),
                                  ),
                                ),
                              ), 
                              // เป็นวันอื่นๆ แสดงค่าปกติ                                   
                              Center(
                                child: Text(
                                  '${day.day}',
                                  style: TextStyle(
                                    fontSize: 20,
                                  ),
                                ),
                              ),                     
                            ],
                          ),
                        );
                  },
                  // ส่วนจัดรูปแบบชื่อวันแบบย่อในปฏิทิน ในที่นี้ ตรวจว่าเป็นวัน
                  // เสาร์ หรืออาทิตย์ แล้วให้แสดงสีข้อความเป็นสีแดง
                  dowBuilder: (context, day) {
                    if (day.weekday == DateTime.sunday ||
                        day.weekday == DateTime.saturday) {
                      // ใช้แพ็กเก็จ intl จัดรูปแบบเอาวันของข้อมูลวันที่ มาแสดง
                      final textDay = DateFormat.E('th').format(day);
                      return Center(
                        child: Text(
                          textDay,
                          style: const TextStyle(
                            color: Colors.red,
                            fontSize: 20.0,
                          ),
                        ),
                      );
                    }
                    return null; // ถ้าเป็นวันอื่นๆ ไม่ต้องจัดรูปแบบเพิ่มเติม
                  },
                ),
                // เมื่อเลื่อนเปลี่ยนหน้าข้อมูล เช่น เปลี่ยนเดือน
                onPageChanged: (day) {
                
                },
                // เมื่อกดเลือกวันในปฏิทิน
                onDaySelected: (selectedDay, focusedDay) {
                  // กำหนดตัวแปรรับ วันที่เลือกในรูปแบบ 2024-10-10 เพื่อส่งค่าไปตรวจสอบว่า
                  // เป็นวันพระหรือไม่ คืนค่าเป็น String ข้างขึ้นข้างแรม ของวันนั้นๆ เช่น แรม 15 ค่ำ เดือน 6 หรือ ค่าว่าง
                  String _selectDateInput = DateFormat('yyyy-MM-dd').format(selectedDay);
                  print(getWaxingCrescentInfo(_selectDateInput));
                  // ถ้าไม่ใช่วันที่เดียวกัน
                  if (!isSameDay(_selectedDay, selectedDay)) {
                    setState(() {
                      _selectedDay = selectedDay;
                      _focusedDay = focusedDay;
                    });
                  }
                },
                // ส่วนสำหรับกำหนด event กิจกรรมในปฏิทิน
                eventLoader: (day) {       
                  return [];
                },
              ),
            ],
          )
        ),
    );
  }
}
 
 
// ส่วนของฟังก์ชั่นเกียวกับวันพระ วันข้างขึ้นข้างแรม
 
// ฟังก์ชั่นหาปีอธิกมาธ กับปีอธิกวาร
String isAtikamasAtikawan(int year) {
  int inputYear = year - 78;
  int beginYear = 1700;
  int endYear = inputYear + 5;
  double beginDeviation = 0.098385;
  int i = beginYear;
  Map<int, double> arrYearModulus = {};
  Map<int, double> arrYearDeviation = {};
  Map<int, String> arrYearType = {};
 
  while (i < endYear) {
    int nextYear = i + 1;
    double valModulus = (i - 0.45222) % 2.7118886;
    double valModulusNextYear = (nextYear - 0.45222) % 2.7118886;
    arrYearModulus[i] = valModulus;
 
    double valDeviation;
    if (i == beginYear) {
      valDeviation = beginDeviation;
    } else {
      double checkValue1 = (arrYearType[i - 1] == "wan") ? -0.632944 : 0.367056;
      double checkValue2 = (arrYearModulus[i - 1]! < 1) ? -0.102356 : checkValue1;
      valDeviation = arrYearDeviation[i - 1]! + checkValue2;
    }
 
    arrYearDeviation[i] = valDeviation;
 
    if (valModulus < 1) {
      arrYearType[i] = "mas";
    } else {
      double checkValue3 = (valModulusNextYear < 1) ? 0.0169501433191599 : -0.0142223099315486;
      if (valDeviation > checkValue3) {
        arrYearType[i] = "wan";
      } else {
        arrYearType[i] = "";
      }
    }
    i++;
  }
  return arrYearType[inputYear].toString();
}
 
// ฟังก์ชั่นสำหรับตรวจสอบวันพระ โดยส่งวันที่แบบ String เช่น "2024-10-10" เข้าไป ประยุกต์จากภาษา PHP http://niik.in/1021
// getWaxingCrescentInfo('2024-10-10',getlistBuddshDay: true); คืนค่าเป็น List<Map> รายการวันพระท้งหมดของปีนั้นๆ
// getWaxingCrescentInfo('2024-10-10',buddhistDayType: true); คืนค่าเป็น String ชนิดวันพระ ของวันนั้นๆ 1 2 3 4 หรือ ค่าว่าง
// getWaxingCrescentInfo('2024-10-10'); คืนค่าเป็น String ข้างขึ้นข้างแรม ของวันนั้นๆ เช่น แรม 15 ค่ำ เดือน 6 หรือ ค่าว่าง
dynamic getWaxingCrescentInfo(String inputDay, {bool buddhistDayType = false,bool getlistBuddshDay = false}) {
  DateFormat formatter = DateFormat('yyyy-MM-dd');
  DateTime parsedDate;
 
  try {
    parsedDate = formatter.parse(inputDay);
  } catch (e) {
    return "รูปแบบวันที่ไม่ถูกต้อง";
  }
 
  Map<String, String> arrReturnDay = {};
  Map<String, String> arrReturnBuddhaDay = {};
  DateTime beginThaiDate = DateTime(1956, 12, 3);
  int checkYear = parsedDate.year;
 
  int dayAdd = 0;
  // print(checkYear);
  for (int i = 1957; i <= checkYear; i++) {
    bool isAtikamas = isAtikamasAtikawan(i) == 'mas' ? true : false;
    bool isAtikawan = isAtikamasAtikawan(i) == 'wan' ? true : false;
    int dayInYear = 354;
    dayInYear = isAtikamas ? 384 : dayInYear;
    dayInYear = isAtikawan ? 355 : dayInYear;
 
    DateTime beginBuddhistDayOfYear = beginThaiDate.add(Duration(days: dayAdd));
    dayAdd += dayInYear;
 
    // ignore: unused_local_variable
    int cDay = 0; // นับวัน
    int cuDay = 0;  // นับวันขึ้น
    int cdDay = 0; // นับวันแรม  
    String double_month_eight = ""; // เดือน 8 สองหนหรือไม่ 
 
 
    int currentMonth = 1; // เดือนเริ่มต้น
    bool doubleMonth = false; // เดือน 8 สองหนหรือไม่
 
    if (i == checkYear) {
      int buddhaMonthDay = 0;
      // print(dayInYear);
      for (int v = 0; v < dayInYear + 60; v++) {
        cDay++;
        int finalDDay = (currentMonth % 2 == 1) ? 14 : 15;
        finalDDay = isAtikawan && currentMonth == 7 ? 15 : finalDDay;
 
        if (cdDay == finalDDay) {
          currentMonth++;
          if (currentMonth == 13) currentMonth = 1;
          if (isAtikamas && currentMonth == 9 && !doubleMonth) {
            currentMonth--;
            doubleMonth = true;
            double_month_eight = "หลัง";
          }else{
            double_month_eight = "";
          }
          cuDay = 0;
          cdDay = 0;
        }
 
        if(cuDay < 15){
          cuDay++;
          if(cuDay==8 || cuDay==15){
            if(cuDay==15){
              buddhaMonthDay = 4; // ขึ้นเต็มดวง
            }else{
              buddhaMonthDay = 3; // ขึ้นไม่เต็มดวง
            }
          }else{
            buddhaMonthDay = 0;
          }
        }else{
          if(cdDay < finalDDay){
            cdDay++;
            if(cdDay==8 || cdDay==finalDDay){
              if(cdDay==finalDDay){
                buddhaMonthDay = 2; // ดับทั้งดวง
              }else{
                buddhaMonthDay = 1; // ดับครึ่งดวง
              }
            }else{
              buddhaMonthDay = 0;
            }
          }
        }
 
        String dataWaxingCrescent = cdDay > 0
            ? "แรม $cdDay ค่ำ เดือน $currentMonth $double_month_eight"
            : "ขึ้น $cuDay ค่ำ เดือน $currentMonth $double_month_eight";
        // print(dataWaxingCrescent);
        String currentDate = formatter.format(beginBuddhistDayOfYear.add(Duration(days: v)));
        // arrReturnDay[currentDate] = dataWaxingCrescent;
        // print('${currentDate} - ${dataWaxingCrescent}');
        if (buddhaMonthDay > 0 && buddhaMonthDay <= 4) {
          if(getlistBuddshDay){
              arrReturnDay[currentDate] = "$buddhaMonthDay";
          }else{
            if(buddhistDayType){
                arrReturnBuddhaDay[currentDate] = "$buddhaMonthDay";
            }else{
                arrReturnBuddhaDay[currentDate] = dataWaxingCrescent;
            }
          }
        }
      }
    }
  }
  if(getlistBuddshDay){
    return arrReturnDay;
  }else{
    if(arrReturnBuddhaDay[inputDay]!=null){
      return arrReturnBuddhaDay[inputDay].toString();
    }else{
      return '';
    }
  }
 
}
 
 

ผลลัพธ์ที่ได้

 



 
 
จะเป็นการแสดงวันพระในช่วงประมาณ 1 ปี รวบปีที่กำลังแสดงข้อมูลหรือปีปัจจุบัน หากจะให้รองรับ
ปีถัดไปด้วย ก็สามารถประยุกต์เพิ่มเติมได้ วันพระยังสามารถแยกเป็นวันพระเล็ก (ขึ้น 8 ค่ำ และ แรม
 8 ค่ำ) และวันพระใหญ๋ (ขึ้น 15 ค่ำ และ แรม 14 หรือ แรม 15 ค่ำ) 
 
สำหรับรายละเอียดการใช้งาน table_calendar ก็จะของจบประมาณเท่านี้ ยังมีส่วนของ event ที่
เราสามารถปรับเพิ่มได้ และเราสามารถประยุกต์หรือทำคำสั่งแสดงข้อมูลส่วนล่างของปฏิทิน เมื่อกดเลือก
วันที่ที่ต้องการได้ เช่นการไปประยุกต์กับปฏิทินกิจกรรม ที่อาจจะบันทึกลงฐานข้อมูล ก็ดึงมาแสดงได้
    หวังว่าเนื้อหานี้จะเป็นแนวทางนำไปปรับประยุกต์ใช้งานต่อไป


กด Like หรือ Share เป็นกำลังใจ ให้มีบทความใหม่ๆ เรื่อยๆ น่ะครับ



อ่านต่อที่บทความ



ทบทวนบทความที่แล้ว









เนื้อหาที่เกี่ยวข้อง






เนื้อหาพิเศษ เฉพาะสำหรับสมาชิก

กรุณาล็อกอิน เพื่ออ่านเนื้อหาบทความ

ยังไม่เป็นสมาชิก

สมาชิกล็อกอิน



( หรือ เข้าใช้งานผ่าน Social Login )




URL สำหรับอ้างอิง











เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ