0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

FlutterでMVVMを採用して、カレンダーアプリを作成してみた件 その1

Posted at

カレンダーで表示する日付を取得するための定数を実装していく
ここがカレンダーの実装における肝になるかと思います

calendar_constraint.dart
/// 有効表示内の最初の日付
final DateTime calendarStartDay = DateTime(
  DateTimeExtension.today.year - 1,
  DateTimeExtension.today.month,
  DateTimeExtension.today.day,
);

/// 有効表示内の最後の日付
final DateTime calendarEndDay = DateTime(
  DateTimeExtension.today.year + 1,
  DateTimeExtension.today.month,
  DateTimeExtension.today.day,
);

カレンダーで表示する範囲を定めてあげないと無限に生成する必要があるので宣言してあげる
今回は今日の日付±1年としました

次に曜日を列挙型で宣言します
カレンダーによっては日曜日や月曜日を始めにすることもあると思うのでそこも宣言しておきます

calendar_constraint.dart
/// 始まりの曜日
const startWeekType = WeekType.sunday;

/// 曜日
enum WeekType {
  /// 月曜日
  monday,

  /// 火曜日
  tuesday,

  /// 水曜日
  wednesday,

  /// 木曜日
  thursday,

  /// 金曜日
  friday,

  /// 土曜日
  saturday,

  /// 日曜日
  sunday,
}

startWeekTypeを変更すると始まりの曜日が変わります

calendar_constraint.dart
/// 始まりの曜日
const startWeekType = WeekType.monday;

このような実装をするためにstartWeekTypeを最初に持ってきて配列で返す処理を実装しています

calendar_constraint.dart
extension WeekTypeExtension on WeekType {
  /// 曜日の配列を取得
  static List<WeekType> get weekTypeList {
    // 曜日の配列を作成
    final list = [...WeekType.values];
    // 曜日の始まりが配列がどこにあるか検索
    final start = list.indexOf(startWeekType);
    // 曜日の始まりから日曜日までを取得
    final addList = list.sublist(start);
    // 曜日の始まりから日曜日までを先頭に持ってくる
    list
      ..removeRange(start, list.length)
      ..insertAll(0, addList);

    return list;
  }

※ちなみに、[...WeekType.values]で宣言しないとremoveとかaddができないので注意です

リンク

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?