Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
cfe44e6
Added new wa to calculate column width to CupertinoDatePicker
Mairramer Jul 1, 2024
c185d66
Map longest words
Mairramer Jul 3, 2024
9d4a965
Merge branch 'master' into fix/CupertinoDatePicker-incorrect-column-w…
Mairramer Jul 3, 2024
5af63be
fix space
Mairramer Jul 3, 2024
774296f
improve logic
Mairramer Jul 3, 2024
aec74c8
improves
Mairramer Jul 3, 2024
b868b57
fix
Mairramer Jul 3, 2024
f0c1b2c
remove wrong logic
Mairramer Jul 3, 2024
e337a05
code improve
Mairramer Jul 3, 2024
711ea43
Merge branch 'master' into fix/CupertinoDatePicker-incorrect-column-w…
Mairramer Jul 3, 2024
e11a95a
move logic to text painter
Mairramer Jul 4, 2024
c71e76e
improve test
Mairramer Jul 4, 2024
bc0bbe0
added test comment
Mairramer Jul 4, 2024
731fc2c
remove unused import
Mairramer Jul 4, 2024
cf9a1e6
Merge branch 'master' into fix/CupertinoDatePicker-incorrect-column-w…
Mairramer Jul 4, 2024
0e22048
fix tests
Mairramer Jul 4, 2024
aca229b
fix
Mairramer Jul 4, 2024
e149b8c
move logic again
Mairramer Jul 6, 2024
b7051ea
fix
Mairramer Jul 6, 2024
4b8e965
code improve
Mairramer Jul 6, 2024
dd73233
code improve
Mairramer Jul 27, 2024
0f9902d
Merge branch 'master' into fix/CupertinoDatePicker-incorrect-column-w…
Mairramer Jul 27, 2024
2aac5f2
fix
Mairramer Jul 27, 2024
ed31056
fix
Mairramer Jul 27, 2024
31db0ad
fix web tests
Mairramer Jul 27, 2024
9051295
fix web tests
Mairramer Jul 27, 2024
972c374
fix
Mairramer Jul 27, 2024
e1033cd
fix test
Mairramer Jul 27, 2024
c8426c1
improve
Mairramer Aug 8, 2024
2e5104f
Merge branch 'master' into fix/CupertinoDatePicker-incorrect-column-w…
Mairramer Aug 8, 2024
b077c8f
fix
Mairramer Aug 8, 2024
2143472
improves
Mairramer Aug 9, 2024
016f54c
some improves
Mairramer Aug 29, 2024
bada5d5
improve comment
Mairramer Aug 29, 2024
46e7a09
Merge branch 'master' into fix/CupertinoDatePicker-incorrect-column-w…
Mairramer Aug 29, 2024
be7ea1a
fix comment
Mairramer Aug 29, 2024
247645b
fix
Mairramer Sep 2, 2024
cbc5739
Merge branch 'master' into fix/CupertinoDatePicker-incorrect-column-w…
Mairramer Sep 4, 2024
0840e5e
fix
Mairramer Sep 10, 2024
d4e9f86
fix nit
Mairramer Sep 10, 2024
931a654
fix test
Mairramer Sep 11, 2024
f465fea
Merge branch 'master' into fix/CupertinoDatePicker-incorrect-column-w…
Mairramer Sep 11, 2024
b3dc937
fix
Mairramer Sep 11, 2024
da1e4b5
improve test
Mairramer Sep 11, 2024
831c8fb
improve tests
Mairramer Sep 13, 2024
f04547d
improve
Mairramer Sep 13, 2024
564af27
fix
Mairramer Sep 13, 2024
bffbdca
improve test
Mairramer Sep 15, 2024
a94fc32
fix
Mairramer Sep 15, 2024
e6cc9e3
Merge branch 'master' into fix/CupertinoDatePicker-incorrect-column-w…
Mairramer Sep 19, 2024
522be8f
remove skip test
Mairramer Sep 27, 2024
ea1d20d
Merge branch 'master' into fix/CupertinoDatePicker-incorrect-column-w…
Mairramer Sep 27, 2024
abb057e
turn back skip test
Mairramer Sep 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 35 additions & 39 deletions packages/flutter/lib/src/cupertino/date_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -487,78 +487,74 @@ class CupertinoDatePicker extends StatefulWidget {
bool showDayOfWeek, {
bool standaloneMonth = false,
}) {
String longestText = '';
final List<String> longTexts = <String>[];

switch (columnType) {
case _PickerColumnType.date:
// Measuring the length of all possible date is impossible, so here
// just some dates are measured.
for (int i = 1; i <= 12; i++) {
// An arbitrary date.
final String date =
localizations.datePickerMediumDate(DateTime(2018, i, 25));
if (longestText.length < date.length) {
longestText = date;
}
final String date = localizations.datePickerMediumDate(DateTime(2018, i, 25));
longTexts.add(date);
}
case _PickerColumnType.hour:
for (int i = 0; i < 24; i++) {
final String hour = localizations.datePickerHour(i);
if (longestText.length < hour.length) {
longestText = hour;
}
longTexts.add(hour);
}
case _PickerColumnType.minute:
for (int i = 0; i < 60; i++) {
final String minute = localizations.datePickerMinute(i);
if (longestText.length < minute.length) {
longestText = minute;
}
longTexts.add(minute);
}
case _PickerColumnType.dayPeriod:
longestText =
localizations.anteMeridiemAbbreviation.length > localizations.postMeridiemAbbreviation.length
? localizations.anteMeridiemAbbreviation
: localizations.postMeridiemAbbreviation;
longTexts.add(localizations.anteMeridiemAbbreviation);
longTexts.add(localizations.postMeridiemAbbreviation);
case _PickerColumnType.dayOfMonth:
int longestDayOfMonth = 1;
for (int i = 1; i <=31; i++) {
for (int i = 1; i <= 31; i++) {
final String dayOfMonth = localizations.datePickerDayOfMonth(i);
if (longestText.length < dayOfMonth.length) {
longestText = dayOfMonth;
longestDayOfMonth = i;
}
longTexts.add(dayOfMonth);
longestDayOfMonth = i;
}
if (showDayOfWeek) {
for (int wd = 1; wd < DateTime.daysPerWeek; wd++) {
final String dayOfMonth = localizations.datePickerDayOfMonth(longestDayOfMonth, wd);
if (longestText.length < dayOfMonth.length) {
longestText = dayOfMonth;
}
longTexts.add(dayOfMonth);
}
}
case _PickerColumnType.month:
for (int i = 1; i <= 12; i++) {
final String month = standaloneMonth
? localizations.datePickerStandaloneMonth(i)
: localizations.datePickerMonth(i);
if (longestText.length < month.length) {
longestText = month;
}
longTexts.add(month);
}
case _PickerColumnType.year:
longestText = localizations.datePickerYear(2018);
longTexts.add(localizations.datePickerYear(2018));
}

assert(longestText != '', 'column type is not appropriate');
assert(longTexts.isNotEmpty && longTexts.every((String text) => text.isNotEmpty), 'column type is not appropriate');

return TextPainter.computeMaxIntrinsicWidth(
text: TextSpan(
style: _themeTextStyle(context),
text: longestText,
),
textDirection: Directionality.of(context),
);
return getColumnWidth(texts: longTexts, context: context);
}

/// Returns the width of column in the picker.
///
/// This method is intended for testing only. It calculates the width of the
/// widest column in the picker based on the provided list of texts and the
/// given [BuildContext].
@visibleForTesting
static double getColumnWidth({
required List<String> texts,
required BuildContext context,
TextStyle? textStyle,
}) {
return texts.map((String text) => TextPainter.computeMaxIntrinsicWidth(
text: TextSpan(
style: textStyle ?? _themeTextStyle(context),
text: text,
),
textDirection: Directionality.of(context),
)).reduce(math.max);
}
}

