Skip to content

Commit 3675456

Browse files
[material_ui] Fallback theme follows platform cherry pick (flutter#13278)
**Port of: flutter#194256 **Issue:** `Theme.of(context)` falls back to a `ThemeData` built once per isolate (`Theme._kFallbackTheme`) when there is no `Theme` ancestor. `ThemeData()` captures `defaultTargetPlatform` at construction, so the fallback's platform is frozen to whatever platform was in effect the first time a theme-less widget called `Theme.of(context)`. Under a `TargetPlatformVariant`, `debugDefaultTargetPlatformOverride` every later theme-less subtree keeps that first platform's selection controls, toolbar, cursor and other adaptive behavior. In a shuffled test run this makes theme-less tests depend on test order: with seed 20261008, text_field_test.dart fails 45 tests that pass in declaration order. **Fix:** Rebuild the cached fallback whenever its platform no longer matches `defaultTargetPlatform`, so a theme-less subtree adapts to the current platform the same way a `MaterialApp` does. `Theme`s built with an explicit platform are unaffected; only the fallback is compared. Adds a regression test in `theme_test.dart` that fails on master for every non-android variant. With the fallback following the variant, the two handle-drag tests in `text_field_test.dart` fail because they had only ever run against Android handles: desktop platforms draw no selection handles, and the Cupertino start handle sits above the line. These tests have been adjusted; restricted to the platforms that draw handles, the iOS start handle is grabbed where it actually is, the Fuchsia toolbar that covers the handles is hidden, and a test asserts that desktop platforms draw no handles. The orphaned no-shuffle tag is removed. **Other alternatives considered:** - `text_field_test.dart` only fix, tests explicitly install their own theme so `Theme.of(context)` does not return leaky fallback. Decided against this since it wouldn't fix this leak for all users as reported in flutter#83788 Fixes [flutter#83788](flutter#83788) Fixes [flutter#194008](flutter#194008) ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent df2c368 commit 3675456

4 files changed

Lines changed: 162 additions & 109 deletions

File tree

‎packages/material_ui/lib/src/theme.dart‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,21 @@ class Theme extends StatelessWidget {
5959
/// {@macro flutter.widgets.ProxyWidget.child}
6060
final Widget child;
6161

62-
static final ThemeData _kFallbackTheme = ThemeData.fallback();
62+
static ThemeData? _cachedFallbackTheme;
63+
64+
/// The theme [Theme.of] returns when there is no [Theme] ancestor.
65+
///
66+
/// The fallback is cached because constructing a [ThemeData] is expensive,
67+
/// but it is rebuilt whenever [defaultTargetPlatform] no longer matches the
68+
/// platform it was built for (for example under a `TargetPlatformVariant` or
69+
/// after a platform override), so a theme-less subtree adapts to the current
70+
/// platform the same way a [MaterialApp] would.
71+
static ThemeData get _fallbackTheme {
72+
if (_cachedFallbackTheme?.platform != defaultTargetPlatform) {
73+
_cachedFallbackTheme = ThemeData.fallback();
74+
}
75+
return _cachedFallbackTheme!;
76+
}
6377

6478
/// The data from the closest [Theme] instance that encloses the given
6579
/// context.
@@ -134,7 +148,7 @@ class Theme extends StatelessWidget {
134148
(inheritedCupertinoTheme != null
135149
? CupertinoBasedMaterialThemeData(themeData: inheritedCupertinoTheme.theme.data)
136150
.materialTheme
137-
: _kFallbackTheme);
151+
: _fallbackTheme);
138152
return ThemeData.localize(theme, theme.typography.geometryThemeFor(category));
139153
}
140154

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes `Theme.of` returning a fallback `ThemeData` whose `platform` was frozen to the first `defaultTargetPlatform` seen by the process. A subtree without a `Theme` ancestor now adapts when `defaultTargetPlatform` changes, for example under a `TargetPlatformVariant` or the platform override.
3+
version: patch

‎packages/material_ui/test/text_field_test.dart‎

Lines changed: 126 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -3170,124 +3170,99 @@ void main() {
31703170
expect(controller.selection.extentOffset, testValue.indexOf('g'));
31713171
});
31723172

