Unverified Commit 6753a852 authored by st merlhin's avatar st merlhin Committed by GitHub

expose `CupertinoDatePicker` and `CupertinoTimerPicker` `itemExtent` … (#125852)

…property

This PR expose `CupertinoDatePicker` and `CupertinoTimerPicker` `itemExtent` property to allow setting custom one depending on `tMediaQuery.of(context).textScaleFactor`.
Fixes: #125127
parent 856fcc30
...@@ -290,8 +290,13 @@ class CupertinoDatePicker extends StatefulWidget { ...@@ -290,8 +290,13 @@ class CupertinoDatePicker extends StatefulWidget {
this.use24hFormat = false, this.use24hFormat = false,
this.dateOrder, this.dateOrder,
this.backgroundColor, this.backgroundColor,
this.showDayOfWeek = false this.showDayOfWeek = false,
this.itemExtent = _kItemExtent,
}) : initialDateTime = initialDateTime ?? DateTime.now(), }) : initialDateTime = initialDateTime ?? DateTime.now(),
assert(
itemExtent > 0,
'item extent should be greater than 0',
),
assert( assert(
minuteInterval > 0 && 60 % minuteInterval == 0, minuteInterval > 0 && 60 % minuteInterval == 0,
'minute interval is not a positive integer factor of 60', 'minute interval is not a positive integer factor of 60',
...@@ -406,6 +411,11 @@ class CupertinoDatePicker extends StatefulWidget { ...@@ -406,6 +411,11 @@ class CupertinoDatePicker extends StatefulWidget {
/// Whether to to show day of week alongside day. Defaults to false. /// Whether to to show day of week alongside day. Defaults to false.
final bool showDayOfWeek; final bool showDayOfWeek;
/// {@macro flutter.cupertino.picker.itemExtent}
///
/// Defaults to a value that matches the default iOS date picker wheel.
final double itemExtent;
@override @override
State<StatefulWidget> createState() { // ignore: no_logic_in_create_state, https://github.com/flutter/flutter/issues/70499 State<StatefulWidget> createState() { // ignore: no_logic_in_create_state, https://github.com/flutter/flutter/issues/70499
// The `time` mode and `dateAndTime` mode of the picker share the time // The `time` mode and `dateAndTime` mode of the picker share the time
...@@ -725,7 +735,7 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> { ...@@ -725,7 +735,7 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> {
child: CupertinoPicker.builder( child: CupertinoPicker.builder(
scrollController: dateController, scrollController: dateController,
offAxisFraction: offAxisFraction, offAxisFraction: offAxisFraction,
itemExtent: _kItemExtent, itemExtent: widget.itemExtent,
useMagnifier: _kUseMagnifier, useMagnifier: _kUseMagnifier,
magnification: _kMagnification, magnification: _kMagnification,
backgroundColor: widget.backgroundColor, backgroundColor: widget.backgroundColor,
...@@ -803,7 +813,7 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> { ...@@ -803,7 +813,7 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> {
child: CupertinoPicker( child: CupertinoPicker(
scrollController: hourController, scrollController: hourController,
offAxisFraction: offAxisFraction, offAxisFraction: offAxisFraction,
itemExtent: _kItemExtent, itemExtent: widget.itemExtent,
useMagnifier: _kUseMagnifier, useMagnifier: _kUseMagnifier,
magnification: _kMagnification, magnification: _kMagnification,
backgroundColor: widget.backgroundColor, backgroundColor: widget.backgroundColor,
...@@ -868,7 +878,7 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> { ...@@ -868,7 +878,7 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> {
child: CupertinoPicker( child: CupertinoPicker(
scrollController: minuteController, scrollController: minuteController,
offAxisFraction: offAxisFraction, offAxisFraction: offAxisFraction,
itemExtent: _kItemExtent, itemExtent: widget.itemExtent,
useMagnifier: _kUseMagnifier, useMagnifier: _kUseMagnifier,
magnification: _kMagnification, magnification: _kMagnification,
backgroundColor: widget.backgroundColor, backgroundColor: widget.backgroundColor,
...@@ -918,7 +928,7 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> { ...@@ -918,7 +928,7 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> {
child: CupertinoPicker( child: CupertinoPicker(
scrollController: meridiemController, scrollController: meridiemController,
offAxisFraction: offAxisFraction, offAxisFraction: offAxisFraction,
itemExtent: _kItemExtent, itemExtent: widget.itemExtent,
useMagnifier: _kUseMagnifier, useMagnifier: _kUseMagnifier,
magnification: _kMagnification, magnification: _kMagnification,
backgroundColor: widget.backgroundColor, backgroundColor: widget.backgroundColor,
...@@ -1211,7 +1221,7 @@ class _CupertinoDatePickerDateState extends State<CupertinoDatePicker> { ...@@ -1211,7 +1221,7 @@ class _CupertinoDatePickerDateState extends State<CupertinoDatePicker> {
child: CupertinoPicker( child: CupertinoPicker(
scrollController: dayController, scrollController: dayController,
offAxisFraction: offAxisFraction, offAxisFraction: offAxisFraction,
itemExtent: _kItemExtent, itemExtent: widget.itemExtent,
useMagnifier: _kUseMagnifier, useMagnifier: _kUseMagnifier,
magnification: _kMagnification, magnification: _kMagnification,
backgroundColor: widget.backgroundColor, backgroundColor: widget.backgroundColor,
...@@ -1254,7 +1264,7 @@ class _CupertinoDatePickerDateState extends State<CupertinoDatePicker> { ...@@ -1254,7 +1264,7 @@ class _CupertinoDatePickerDateState extends State<CupertinoDatePicker> {
child: CupertinoPicker( child: CupertinoPicker(
scrollController: monthController, scrollController: monthController,
offAxisFraction: offAxisFraction, offAxisFraction: offAxisFraction,
itemExtent: _kItemExtent, itemExtent: widget.itemExtent,
useMagnifier: _kUseMagnifier, useMagnifier: _kUseMagnifier,
magnification: _kMagnification, magnification: _kMagnification,
backgroundColor: widget.backgroundColor, backgroundColor: widget.backgroundColor,
...@@ -1298,7 +1308,7 @@ class _CupertinoDatePickerDateState extends State<CupertinoDatePicker> { ...@@ -1298,7 +1308,7 @@ class _CupertinoDatePickerDateState extends State<CupertinoDatePicker> {
}, },
child: CupertinoPicker.builder( child: CupertinoPicker.builder(
scrollController: yearController, scrollController: yearController,
itemExtent: _kItemExtent, itemExtent: widget.itemExtent,
offAxisFraction: offAxisFraction, offAxisFraction: offAxisFraction,
useMagnifier: _kUseMagnifier, useMagnifier: _kUseMagnifier,
magnification: _kMagnification, magnification: _kMagnification,
...@@ -1895,13 +1905,18 @@ class CupertinoTimerPicker extends StatefulWidget { ...@@ -1895,13 +1905,18 @@ class CupertinoTimerPicker extends StatefulWidget {
this.secondInterval = 1, this.secondInterval = 1,
this.alignment = Alignment.center, this.alignment = Alignment.center,
this.backgroundColor, this.backgroundColor,
this.itemExtent = _kItemExtent,
required this.onTimerDurationChanged, required this.onTimerDurationChanged,
}) : assert(initialTimerDuration >= Duration.zero), }) : assert(initialTimerDuration >= Duration.zero),
assert(initialTimerDuration < const Duration(days: 1)), assert(initialTimerDuration < const Duration(days: 1)),
assert(minuteInterval > 0 && 60 % minuteInterval == 0), assert(minuteInterval > 0 && 60 % minuteInterval == 0),
assert(secondInterval > 0 && 60 % secondInterval == 0), assert(secondInterval > 0 && 60 % secondInterval == 0),
assert(initialTimerDuration.inMinutes % minuteInterval == 0), assert(initialTimerDuration.inMinutes % minuteInterval == 0),
assert(initialTimerDuration.inSeconds % secondInterval == 0); assert(initialTimerDuration.inSeconds % secondInterval == 0),
assert(
itemExtent > 0,
'item extent should be greater than 0'
);
/// The mode of the timer picker. /// The mode of the timer picker.
final CupertinoTimerPickerMode mode; final CupertinoTimerPickerMode mode;
...@@ -1930,6 +1945,11 @@ class CupertinoTimerPicker extends StatefulWidget { ...@@ -1930,6 +1945,11 @@ class CupertinoTimerPicker extends StatefulWidget {
/// Defaults to null, which disables background painting entirely. /// Defaults to null, which disables background painting entirely.
final Color? backgroundColor; final Color? backgroundColor;
/// {@macro flutter.cupertino.picker.itemExtent}
///
/// Defaults to a value that matches the default iOS timer picker wheel.
final double itemExtent;
@override @override
State<StatefulWidget> createState() => _CupertinoTimerPickerState(); State<StatefulWidget> createState() => _CupertinoTimerPickerState();
} }
...@@ -2148,7 +2168,7 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> { ...@@ -2148,7 +2168,7 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
scrollController: FixedExtentScrollController(initialItem: selectedHour!), scrollController: FixedExtentScrollController(initialItem: selectedHour!),
magnification: _kMagnification, magnification: _kMagnification,
offAxisFraction: _calculateOffAxisFraction(additionalPadding.start, 0), offAxisFraction: _calculateOffAxisFraction(additionalPadding.start, 0),
itemExtent: _kItemExtent, itemExtent: widget.itemExtent,
backgroundColor: widget.backgroundColor, backgroundColor: widget.backgroundColor,
squeeze: _kSqueeze, squeeze: _kSqueeze,
onSelectedItemChanged: (int index) { onSelectedItemChanged: (int index) {
...@@ -2212,7 +2232,7 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> { ...@@ -2212,7 +2232,7 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
additionalPadding.start, additionalPadding.start,
widget.mode == CupertinoTimerPickerMode.ms ? 0 : 1, widget.mode == CupertinoTimerPickerMode.ms ? 0 : 1,
), ),
itemExtent: _kItemExtent, itemExtent: widget.itemExtent,
backgroundColor: widget.backgroundColor, backgroundColor: widget.backgroundColor,
squeeze: _kSqueeze, squeeze: _kSqueeze,
looping: true, looping: true,
...@@ -2278,7 +2298,7 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> { ...@@ -2278,7 +2298,7 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
additionalPadding.start, additionalPadding.start,
widget.mode == CupertinoTimerPickerMode.ms ? 1 : 2, widget.mode == CupertinoTimerPickerMode.ms ? 1 : 2,
), ),
itemExtent: _kItemExtent, itemExtent: widget.itemExtent,
backgroundColor: widget.backgroundColor, backgroundColor: widget.backgroundColor,
squeeze: _kSqueeze, squeeze: _kSqueeze,
looping: true, looping: true,
......
...@@ -162,10 +162,12 @@ class CupertinoPicker extends StatefulWidget { ...@@ -162,10 +162,12 @@ class CupertinoPicker extends StatefulWidget {
/// If null, an implicit one will be created internally. /// If null, an implicit one will be created internally.
final FixedExtentScrollController? scrollController; final FixedExtentScrollController? scrollController;
/// {@template flutter.cupertino.picker.itemExtent}
/// The uniform height of all children. /// The uniform height of all children.
/// ///
/// All children will be given the [BoxConstraints] to match this exact /// All children will be given the [BoxConstraints] to match this exact
/// height. Must not be null and must be positive. /// height. Must be a positive value.
/// {@endtemplate}
final double itemExtent; final double itemExtent;
/// {@macro flutter.rendering.RenderListWheelViewport.squeeze} /// {@macro flutter.rendering.RenderListWheelViewport.squeeze}
......
...@@ -148,6 +148,20 @@ void main() { ...@@ -148,6 +148,20 @@ void main() {
expect(pickers.any((CupertinoPicker picker) => picker.backgroundColor != CupertinoColors.black), false); expect(pickers.any((CupertinoPicker picker) => picker.backgroundColor != CupertinoColors.black), false);
}); });
testWidgets('specified item extent value is applied', (WidgetTester tester) async {
await tester.pumpWidget(
CupertinoApp(
home: CupertinoTimerPicker(
itemExtent: 42,
onTimerDurationChanged: (_) { },
),
),
);
final Iterable<CupertinoPicker> pickers = tester.allWidgets.whereType<CupertinoPicker>();
expect(pickers.any((CupertinoPicker picker) => picker.itemExtent != 42), false);
});
testWidgets('columns are ordered correctly when text direction is ltr', (WidgetTester tester) async { testWidgets('columns are ordered correctly when text direction is ltr', (WidgetTester tester) async {
await tester.pumpWidget( await tester.pumpWidget(
CupertinoApp( CupertinoApp(
...@@ -320,6 +334,20 @@ void main() { ...@@ -320,6 +334,20 @@ void main() {
expect(pickers.any((CupertinoPicker picker) => picker.backgroundColor != CupertinoColors.black), false); expect(pickers.any((CupertinoPicker picker) => picker.backgroundColor != CupertinoColors.black), false);
}); });
testWidgets('specified item extent value is applied', (WidgetTester tester) async {
await tester.pumpWidget(
CupertinoApp(
home: CupertinoDatePicker(
itemExtent: 55,
onDateTimeChanged: (_) { },
),
),
);
final Iterable<CupertinoPicker> pickers = tester.allWidgets.whereType<CupertinoPicker>();
expect(pickers.any((CupertinoPicker picker) => picker.itemExtent != 55), false);
});
testWidgets('initial date honors minuteInterval', (WidgetTester tester) async { testWidgets('initial date honors minuteInterval', (WidgetTester tester) async {
late DateTime newDateTime; late DateTime newDateTime;
await tester.pumpWidget( await tester.pumpWidget(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment