Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix text color not resolving when CupertinoThemeData.brightness is null #115026

Merged
merged 12 commits into from
Feb 27, 2023
3 changes: 2 additions & 1 deletion packages/flutter/lib/src/cupertino/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ class _CupertinoAppState extends State<CupertinoApp> {
restorationScopeId: widget.restorationScopeId,
);
}

return WidgetsApp(
key: GlobalObjectKey(this),
navigatorKey: widget.navigatorKey,
Expand Down Expand Up @@ -595,7 +596,7 @@ class _CupertinoAppState extends State<CupertinoApp> {

@override
Widget build(BuildContext context) {
final CupertinoThemeData effectiveThemeData = widget.theme ?? const CupertinoThemeData();
final CupertinoThemeData effectiveThemeData = (widget.theme ?? const CupertinoThemeData()).resolveFrom(context);

return ScrollConfiguration(
behavior: widget.scrollBehavior ?? const CupertinoScrollBehavior(),
Expand Down
92 changes: 92 additions & 0 deletions packages/flutter/test/cupertino/app_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

Expand Down Expand Up @@ -332,6 +333,97 @@ void main() {
);
expect(capturedContext.dependOnInheritedWidgetOfExactType<MediaQuery>()?.key, uniqueKey);
});

testWidgets('Text color is correctly resolved when CupertinoThemeData.brightness is null', (WidgetTester tester) async {
debugBrightnessOverride = Brightness.dark;

await tester.pumpWidget(
const CupertinoApp(
home: CupertinoPageScaffold(
child: Text('Hello'),
),
),
);

final RenderParagraph paragraph = tester.renderObject(find.text('Hello'));
final CupertinoDynamicColor textColor = paragraph.text.style!.color! as CupertinoDynamicColor;

// App with non-null brightness, so resolving color
// doesn't depend on the MediaQuery.platformBrightness.
late BuildContext capturedContext;
await tester.pumpWidget(
CupertinoApp(
theme: const CupertinoThemeData(
brightness: Brightness.dark,
),
home: Builder(
builder: (BuildContext context) {
capturedContext = context;

return const Placeholder();
},
),
),
);

// We expect the string representations of the colors to have darkColor indicated (*) as effective color.
// (color = Color(0xff000000), *darkColor = Color(0xffffffff)*, resolved by: Builder)
expect(textColor.toString(), CupertinoColors.label.resolveFrom(capturedContext).toString());

debugBrightnessOverride = null;
});

testWidgets('Cursor color is resolved when CupertinoThemeData.brightness is null', (WidgetTester tester) async {
debugBrightnessOverride = Brightness.dark;

RenderEditable findRenderEditable(WidgetTester tester) {
final RenderObject root = tester.renderObject(find.byType(EditableText));
expect(root, isNotNull);

RenderEditable? renderEditable;
void recursiveFinder(RenderObject child) {
if (child is RenderEditable) {
renderEditable = child;
return;
}
child.visitChildren(recursiveFinder);
}

root.visitChildren(recursiveFinder);
expect(renderEditable, isNotNull);
return renderEditable!;
}

await tester.pumpWidget(
CupertinoApp(
theme: const CupertinoThemeData(
primaryColor: CupertinoColors.activeOrange,
),
home: CupertinoPageScaffold(
child: Builder(
builder: (BuildContext context) {
return EditableText(
backgroundCursorColor: DefaultSelectionStyle.of(context).selectionColor!,
cursorColor: DefaultSelectionStyle.of(context).cursorColor!,
controller: TextEditingController(),
focusNode: FocusNode(),
style: const TextStyle(),
);
},
),
),
),
);

final RenderEditable editableText = findRenderEditable(tester);
final Color cursorColor = editableText.cursorColor!;

// Cursor color should be equal to the dark variant of the primary color.
// Alpha value needs to be 0, because cursor is not visible by default.
expect(cursorColor, CupertinoColors.activeOrange.darkColor.withAlpha(0));

debugBrightnessOverride = null;
});
}

class MockScrollBehavior extends ScrollBehavior {
Expand Down