GetDaysInWeek() mencantumkan hari-hari dari Minggu hingga Sabtu.

GetSevenInWeek() mencantumkan minggu pertama tahun 2019. 01 Januari seharusnya pada hari Selasa, bukan Minggu.

Bagaimana Anda mencocokkan tanggal dengan hari dalam seminggu menggunakan properti hari kerja di bawah kelas DateTime atau paket Date_Utils?

match date with day of week

class ThirdRouteState extends State<ThirdRoute> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Calendar Page"),
      ),
      body: Column(children: <Widget>[
        Table(
          children: <TableRow>[
            TableRow(
              children: getDaysInWeek(),
            ),
            TableRow(
              children: getSevenInWeek(),
            ),
          ],
        ),

      ]),
    );
  }
}

List<Widget> getSevenInWeek() {
  var seven = <DateTime>[];
  DateTime start = DateTime(2019, 01, 01);
  final end = DateTime(2019, 01, 08);
  while (start.isBefore(end)) {
    seven.add(start);
    start = start.add(const Duration(days: 1));
  }

  return seven.map((DateTime time) {
    return Text(
      new DateFormat("dd").format(time),
      style: TextStyle(backgroundColor: Colors.red),
      textAlign: TextAlign.left,
    );
  }).toList();
}

List<Widget> getDaysInWeek() {
  List<Widget> dayWidgets = [];

  Utils.weekdays.forEach(
    (day) {
      dayWidgets.add(
        new CalendarTile(
          isDayOfWeek: true,
          dayOfWeek: day,
        ),
      );
    },
  );

  return dayWidgets;
}

0
Kagimura 20 September 2019, 23:08

1 menjawab

Jawaban Terbaik

Alih-alih mengatur hari di dalam fungsi getSevenInWeek. Lakukan di kelas State seperti itu, lalu berikan ke getSevenInWeek dan getDaysInWeek:

// no need for a library.
String getDay(int dayNumber) {
  List<String> weekdays = const [
    "Mon",
    "Tue",
    "Wed",
    "Thu",
    "Fri",
    "Sat",
    "Sun",
  ];
  return weekdays[dayNumber];
}

class ThirdRouteState extends State<ThirdRoute> {
  List<DateTime> seven = <DateTime>[];
  @override
  void initState() {
    seven = <DateTime>[];
    DateTime start = DateTime(2019, 01, 01);
    DateTime end = DateTime(2019, 01, 08);
    while (start.isBefore(end)) {
      seven.add(start);
      start = start.add(const Duration(days: 1));
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Calendar Page"),
      ),
      body: Column(children: <Widget>[
        Table(
          children: <TableRow>[
            TableRow(
              children: getDaysInWeek(seven),
            ),
            TableRow(
              children: getSevenInWeek(seven),
            ),
          ],
        ),
      ]),
    );
  }
}

List<Widget> getSevenInWeek(List<DateTime> days) {
  return days.map((DateTime time) {
    return Text(
      new DateFormat("dd").format(time),
      style: TextStyle(backgroundColor: Colors.red),
      textAlign: TextAlign.left,
    );
  }).toList();
}

List<Widget> getDaysInWeek(List<DateTime> days) {
  List<Widget> dayWidgets = [];

  days.forEach(
    (DateTime day) {
      dayWidgets.add(
        new CalendarTile(
          isDayOfWeek: true,
          dayOfWeek: getDay(day.weekday),
        ),
      );
    },
  );

  return dayWidgets;
}

1
Sayegh 20 September 2019, 22:35