Skip to content

Commit 98a4d30

Browse files
Renzo-OlivaressuryaKommana2662
authored andcommitted
Make the Material fallback theme follow defaultTargetPlatform (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 **This will still need a follow up PR that does the same change in `material_ui`** Fixes flutter#83788 Fixes flutter#194008 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
1 parent e25ce61 commit 98a4d30

3 files changed

Lines changed: 159 additions & 109 deletions

File tree

‎packages/flutter/lib/src/material/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

‎packages/flutter/test/material/text_field_test.dart‎

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@@ -3314,6 +3289,11 @@ void main() {
33143289
expect(selection.baseOffset, 4);
33153290
expect(selection.extentOffset, 7);
33163291

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

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

‎packages/flutter/test/material/theme_test.dart‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:ui' as ui;
66

77
import 'package:flutter/cupertino.dart';
8+
import 'package:flutter/foundation.dart';
89
import 'package:flutter/material.dart';
910
import 'package:flutter/rendering.dart';
1011
import 'package:flutter_test/flutter_test.dart';
@@ -228,6 +229,22 @@ void main() {
228229
);
229230
});
230231

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

0 commit comments

Comments
 (0)