3
1

More than 1 year has passed since last update.

[flutter]SfCalendarの基本的な使い方まとめ

Posted at

はじめに

flutterでカレンダーを使用する際にはtale_calendarが紹介されがちですが、今回のプロジェクトでの要件を満たすためにSfCalendarを使用したので、基本的な使い方を紹介します。

SfCalendar導入

import
dependencies:
  flutter:
    sdk: flutter
  syncfusion_flutter_calendar: ^22.1.38 //追加

最新のバージョンは公式ドキュメントから確認してください。

ミニマムコード

@override
Widget build(BuildContext context) {
  return Scaffold(
      body: SfCalendar(
    view: CalendarView.month,
  ));
}

上記コードではviewのパラメータはそれぞれ、

  • CalendarView.month → カレンダー月表示スクリーンショット 2023-07-17 15.48.13.png
  • CalendarView.day → カレンダー曜日表示
    スクリーンショット 2023-07-17 15.48.50.png
  • CalendarView.week → カレンダー週表示スクリーンショット 2023-07-17 15.49.07.png

他にもありますが、他のviewの形式は実際に触って確かめてみてください。

その他主要パラメータ

todayHighlightColor

※今回は月表示を前提として進めます。

@override
Widget build(BuildContext context) {
  return Scaffold(
      body: SfCalendar(
    view: CalendarView.month
todayHighlightColor: Colors.orangeAccent,///new

  ));
}

todayHighlightColorで今日の日付をハイライトするcolorを選択します。

showNavigationArrow

@override
Widget build(BuildContext context) {
  return Scaffold(
      body: SfCalendar(
    view: CalendarView.month
todayHighlightColor: Colors.orangeAccent,
showNavigationArrow: false,///new

  ));
}

showNavigationArrowでは、次、前月にスクロールする際にナビゲーションを表示するかを制御するパラメータになります。

selectionDecoration

@override
Widget build(BuildContext context) {
  return Scaffold(
      body: SfCalendar(
    view: CalendarView.month
todayHighlightColor: Colors.orangeAccent,
showNavigationArrow: false,
selectionDecoration: BoxDecoration(
            color: Colors.transparent,
            border: Border.all(color: Colors.transparent,width: 0),),
///new

  ));
}

selectionDecorationでは、任意の日付をタップした際の色を決定します。
※上記コードでは選択時に色をつけない仕様にしています。

headerStyle

@override
Widget build(BuildContext context) {
  return Scaffold(
      body: SfCalendar(
    view: CalendarView.month
todayHighlightColor: Colors.orangeAccent,
showNavigationArrow: false,
selectionDecoration: BoxDecoration(
            color: Colors.transparent,
            border: Border.all(color: Colors.transparent,width: 0),),

headerStyle: const CalendarHeaderStyle( textAlign: TextAlign.center),///new

  ));
}

ヘッダーのテキストを修飾するロジックです。
※上記コードではヘッダーのテキストをセンターに配置しています。

appointmentDisplayCount

@override
Widget build(BuildContext context) {
  return Scaffold(
      body: SfCalendar(
    view: CalendarView.month
todayHighlightColor: Colors.orangeAccent,
showNavigationArrow: false,
selectionDecoration: BoxDecoration(
            color: Colors.transparent,
            border: Border.all(color: Colors.transparent,width: 0),),

headerStyle: const CalendarHeaderStyle( textAlign: TextAlign.center),

 appointmentDisplayCount:4///new

  ));
}

appointmentDisplayCountでは一つの曜日に表示できるイベント数の最大数を指定できます。

端末サイズによって三項演算子とうで場合分けして使用すればいい感じになりそうです。

onViewChanged&onTap

@override
Widget build(BuildContext context) {
  return Scaffold(
      body: SfCalendar(
    view: CalendarView.month
todayHighlightColor: Colors.orangeAccent,
showNavigationArrow: false,
selectionDecoration: BoxDecoration(
            color: Colors.transparent,
            border: Border.all(color: Colors.transparent,width: 0),),

headerStyle: const CalendarHeaderStyle( textAlign: TextAlign.center),

 appointmentDisplayCount:4

  onViewChanged: (ViewChangedDetails details) {
                           print(details.visibleDates)

                        },///new

 onTap: (CalendarTapDetails details) {
                          print(details.visibleDates)
                        },///new

  ));
}

onViewChangedでは前、次スクロールした際のロジックを記載できます。

onTapは文字通りタップした際のロジックを記載できます。

全コード


import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_calendar/calendar.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, });


 

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
 

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body: SfCalendar(
    view: CalendarView.month
    todayHighlightColor: Colors.orangeAccent,
    showNavigationArrow: false,
    selectionDecoration: BoxDecoration(
            color: Colors.transparent,
            border: Border.all(color: Colors.transparent,width: 0),),

   headerStyle: const CalendarHeaderStyle( textAlign: 
    TextAlign.center),

   appointmentDisplayCount:4

   onViewChanged: (ViewChangedDetails details) {
                             print(details.visibleDates)

                        },

   onTap: (CalendarTapDetails details) {
                          print(details.visibleDates)
                        },

  ));
}}
3
1
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
3
1