From 19cdb21cb67acf51fb9b4e4e7a367d0bcd82fc7b Mon Sep 17 00:00:00 2001 From: Venkataramana Neelapala Date: Tue, 27 Aug 2019 03:17:49 +0530 Subject: [PATCH] Added backgroundColor property to CupertinoTimePicker and CupertinoDatePicker. #34741 (#39056) --- .../lib/src/cupertino/date_picker.dart | 36 ++++++--- .../test/cupertino/date_picker_test.dart | 78 +++++++++++++++++++ 2 files changed, 103 insertions(+), 11 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/date_picker.dart b/packages/flutter/lib/src/cupertino/date_picker.dart index 49c709db9f9..e79bc81a451 100644 --- a/packages/flutter/lib/src/cupertino/date_picker.dart +++ b/packages/flutter/lib/src/cupertino/date_picker.dart @@ -210,10 +210,12 @@ class CupertinoDatePicker extends StatefulWidget { this.maximumYear, this.minuteInterval = 1, this.use24hFormat = false, + this.backgroundColor = _kBackgroundColor }) : initialDateTime = initialDateTime ?? DateTime.now(), assert(mode != null), assert(onDateTimeChanged != null), assert(minimumYear != null), + assert(backgroundColor != null), assert( minuteInterval > 0 && 60 % minuteInterval == 0, 'minute interval is not a positive integer factor of 60', @@ -281,6 +283,11 @@ class CupertinoDatePicker extends StatefulWidget { /// null. final ValueChanged onDateTimeChanged; + /// Background color of date picker. + /// + /// Defaults to [CupertinoColors.white] when null. + final Color backgroundColor; + @override State createState() { // The `time` mode and `dateAndTime` mode of the picker share the time @@ -500,7 +507,7 @@ class _CupertinoDatePickerDateTimeState extends State { itemExtent: _kItemExtent, useMagnifier: _kUseMagnifier, magnification: _kMagnification, - backgroundColor: _kBackgroundColor, + backgroundColor: widget.backgroundColor, squeeze: _kSqueeze, onSelectedItemChanged: (int index) { selectedDayFromInitial = index; @@ -545,7 +552,7 @@ class _CupertinoDatePickerDateTimeState extends State { itemExtent: _kItemExtent, useMagnifier: _kUseMagnifier, magnification: _kMagnification, - backgroundColor: _kBackgroundColor, + backgroundColor: widget.backgroundColor, squeeze: _kSqueeze, onSelectedItemChanged: (int index) { if (widget.use24hFormat) { @@ -599,7 +606,7 @@ class _CupertinoDatePickerDateTimeState extends State { itemExtent: _kItemExtent, useMagnifier: _kUseMagnifier, magnification: _kMagnification, - backgroundColor: _kBackgroundColor, + backgroundColor: widget.backgroundColor, squeeze: _kSqueeze, onSelectedItemChanged: (int index) { selectedMinute = index * widget.minuteInterval; @@ -627,7 +634,7 @@ class _CupertinoDatePickerDateTimeState extends State { itemExtent: _kItemExtent, useMagnifier: _kUseMagnifier, magnification: _kMagnification, - backgroundColor: _kBackgroundColor, + backgroundColor: widget.backgroundColor, squeeze: _kSqueeze, onSelectedItemChanged: (int index) { selectedAmPm = index; @@ -792,7 +799,7 @@ class _CupertinoDatePickerDateState extends State { itemExtent: _kItemExtent, useMagnifier: _kUseMagnifier, magnification: _kMagnification, - backgroundColor: _kBackgroundColor, + backgroundColor: widget.backgroundColor, squeeze: _kSqueeze, onSelectedItemChanged: (int index) { selectedDay = index + 1; @@ -823,7 +830,7 @@ class _CupertinoDatePickerDateState extends State { itemExtent: _kItemExtent, useMagnifier: _kUseMagnifier, magnification: _kMagnification, - backgroundColor: _kBackgroundColor, + backgroundColor: widget.backgroundColor, squeeze: _kSqueeze, onSelectedItemChanged: (int index) { selectedMonth = index + 1; @@ -850,7 +857,7 @@ class _CupertinoDatePickerDateState extends State { offAxisFraction: offAxisFraction, useMagnifier: _kUseMagnifier, magnification: _kMagnification, - backgroundColor: _kBackgroundColor, + backgroundColor: widget.backgroundColor, onSelectedItemChanged: (int index) { selectedYear = index; if (DateTime(selectedYear, selectedMonth, selectedDay).day == selectedDay) @@ -1051,6 +1058,7 @@ class CupertinoTimerPicker extends StatefulWidget { this.initialTimerDuration = Duration.zero, this.minuteInterval = 1, this.secondInterval = 1, + this.backgroundColor = _kBackgroundColor, @required this.onTimerDurationChanged, }) : assert(mode != null), assert(onTimerDurationChanged != null), @@ -1059,7 +1067,8 @@ class CupertinoTimerPicker extends StatefulWidget { assert(minuteInterval > 0 && 60 % minuteInterval == 0), assert(secondInterval > 0 && 60 % secondInterval == 0), assert(initialTimerDuration.inMinutes % minuteInterval == 0), - assert(initialTimerDuration.inSeconds % secondInterval == 0); + assert(initialTimerDuration.inSeconds % secondInterval == 0), + assert(backgroundColor != null); /// The mode of the timer picker. final CupertinoTimerPickerMode mode; @@ -1078,6 +1087,11 @@ class CupertinoTimerPicker extends StatefulWidget { /// Callback called when the timer duration changes. final ValueChanged onTimerDurationChanged; + /// Background color of timer picker. + /// + /// Defaults to [CupertinoColors.white] when null. + final Color backgroundColor; + @override State createState() => _CupertinoTimerPickerState(); } @@ -1134,7 +1148,7 @@ class _CupertinoTimerPickerState extends State { scrollController: FixedExtentScrollController(initialItem: selectedHour), offAxisFraction: -0.5 * textDirectionFactor, itemExtent: _kItemExtent, - backgroundColor: _kBackgroundColor, + backgroundColor: widget.backgroundColor, squeeze: _kSqueeze, onSelectedItemChanged: (int index) { setState(() { @@ -1213,7 +1227,7 @@ class _CupertinoTimerPickerState extends State { ), offAxisFraction: offAxisFraction, itemExtent: _kItemExtent, - backgroundColor: _kBackgroundColor, + backgroundColor: widget.backgroundColor, squeeze: _kSqueeze, onSelectedItemChanged: (int index) { setState(() { @@ -1326,7 +1340,7 @@ class _CupertinoTimerPickerState extends State { ), offAxisFraction: offAxisFraction, itemExtent: _kItemExtent, - backgroundColor: _kBackgroundColor, + backgroundColor: widget.backgroundColor, squeeze: _kSqueeze, onSelectedItemChanged: (int index) { setState(() { diff --git a/packages/flutter/test/cupertino/date_picker_test.dart b/packages/flutter/test/cupertino/date_picker_test.dart index 0b8908f0f09..311ab59da2e 100644 --- a/packages/flutter/test/cupertino/date_picker_test.dart +++ b/packages/flutter/test/cupertino/date_picker_test.dart @@ -104,6 +104,45 @@ void main() { ); }); + testWidgets('background color default value', (WidgetTester tester) async { + await tester.pumpWidget( + CupertinoApp( + home: CupertinoTimerPicker( + onTimerDurationChanged: (_) { }, + ), + ), + ); + + final Iterable pickers = tester.allWidgets.whereType(); + expect(pickers.any((CupertinoPicker picker) => picker.backgroundColor != CupertinoColors.white), false); + }); + + testWidgets('background color is not null', (WidgetTester tester) async { + expect( + () { + CupertinoTimerPicker( + onTimerDurationChanged: (_) { }, + backgroundColor: null, + ); + }, + throwsAssertionError, + ); + }); + + testWidgets('specified background color is applied', (WidgetTester tester) async { + await tester.pumpWidget( + CupertinoApp( + home: CupertinoTimerPicker( + onTimerDurationChanged: (_) { }, + backgroundColor: CupertinoColors.black, + ), + ), + ); + + final Iterable pickers = tester.allWidgets.whereType(); + expect(pickers.any((CupertinoPicker picker) => picker.backgroundColor != CupertinoColors.black), false); + }); + testWidgets('columns are ordered correctly when text direction is ltr', (WidgetTester tester) async { await tester.pumpWidget( CupertinoApp( @@ -265,6 +304,45 @@ void main() { expect(picker.initialDateTime, isNotNull); }); + testWidgets('background color default value', (WidgetTester tester) async { + await tester.pumpWidget( + CupertinoApp( + home: CupertinoDatePicker( + onDateTimeChanged: (_) { }, + ), + ), + ); + + final Iterable pickers = tester.allWidgets.whereType(); + expect(pickers.any((CupertinoPicker picker) => picker.backgroundColor != CupertinoColors.white), false); + }); + + testWidgets('background color is not null', (WidgetTester tester) async { + expect( + () { + CupertinoDatePicker( + onDateTimeChanged: (_) { }, + backgroundColor: null, + ); + }, + throwsAssertionError, + ); + }); + + testWidgets('specified background color is applied', (WidgetTester tester) async { + await tester.pumpWidget( + CupertinoApp( + home: CupertinoDatePicker( + onDateTimeChanged: (_) { }, + backgroundColor: CupertinoColors.black, + ), + ), + ); + + final Iterable pickers = tester.allWidgets.whereType(); + expect(pickers.any((CupertinoPicker picker) => picker.backgroundColor != CupertinoColors.black), false); + }); + testWidgets('initial date honors minuteInterval', (WidgetTester tester) async { DateTime newDateTime; await tester.pumpWidget(