Added backgroundColor property to CupertinoTimePicker and CupertinoDatePicker. #34741 (#39056)

This commit is contained in:
Venkataramana Neelapala
2019-08-27 03:17:49 +05:30
committed by LongCatIsLooong
parent e31d88f9c9
commit 19cdb21cb6
2 changed files with 103 additions and 11 deletions

View File

@@ -210,10 +210,12 @@ class CupertinoDatePicker extends StatefulWidget {
this.maximumYear, this.maximumYear,
this.minuteInterval = 1, this.minuteInterval = 1,
this.use24hFormat = false, this.use24hFormat = false,
this.backgroundColor = _kBackgroundColor
}) : initialDateTime = initialDateTime ?? DateTime.now(), }) : initialDateTime = initialDateTime ?? DateTime.now(),
assert(mode != null), assert(mode != null),
assert(onDateTimeChanged != null), assert(onDateTimeChanged != null),
assert(minimumYear != null), assert(minimumYear != null),
assert(backgroundColor != null),
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',
@@ -281,6 +283,11 @@ class CupertinoDatePicker extends StatefulWidget {
/// null. /// null.
final ValueChanged<DateTime> onDateTimeChanged; final ValueChanged<DateTime> onDateTimeChanged;
/// Background color of date picker.
///
/// Defaults to [CupertinoColors.white] when null.
final Color backgroundColor;
@override @override
State<StatefulWidget> createState() { State<StatefulWidget> createState() {
// The `time` mode and `dateAndTime` mode of the picker share the time // The `time` mode and `dateAndTime` mode of the picker share the time
@@ -500,7 +507,7 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> {
itemExtent: _kItemExtent, itemExtent: _kItemExtent,
useMagnifier: _kUseMagnifier, useMagnifier: _kUseMagnifier,
magnification: _kMagnification, magnification: _kMagnification,
backgroundColor: _kBackgroundColor, backgroundColor: widget.backgroundColor,
squeeze: _kSqueeze, squeeze: _kSqueeze,
onSelectedItemChanged: (int index) { onSelectedItemChanged: (int index) {
selectedDayFromInitial = index; selectedDayFromInitial = index;
@@ -545,7 +552,7 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> {
itemExtent: _kItemExtent, itemExtent: _kItemExtent,
useMagnifier: _kUseMagnifier, useMagnifier: _kUseMagnifier,
magnification: _kMagnification, magnification: _kMagnification,
backgroundColor: _kBackgroundColor, backgroundColor: widget.backgroundColor,
squeeze: _kSqueeze, squeeze: _kSqueeze,
onSelectedItemChanged: (int index) { onSelectedItemChanged: (int index) {
if (widget.use24hFormat) { if (widget.use24hFormat) {
@@ -599,7 +606,7 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> {
itemExtent: _kItemExtent, itemExtent: _kItemExtent,
useMagnifier: _kUseMagnifier, useMagnifier: _kUseMagnifier,
magnification: _kMagnification, magnification: _kMagnification,
backgroundColor: _kBackgroundColor, backgroundColor: widget.backgroundColor,
squeeze: _kSqueeze, squeeze: _kSqueeze,
onSelectedItemChanged: (int index) { onSelectedItemChanged: (int index) {
selectedMinute = index * widget.minuteInterval; selectedMinute = index * widget.minuteInterval;
@@ -627,7 +634,7 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> {
itemExtent: _kItemExtent, itemExtent: _kItemExtent,
useMagnifier: _kUseMagnifier, useMagnifier: _kUseMagnifier,
magnification: _kMagnification, magnification: _kMagnification,
backgroundColor: _kBackgroundColor, backgroundColor: widget.backgroundColor,
squeeze: _kSqueeze, squeeze: _kSqueeze,
onSelectedItemChanged: (int index) { onSelectedItemChanged: (int index) {
selectedAmPm = index; selectedAmPm = index;
@@ -792,7 +799,7 @@ class _CupertinoDatePickerDateState extends State<CupertinoDatePicker> {
itemExtent: _kItemExtent, itemExtent: _kItemExtent,
useMagnifier: _kUseMagnifier, useMagnifier: _kUseMagnifier,
magnification: _kMagnification, magnification: _kMagnification,
backgroundColor: _kBackgroundColor, backgroundColor: widget.backgroundColor,
squeeze: _kSqueeze, squeeze: _kSqueeze,
onSelectedItemChanged: (int index) { onSelectedItemChanged: (int index) {
selectedDay = index + 1; selectedDay = index + 1;
@@ -823,7 +830,7 @@ class _CupertinoDatePickerDateState extends State<CupertinoDatePicker> {
itemExtent: _kItemExtent, itemExtent: _kItemExtent,
useMagnifier: _kUseMagnifier, useMagnifier: _kUseMagnifier,
magnification: _kMagnification, magnification: _kMagnification,
backgroundColor: _kBackgroundColor, backgroundColor: widget.backgroundColor,
squeeze: _kSqueeze, squeeze: _kSqueeze,
onSelectedItemChanged: (int index) { onSelectedItemChanged: (int index) {
selectedMonth = index + 1; selectedMonth = index + 1;
@@ -850,7 +857,7 @@ class _CupertinoDatePickerDateState extends State<CupertinoDatePicker> {
offAxisFraction: offAxisFraction, offAxisFraction: offAxisFraction,
useMagnifier: _kUseMagnifier, useMagnifier: _kUseMagnifier,
magnification: _kMagnification, magnification: _kMagnification,
backgroundColor: _kBackgroundColor, backgroundColor: widget.backgroundColor,
onSelectedItemChanged: (int index) { onSelectedItemChanged: (int index) {
selectedYear = index; selectedYear = index;
if (DateTime(selectedYear, selectedMonth, selectedDay).day == selectedDay) if (DateTime(selectedYear, selectedMonth, selectedDay).day == selectedDay)
@@ -1051,6 +1058,7 @@ class CupertinoTimerPicker extends StatefulWidget {
this.initialTimerDuration = Duration.zero, this.initialTimerDuration = Duration.zero,
this.minuteInterval = 1, this.minuteInterval = 1,
this.secondInterval = 1, this.secondInterval = 1,
this.backgroundColor = _kBackgroundColor,
@required this.onTimerDurationChanged, @required this.onTimerDurationChanged,
}) : assert(mode != null), }) : assert(mode != null),
assert(onTimerDurationChanged != null), assert(onTimerDurationChanged != null),
@@ -1059,7 +1067,8 @@ class CupertinoTimerPicker extends StatefulWidget {
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(backgroundColor != null);
/// The mode of the timer picker. /// The mode of the timer picker.
final CupertinoTimerPickerMode mode; final CupertinoTimerPickerMode mode;
@@ -1078,6 +1087,11 @@ class CupertinoTimerPicker extends StatefulWidget {
/// Callback called when the timer duration changes. /// Callback called when the timer duration changes.
final ValueChanged<Duration> onTimerDurationChanged; final ValueChanged<Duration> onTimerDurationChanged;
/// Background color of timer picker.
///
/// Defaults to [CupertinoColors.white] when null.
final Color backgroundColor;
@override @override
State<StatefulWidget> createState() => _CupertinoTimerPickerState(); State<StatefulWidget> createState() => _CupertinoTimerPickerState();
} }
@@ -1134,7 +1148,7 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
scrollController: FixedExtentScrollController(initialItem: selectedHour), scrollController: FixedExtentScrollController(initialItem: selectedHour),
offAxisFraction: -0.5 * textDirectionFactor, offAxisFraction: -0.5 * textDirectionFactor,
itemExtent: _kItemExtent, itemExtent: _kItemExtent,
backgroundColor: _kBackgroundColor, backgroundColor: widget.backgroundColor,
squeeze: _kSqueeze, squeeze: _kSqueeze,
onSelectedItemChanged: (int index) { onSelectedItemChanged: (int index) {
setState(() { setState(() {
@@ -1213,7 +1227,7 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
), ),
offAxisFraction: offAxisFraction, offAxisFraction: offAxisFraction,
itemExtent: _kItemExtent, itemExtent: _kItemExtent,
backgroundColor: _kBackgroundColor, backgroundColor: widget.backgroundColor,
squeeze: _kSqueeze, squeeze: _kSqueeze,
onSelectedItemChanged: (int index) { onSelectedItemChanged: (int index) {
setState(() { setState(() {
@@ -1326,7 +1340,7 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
), ),
offAxisFraction: offAxisFraction, offAxisFraction: offAxisFraction,
itemExtent: _kItemExtent, itemExtent: _kItemExtent,
backgroundColor: _kBackgroundColor, backgroundColor: widget.backgroundColor,
squeeze: _kSqueeze, squeeze: _kSqueeze,
onSelectedItemChanged: (int index) { onSelectedItemChanged: (int index) {
setState(() { setState(() {

View File

@@ -104,6 +104,45 @@ void main() {
); );
}); });
testWidgets('background color default value', (WidgetTester tester) async {
await tester.pumpWidget(
CupertinoApp(
home: CupertinoTimerPicker(
onTimerDurationChanged: (_) { },
),
),
);
final Iterable<CupertinoPicker> pickers = tester.allWidgets.whereType<CupertinoPicker>();
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<CupertinoPicker> pickers = tester.allWidgets.whereType<CupertinoPicker>();
expect(pickers.any((CupertinoPicker picker) => picker.backgroundColor != CupertinoColors.black), 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(
@@ -265,6 +304,45 @@ void main() {
expect(picker.initialDateTime, isNotNull); expect(picker.initialDateTime, isNotNull);
}); });
testWidgets('background color default value', (WidgetTester tester) async {
await tester.pumpWidget(
CupertinoApp(
home: CupertinoDatePicker(
onDateTimeChanged: (_) { },
),
),
);
final Iterable<CupertinoPicker> pickers = tester.allWidgets.whereType<CupertinoPicker>();
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<CupertinoPicker> pickers = tester.allWidgets.whereType<CupertinoPicker>();
expect(pickers.any((CupertinoPicker picker) => picker.backgroundColor != CupertinoColors.black), false);
});
testWidgets('initial date honors minuteInterval', (WidgetTester tester) async { testWidgets('initial date honors minuteInterval', (WidgetTester tester) async {
DateTime newDateTime; DateTime newDateTime;
await tester.pumpWidget( await tester.pumpWidget(