Expand Down
55 changes: 55 additions & 0 deletions packages/flutter/test/cupertino/date_picker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@Tags(<String>['reduced-test-set'])
library;

import 'dart:math' as math;
import 'dart:ui';

import 'package:flutter/cupertino.dart';
Expand Down Expand Up @@ -2414,6 +2415,50 @@ void main() {

expect(find.byType(CupertinoPickerDefaultSelectionOverlay), findsExactly(4));
});

testWidgets('CupertinoDatePicker accommodates widest text using table codepoints', (WidgetTester tester) async {
// |---------|
// | 0x2002 | // EN SPACE - 1/2 Advance
// | 0x2005 | // FOUR-PER-EM SPACE - 1/4 Advance
// |---------|
final List<String> testWords = <String>[
'\u2002' * 10, // Output: 10 * 1/2 = 5
'\u2005' * 20, // Output: 20 * 1/4 = 5
];

await tester.pumpWidget(
CupertinoApp(
home: Center(
child: CupertinoDatePicker(
onDateTimeChanged: (DateTime date) {},
initialDateTime: DateTime(2018, 9, 15),
),
),
),
);

final BuildContext context = tester.element(find.byType(CupertinoDatePicker));

const TextStyle textStyle = TextStyle(
fontSize: 21,
letterSpacing: 0.4,
fontWeight: FontWeight.normal,
color: CupertinoColors.label,
);

final List<double> widths = testWords.map((String word) => getColumnWidth(word, textStyle, context)).toList();

final double largestWidth = widths.reduce(math.max);

final double testWidth = CupertinoDatePicker.getColumnWidth(
texts: testWords,
context: context,
textStyle: textStyle,
);

expect(testWidth, equals(largestWidth));
expect(widths.indexOf(largestWidth), equals(1));
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/39998
}

Widget _buildPicker({
Expand All @@ -2438,3 +2483,13 @@ Widget _buildPicker({
),
);
}

double getColumnWidth(String text, TextStyle textStyle, BuildContext context) {
return TextPainter.computeMaxIntrinsicWidth(
text: TextSpan(
text: text,
style: textStyle,
),
textDirection: Directionality.of(context),
);
}