3173-
testWidgets(
3174-
'Can drag handles to change selection on Apple platforms',
3175-
(WidgetTester tester) async {
3176-
final TextEditingController controller = _textEditingController();
3173+
testWidgets('Can drag handles to change selection on iOS', (WidgetTester tester) async {
3174+
final TextEditingController controller = _textEditingController();
31773175

3178-
await tester.pumpWidget(
3179-
overlay(
3180-
child: TextField(dragStartBehavior: DragStartBehavior.down, controller: controller),
3181-
),
3182-
);
3176+
await tester.pumpWidget(
3177+
overlay(
3178+
child: TextField(dragStartBehavior: DragStartBehavior.down, controller: controller),
3179+
),
3180+
);
31833181

3184-
const testValue = 'abc def ghi';
3185-
await tester.enterText(find.byType(TextField), testValue);
3186-
await skipPastScrollingAnimation(tester);
3182+
const testValue = 'abc def ghi';
3183+
await tester.enterText(find.byType(TextField), testValue);
3184+
await skipPastScrollingAnimation(tester);
31873185

3188-
// Double tap the 'e' to select 'def'.
3189-
final Offset ePos = textOffsetToPosition(tester, testValue.indexOf('e'));
3190-
// The first tap.
3191-
TestGesture gesture = await tester.startGesture(ePos, pointer: 7);
3192-
await tester.pump();
3193-
await gesture.up();
3194-
await tester.pump();
3195-
await tester.pump(
3196-
const Duration(milliseconds: 200),
3197-
); // skip past the frame where the opacity is zero
3186+
// Double tap the 'e' to select 'def'.
3187+
final Offset ePos = textOffsetToPosition(tester, testValue.indexOf('e'));
3188+
// The first tap.
3189+
TestGesture gesture = await tester.startGesture(ePos, pointer: 7);
3190+
await tester.pump();
3191+
await gesture.up();
3192+
await tester.pump();
3193+
await tester.pump(
3194+
const Duration(milliseconds: 200),
3195+
); // skip past the frame where the opacity is zero
31983196

3199-
// The second tap.
3200-
await gesture.down(ePos);
3201-
await tester.pump();
3202-
await gesture.up();
3203-
await tester.pump();
3197+
// The second tap.
3198+
await gesture.down(ePos);
3199+
await tester.pump();
3200+
await gesture.up();
3201+
await tester.pump();
32043202

3205-
final TextSelection selection = controller.selection;
3206-
expect(selection.baseOffset, 4);
3207-
expect(selection.extentOffset, 7);
3203+
final TextSelection selection = controller.selection;
3204+
expect(selection.baseOffset, 4);
3205+
expect(selection.extentOffset, 7);
32083206

3209-
final RenderEditable renderEditable = findRenderEditable(tester);
3210-
List<TextSelectionPoint> endpoints = globalize(
3211-
renderEditable.getEndpointsForSelection(selection),
3212-
renderEditable,
3213-
);
3214-
expect(endpoints.length, 2);
3207+
final RenderEditable renderEditable = findRenderEditable(tester);
3208+
List<TextSelectionPoint> endpoints = globalize(
3209+
renderEditable.getEndpointsForSelection(selection),
3210+
renderEditable,
3211+
);
3212+
expect(endpoints.length, 2);
32153213

3216-
// Drag the right handle 2 letters to the right.
3217-
// We use a small offset because the endpoint is on the very corner
3218-
// of the handle.
3219-
Offset handlePos = endpoints[1].point + const Offset(1.0, 1.0);
3220-
Offset newHandlePos = textOffsetToPosition(tester, testValue.length);
3221-
gesture = await tester.startGesture(handlePos, pointer: 7);
3222-
await tester.pump();
3223-
await gesture.moveTo(newHandlePos);
3224-
await tester.pump();
3225-
await gesture.up();
3226-
await tester.pump();
3214+
// Drag the right handle 2 letters to the right.
3215+
// We use a small offset because the endpoint is on the very corner
3216+
// of the handle.
3217+
Offset handlePos = endpoints[1].point + const Offset(1.0, 1.0);
3218+
Offset newHandlePos = textOffsetToPosition(tester, testValue.length);
3219+
gesture = await tester.startGesture(handlePos, pointer: 7);
3220+
await tester.pump();
3221+
await gesture.moveTo(newHandlePos);
3222+
await tester.pump();
3223+
await gesture.up();
3224+
await tester.pump();
32273225

3228-
expect(controller.selection.baseOffset, 4);
3229-
expect(controller.selection.extentOffset, 11);
3226+
expect(controller.selection.baseOffset, 4);
3227+
expect(controller.selection.extentOffset, 11);
32303228

3231-
// Drag the left handle 2 letters to the left.
3232-
handlePos = endpoints[0].point + const Offset(-1.0, 1.0);
3233-
newHandlePos = textOffsetToPosition(tester, 2);
3234-
gesture = await tester.startGesture(handlePos, pointer: 7);
3235-
await tester.pump();
3236-
await gesture.moveTo(newHandlePos);
3237-
await tester.pump();
3238-
await gesture.up();
3239-
await tester.pump();
3229+
// Drag the left handle 2 letters to the left. The Cupertino start handle
3230+
// sits above the line with its anchor at the line's bottom edge, so grab
3231+
// just inside its bottom-left corner.
3232+
handlePos = endpoints[0].point + const Offset(-1.0, -1.0);
3233+
newHandlePos = textOffsetToPosition(tester, 2);
3234+
gesture = await tester.startGesture(handlePos, pointer: 7);
3235+
await tester.pump();
3236+
await gesture.moveTo(newHandlePos);
3237+
await tester.pump();
3238+
await gesture.up();
3239+
await tester.pump();
32403240

3241-
switch (defaultTargetPlatform) {
3242-
// On Apple platforms, dragging the base handle makes it the extent.
3243-
case TargetPlatform.iOS:
3244-
case TargetPlatform.macOS:
3245-
expect(controller.selection.baseOffset, 11);
3246-
expect(controller.selection.extentOffset, 2);
3247-
case TargetPlatform.android:
3248-
case TargetPlatform.fuchsia:
3249-
case TargetPlatform.linux:
3250-
case TargetPlatform.windows:
3251-
expect(controller.selection.baseOffset, 2);
3252-
expect(controller.selection.extentOffset, 11);
3253-
}
3241+
// On iOS, dragging the base handle makes it the extent.
3242+
expect(controller.selection.baseOffset, 11);
3243+
expect(controller.selection.extentOffset, 2);
32543244

3255-
// Drag the left handle 2 letters to the left again.
3256-
endpoints = globalize(
3257-
renderEditable.getEndpointsForSelection(controller.selection),
3258-
renderEditable,
3259-
);
3260-
handlePos = endpoints[0].point + const Offset(-1.0, 1.0);
3261-
newHandlePos = textOffsetToPosition(tester, 0);
3262-
gesture = await tester.startGesture(handlePos, pointer: 7);
3263-
await tester.pump();
3264-
await gesture.moveTo(newHandlePos);
3265-
await tester.pump();
3266-
await gesture.up();
3267-
await tester.pump();
3245+
// Drag the left handle 2 letters to the left again.
3246+
endpoints = globalize(
3247+
renderEditable.getEndpointsForSelection(controller.selection),
3248+
renderEditable,
3249+
);
3250+
handlePos = endpoints[0].point + const Offset(-1.0, -1.0);
3251+
newHandlePos = textOffsetToPosition(tester, 0);
3252+
gesture = await tester.startGesture(handlePos, pointer: 7);
3253+
await tester.pump();
3254+
await gesture.moveTo(newHandlePos);
3255+
await tester.pump();
3256+
await gesture.up();
3257+
await tester.pump();
32683258

3269-
switch (defaultTargetPlatform) {
3270-
case TargetPlatform.iOS:
3271-
case TargetPlatform.macOS:
3272-
// The left handle was already the extent, and it remains so.
3273-
expect(controller.selection.baseOffset, 11);
3274-
expect(controller.selection.extentOffset, 0);
3275-
case TargetPlatform.android:
3276-
case TargetPlatform.fuchsia:
3277-
case TargetPlatform.linux:
3278-
case TargetPlatform.windows:
3279-
expect(controller.selection.baseOffset, 0);
3280-
expect(controller.selection.extentOffset, 11);
3281-
}
3282-
},
3283-
variant: const TargetPlatformVariant(<TargetPlatform>{
3284-
TargetPlatform.iOS,
3285-
TargetPlatform.macOS,
3286-
}),
3287-
);
3259+
// The left handle was already the extent, and it remains so.
3260+
expect(controller.selection.baseOffset, 11);
3261+
expect(controller.selection.extentOffset, 0);
3262+
}, variant: TargetPlatformVariant.only(TargetPlatform.iOS));
32883263

