From bd2ca58db388bcdc0593fef9422f48f9b1ea6445 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Mon, 2 May 2022 22:42:03 +0300 Subject: [PATCH] Fix `CupertinoPicker`dark mode text color (#100310) Improved code --- .../flutter/lib/src/cupertino/picker.dart | 3 +- .../flutter/test/cupertino/picker_test.dart | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/cupertino/picker.dart b/packages/flutter/lib/src/cupertino/picker.dart index 45c981d3bca..de1884b71a9 100644 --- a/packages/flutter/lib/src/cupertino/picker.dart +++ b/packages/flutter/lib/src/cupertino/picker.dart @@ -281,11 +281,12 @@ class _CupertinoPickerState extends State { @override Widget build(BuildContext context) { + final TextStyle textStyle = CupertinoTheme.of(context).textTheme.pickerTextStyle; final Color? resolvedBackgroundColor = CupertinoDynamicColor.maybeResolve(widget.backgroundColor, context); assert(RenderListWheelViewport.defaultPerspective == _kDefaultPerspective); final Widget result = DefaultTextStyle( - style: CupertinoTheme.of(context).textTheme.pickerTextStyle, + style: textStyle.copyWith(color: CupertinoDynamicColor.maybeResolve(textStyle.color, context)), child: Stack( children: [ Positioned.fill( diff --git a/packages/flutter/test/cupertino/picker_test.dart b/packages/flutter/test/cupertino/picker_test.dart index 58d82bc2e75..68fb3bdeb70 100644 --- a/packages/flutter/test/cupertino/picker_test.dart +++ b/packages/flutter/test/cupertino/picker_test.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -423,6 +424,46 @@ void main() { }, variant: const TargetPlatformVariant({ TargetPlatform.iOS, TargetPlatform.macOS })); }); + testWidgets('Picker adapts to MaterialApp dark mode', (WidgetTester tester) async { + Widget _buildCupertinoPicker(Brightness brightness) { + return MaterialApp( + theme: ThemeData(brightness: brightness), + home: Align( + alignment: Alignment.topLeft, + child: SizedBox( + height: 300.0, + width: 300.0, + child: CupertinoPicker( + itemExtent: 50.0, + onSelectedItemChanged: (_) { }, + children: List.generate(3, (int index) { + return SizedBox( + height: 50.0, + width: 300.0, + child: Text(index.toString()), + ); + }), + ), + ), + ), + ); + } + + // CupertinoPicker with light theme. + await tester.pumpWidget(_buildCupertinoPicker(Brightness.light)); + RenderParagraph paragraph = tester.renderObject(find.text('1')); + expect(paragraph.text.style!.color, CupertinoColors.label); + // Text style should not return unresolved color. + expect(paragraph.text.style!.color.toString().contains('UNRESOLVED'), isFalse); + + // CupertinoPicker with dark theme. + await tester.pumpWidget(_buildCupertinoPicker(Brightness.dark)); + paragraph = tester.renderObject(find.text('1')); + expect(paragraph.text.style!.color, CupertinoColors.label); + // Text style should not return unresolved color. + expect(paragraph.text.style!.color.toString().contains('UNRESOLVED'), isFalse); + }); + group('CupertinoPickerDefaultSelectionOverlay', () { testWidgets('should be using directional decoration', (WidgetTester tester) async { await tester.pumpWidget(