Added calendar delegate to support custom calendar systems (#161874)

Added `CalendarDelegate` class that supports plugging in custom calendar
logics other than Gregorian Calendar System.

Here is an example implementation for Nepali(Bikram Sambat) Calendar
System:
https://github.com/sarbagyastha/nepali_date_picker/blob/m3/lib/src/nepali_calendar_delegate.dart

Demo using the `NepaliDatePickerDelegate`:
https://date.sarbagyastha.com.np/

Fixes https://github.com/flutter/flutter/issues/77531,
https://github.com/flutter/flutter/issues/161873

## Pre-launch Checklist

- [X] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

---------

Co-authored-by: Tong Mu <dkwingsmt@users.noreply.github.com>
This commit is contained in:
Sarbagya Dhaubanjar
2025-03-08 08:26:17 +05:45
committed by GitHub
parent 83781ae65c
commit 6d6d7914f9
10 changed files with 1095 additions and 108 deletions

View File

@@ -0,0 +1,45 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter_api_samples/material/date_picker/custom_calendar_date_picker.0.dart'
as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
Text getLastDayText(WidgetTester tester) {
final Finder dayFinder = find.descendant(of: find.byType(Ink), matching: find.byType(Text));
return tester.widget(dayFinder.last);
}
testWidgets('Days are based on the calendar delegate', (WidgetTester tester) async {
await tester.pumpWidget(const example.CalendarDatePickerApp());
final Finder nextMonthButton = find.byIcon(Icons.chevron_right);
Text lastDayText = getLastDayText(tester);
expect(find.text('February 2025'), findsOneWidget);
expect(lastDayText.data, equals('21'));
await tester.tap(nextMonthButton);
await tester.pumpAndSettle();
lastDayText = getLastDayText(tester);
expect(find.text('March 2025'), findsOneWidget);
expect(lastDayText.data, equals('28'));
await tester.tap(nextMonthButton);
await tester.pumpAndSettle();
lastDayText = getLastDayText(tester);
expect(find.text('April 2025'), findsOneWidget);
expect(lastDayText.data, equals('21'));
await tester.tap(nextMonthButton);
await tester.pumpAndSettle();
lastDayText = getLastDayText(tester);
expect(lastDayText.data, equals('28'));
});
}