32893264
testWidgets(
3290-
'Can drag handles to change selection on non-Apple platforms',
3265+
'Can drag handles to change selection on Android and Fuchsia',
32913266
(WidgetTester tester) async {
32923267
final TextEditingController controller = _textEditingController();
32933268

@@ -3315,6 +3290,11 @@ void main() {
33153290
expect(selection.baseOffset, 4);
33163291
expect(selection.extentOffset, 7);
33173292

3293+
// On Fuchsia the desktop-style toolbar shown by the long press covers the
3294+
// handles; hide it so the drags below reach them.
3295+
tester.state<EditableTextState>(find.byType(EditableText)).hideToolbar(false);
3296+
await tester.pump();
3297+
33183298
final RenderEditable renderEditable = findRenderEditable(tester);
33193299
List<TextSelectionPoint> endpoints = globalize(
33203300
renderEditable.getEndpointsForSelection(selection),
@@ -3389,11 +3369,50 @@ void main() {
33893369
expect(controller.selection.extentOffset, 11);
33903370
}
33913371
},
3392-
variant: TargetPlatformVariant.all(
3393-
excluding: <TargetPlatform>{TargetPlatform.iOS, TargetPlatform.macOS},
3394-
),
3372+
variant: const TargetPlatformVariant(<TargetPlatform>{
3373+
TargetPlatform.android,
3374+
TargetPlatform.fuchsia,
3375+
}),
33953376
);
33963377

3378+
testWidgets('Desktop platforms do not show selection handles by default', (
3379+
WidgetTester tester,
3380+
) async {
3381+
final TextEditingController controller = _textEditingController();
3382+
3383+
await tester.pumpWidget(
3384+
overlay(
3385+
child: TextField(dragStartBehavior: DragStartBehavior.down, controller: controller),
3386+
),
3387+
);
3388+
3389+
const testValue = 'abc def ghi';
3390+
await tester.enterText(find.byType(TextField), testValue);
3391+
await skipPastScrollingAnimation(tester);
3392+
3393+
// Double tap the 'e' to select 'def'.
3394+
final Offset ePos = textOffsetToPosition(tester, testValue.indexOf('e'));
3395+
final TestGesture gesture = await tester.startGesture(ePos, pointer: 7);
3396+
await tester.pump();
3397+
await gesture.up();
3398+
await tester.pump();
3399+
await tester.pump(const Duration(milliseconds: 200));
3400+
await gesture.down(ePos);
3401+
await tester.pump();
3402+
await gesture.up();
3403+
await tester.pump();
3404+
3405+
expect(controller.selection.baseOffset, 4);
3406+
expect(controller.selection.extentOffset, 7);
3407+
expect(
3408+
find.descendant(
3409+
of: find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_SelectionHandleOverlay'),
3410+
matching: find.byType(CustomPaint),
3411+
),
3412+
findsNothing,
3413+
);
3414+
}, variant: TargetPlatformVariant.desktop());
3415+
33973416
testWidgets(
33983417
'assertion error is not thrown when attempting to drag both selection handles',
33993418
(WidgetTester tester) async {

‎packages/material_ui/test/theme_test.dart‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:cupertino_ui/cupertino_ui.dart';
6+
import 'package:flutter/foundation.dart';
67
import 'package:flutter/rendering.dart';
78
import 'package:flutter_test/flutter_test.dart';
89
import 'package:material_ui/material_ui.dart';
@@ -226,6 +227,22 @@ void main() {
226227
);
227228
});
228229

230+
testWidgets('Fallback theme updates platform when defaultTargetPlatform changes', (
231+
WidgetTester tester,
232+
) async {
233+
late BuildContext capturedContext;
234+
await tester.pumpWidget(
235+
Builder(
236+
builder: (BuildContext context) {
237+
capturedContext = context;
238+
return Container();
239+
},
240+
),
241+
);
242+
243+
expect(Theme.of(capturedContext).platform, defaultTargetPlatform);
244+
}, variant: TargetPlatformVariant.all());
245+
229246
testWidgets('ThemeData.localize memoizes the result', (WidgetTester tester) async {
230247
final light = ThemeData();
231248
final dark = ThemeData.dark();

0 commit comments

Comments
 (0)