Skip to content

Commit

Permalink
Fix text color not resolving when CupertinoThemeData.brightness is …
Browse files Browse the repository at this point in the history
…null (#115026)

* Fix text color not resolving in `CupertinoApp`

* Make linter happy

* Create cupertinoBuilder for theming

* Make linter happy

* Fix tests

* Fix regression

* Delete whitespace

* Resolve color passed to the `WidgetsApp`

* Revert "Resolve color passed to the `WidgetsApp`"

This reverts commit 6994719a564c476dcb6d389e4547bff3ded08203.

* Resolve selection colors

* Kick tests

* Delete doubled test
  • Loading branch information
ivirtex authored Feb 27, 2023
1 parent 516b60b commit edba606
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
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

0 comments on commit edba606

Please sign in to comment.