Skip to content

Commit 95e52da

Browse files
authored
Revert "Add default selection style (#100719)" (#101921)
* Revert "Add default selection style (#100719)" This reverts commit c8057bc. * update * update
1 parent a63402a commit 95e52da

15 files changed

+57
-359
lines changed

packages/flutter/lib/src/cupertino/app.dart

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ import 'theme.dart';
4343
/// widget, which the [CupertinoApp] composes. If you use Material widgets, a
4444
/// [MaterialApp] also creates the needed dependencies for Cupertino widgets.
4545
///
46-
/// {@template flutter.cupertino.CupertinoApp.defaultSelectionStyle}
47-
/// The [CupertinoApp] automatically creates a [DefaultSelectionStyle] with
48-
/// selectionColor sets to [CupertinoThemeData.primaryColor] with 0.2 opacity
49-
/// and cursorColor sets to [CupertinoThemeData.primaryColor].
50-
/// {@endtemplate}
51-
///
5246
/// Use this widget with caution on Android since it may produce behaviors
5347
/// Android users are not expecting such as:
5448
///
@@ -590,14 +584,10 @@ class _CupertinoAppState extends State<CupertinoApp> {
590584
data: CupertinoUserInterfaceLevelData.base,
591585
child: CupertinoTheme(
592586
data: effectiveThemeData,
593-
child: DefaultSelectionStyle(
594-
selectionColor: effectiveThemeData.primaryColor.withOpacity(0.2),
595-
cursorColor: effectiveThemeData.primaryColor,
596-
child: HeroControllerScope(
597-
controller: _heroController,
598-
child: Builder(
599-
builder: _buildWidgetApp,
600-
),
587+
child: HeroControllerScope(
588+
controller: _heroController,
589+
child: Builder(
590+
builder: _buildWidgetApp,
601591
),
602592
),
603593
),

packages/flutter/lib/src/cupertino/text_field.dart

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,7 @@ class CupertinoTextField extends StatefulWidget {
710710

711711
/// The color to use when painting the cursor.
712712
///
713-
/// Defaults to the [DefaultSelectionStyle.cursorColor]. If that color is
714-
/// null, it uses the [CupertinoThemeData.primaryColor] of the ambient theme,
713+
/// Defaults to the [CupertinoThemeData.primaryColor] of the ambient theme,
715714
/// which itself defaults to [CupertinoColors.activeBlue] in the light theme
716715
/// and [CupertinoColors.activeOrange] in the dark theme.
717716
final Color? cursorColor;
@@ -1179,11 +1178,7 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> with Restoratio
11791178
final TextStyle placeholderStyle = textStyle.merge(resolvedPlaceholderStyle);
11801179

11811180
final Brightness keyboardAppearance = widget.keyboardAppearance ?? CupertinoTheme.brightnessOf(context);
1182-
final Color cursorColor = CupertinoDynamicColor.maybeResolve(
1183-
widget.cursorColor ?? DefaultSelectionStyle.of(context).cursorColor,
1184-
context,
1185-
) ?? themeData.primaryColor;
1186-
1181+
final Color cursorColor = CupertinoDynamicColor.maybeResolve(widget.cursorColor, context) ?? themeData.primaryColor;
11871182
final Color disabledColor = CupertinoDynamicColor.resolve(_kDisabledBackground, context);
11881183

11891184
final Color? decorationColor = CupertinoDynamicColor.maybeResolve(widget.decoration?.color, context);
@@ -1211,10 +1206,7 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> with Restoratio
12111206
color: enabled ? decorationColor : disabledColor,
12121207
);
12131208

1214-
final Color selectionColor = CupertinoDynamicColor.maybeResolve(
1215-
DefaultSelectionStyle.of(context).selectionColor,
1216-
context,
1217-
) ?? CupertinoTheme.of(context).primaryColor.withOpacity(0.2);
1209+
final Color selectionColor = CupertinoTheme.of(context).primaryColor.withOpacity(0.2);
12181210

12191211
final Widget paddedEditable = Padding(
12201212
padding: widget.padding,

packages/flutter/lib/src/material/app.dart

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,6 @@ enum ThemeMode {
8282
/// This widget also configures the observer of the top-level [Navigator] (if
8383
/// any) to perform [Hero] animations.
8484
///
85-
/// {@template flutter.material.MaterialApp.defaultSelectionStyle}
86-
/// The [MaterialApp] automatically creates a [DefaultSelectionStyle]. It uses
87-
/// the colors in the [ThemeData.textSelectionTheme] if they are not null;
88-
/// otherwise, the [MaterialApp] sets [DefaultSelectionStyle.selectionColor] to
89-
/// [ColorScheme.primary] with 0.4 opacity and
90-
/// [DefaultSelectionStyle.cursorColor] to [ColorScheme.primary].
91-
/// {@endtemplate}
92-
///
9385
/// If [home], [routes], [onGenerateRoute], and [onUnknownRoute] are all null,
9486
/// and [builder] is not null, then no [Navigator] is created.
9587
///
@@ -879,35 +871,29 @@ class _MaterialAppState extends State<MaterialApp> {
879871
theme = widget.highContrastTheme;
880872
}
881873
theme ??= widget.theme ?? ThemeData.light();
882-
final Color effectiveSelectionColor = theme.textSelectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
883-
final Color effectiveCursorColor = theme.textSelectionTheme.cursorColor ?? theme.colorScheme.primary;
884874

885875
return ScaffoldMessenger(
886876
key: widget.scaffoldMessengerKey,
887-
child: DefaultSelectionStyle(
888-
selectionColor: effectiveSelectionColor,
889-
cursorColor: effectiveCursorColor,
890-
child: AnimatedTheme(
891-
data: theme,
892-
child: widget.builder != null
893-
? Builder(
894-
builder: (BuildContext context) {
895-
// Why are we surrounding a builder with a builder?
896-
//
897-
// The widget.builder may contain code that invokes
898-
// Theme.of(), which should return the theme we selected
899-
// above in AnimatedTheme. However, if we invoke
900-
// widget.builder() directly as the child of AnimatedTheme
901-
// then there is no Context separating them, and the
902-
// widget.builder() will not find the theme. Therefore, we
903-
// surround widget.builder with yet another builder so that
904-
// a context separates them and Theme.of() correctly
905-
// resolves to the theme we passed to AnimatedTheme.
906-
return widget.builder!(context, child);
907-
},
908-
)
909-
: child ?? const SizedBox.shrink(),
910-
),
877+
child: AnimatedTheme(
878+
data: theme,
879+
child: widget.builder != null
880+
? Builder(
881+
builder: (BuildContext context) {
882+
// Why are we surrounding a builder with a builder?
883+
//
884+
// The widget.builder may contain code that invokes
885+
// Theme.of(), which should return the theme we selected
886+
// above in AnimatedTheme. However, if we invoke
887+
// widget.builder() directly as the child of AnimatedTheme
888+
// then there is no Context separating them, and the
889+
// widget.builder() will not find the theme. Therefore, we
890+
// surround widget.builder with yet another builder so that
891+
// a context separates them and Theme.of() correctly
892+
// resolves to the theme we passed to AnimatedTheme.
893+
return widget.builder!(context, child);
894+
},
895+
)
896+
: child ?? const SizedBox.shrink(),
911897
),
912898
);
913899
}

packages/flutter/lib/src/material/selectable_text.dart

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:flutter/rendering.dart';
1212
import 'desktop_text_selection.dart';
1313
import 'feedback.dart';
1414
import 'text_selection.dart';
15+
import 'text_selection_theme.dart';
1516
import 'theme.dart';
1617

1718
/// An eyeballed value that moves the cursor slightly left of where it is
@@ -356,14 +357,9 @@ class SelectableText extends StatefulWidget {
356357
/// {@macro flutter.widgets.editableText.cursorRadius}
357358
final Radius? cursorRadius;
358359

359-
/// The color of the cursor.
360+
/// The color to use when painting the cursor.
360361
///
361-
/// The cursor indicates the current text insertion point.
362-
///
363-
/// If null then [DefaultSelectionStyle.cursorColor] is used. If that is also
364-
/// null and [ThemeData.platform] is [TargetPlatform.iOS] or
365-
/// [TargetPlatform.macOS], then [CupertinoThemeData.primaryColor] is used.
366-
/// Otherwise [ColorScheme.primary] of [ThemeData.colorScheme] is used.
362+
/// Defaults to the theme's `cursorColor` when null.
367363
final Color? cursorColor;
368364

369365
/// Controls how tall the selection highlight boxes are computed to be.
@@ -599,14 +595,14 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio
599595
);
600596

601597
final ThemeData theme = Theme.of(context);
602-
final DefaultSelectionStyle selectionStyle = DefaultSelectionStyle.of(context);
598+
final TextSelectionThemeData selectionTheme = TextSelectionTheme.of(context);
603599
final FocusNode focusNode = _effectiveFocusNode;
604600

605601
TextSelectionControls? textSelectionControls = widget.selectionControls;
606602
final bool paintCursorAboveText;
607603
final bool cursorOpacityAnimates;
608604
Offset? cursorOffset;
609-
final Color cursorColor;
605+
Color? cursorColor = widget.cursorColor;
610606
final Color selectionColor;
611607
Radius? cursorRadius = widget.cursorRadius;
612608

@@ -617,8 +613,8 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio
617613
textSelectionControls ??= cupertinoTextSelectionControls;
618614
paintCursorAboveText = true;
619615
cursorOpacityAnimates = true;
620-
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? cupertinoTheme.primaryColor;
621-
selectionColor = selectionStyle.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40);
616+
cursorColor ??= selectionTheme.cursorColor ?? cupertinoTheme.primaryColor;
617+
selectionColor = selectionTheme.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40);
622618
cursorRadius ??= const Radius.circular(2.0);
623619
cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0);
624620
break;
@@ -629,8 +625,8 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio
629625
textSelectionControls ??= cupertinoDesktopTextSelectionControls;
630626
paintCursorAboveText = true;
631627
cursorOpacityAnimates = true;
632-
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? cupertinoTheme.primaryColor;
633-
selectionColor = selectionStyle.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40);
628+
cursorColor ??= selectionTheme.cursorColor ?? cupertinoTheme.primaryColor;
629+
selectionColor = selectionTheme.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40);
634630
cursorRadius ??= const Radius.circular(2.0);
635631
cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0);
636632
break;
@@ -641,8 +637,8 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio
641637
textSelectionControls ??= materialTextSelectionControls;
642638
paintCursorAboveText = false;
643639
cursorOpacityAnimates = false;
644-
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? theme.colorScheme.primary;
645-
selectionColor = selectionStyle.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
640+
cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;
641+
selectionColor = selectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
646642
break;
647643

648644
case TargetPlatform.linux:
@@ -651,8 +647,8 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio
651647
textSelectionControls ??= desktopTextSelectionControls;
652648
paintCursorAboveText = false;
653649
cursorOpacityAnimates = false;
654-
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? theme.colorScheme.primary;
655-
selectionColor = selectionStyle.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
650+
cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;
651+
selectionColor = selectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
656652
break;
657653
}
658654

packages/flutter/lib/src/material/text_field.dart

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import 'material_localizations.dart';
1919
import 'material_state.dart';
2020
import 'selectable_text.dart' show iOSHorizontalOffset;
2121
import 'text_selection.dart';
22+
import 'text_selection_theme.dart';
2223
import 'theme.dart';
2324

2425
export 'package:flutter/services.dart' show TextInputType, TextInputAction, TextCapitalization, SmartQuotesType, SmartDashesType;
@@ -613,7 +614,7 @@ class TextField extends StatefulWidget {
613614
/// the field.
614615
///
615616
/// If this is null it will default to the ambient
616-
/// [DefaultSelectionStyle.cursorColor]. If that is null, and the
617+
/// [TextSelectionThemeData.cursorColor]. If that is null, and the
617618
/// [ThemeData.platform] is [TargetPlatform.iOS] or [TargetPlatform.macOS]
618619
/// it will use [CupertinoThemeData.primaryColor]. Otherwise it will use
619620
/// the value of [ColorScheme.primary] of [ThemeData.colorScheme].
@@ -1115,7 +1116,7 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
11151116
);
11161117

11171118
final ThemeData theme = Theme.of(context);
1118-
final DefaultSelectionStyle selectionStyle = DefaultSelectionStyle.of(context);
1119+
final TextSelectionThemeData selectionTheme = TextSelectionTheme.of(context);
11191120
final TextStyle style = theme.textTheme.subtitle1!.merge(widget.style);
11201121
final Brightness keyboardAppearance = widget.keyboardAppearance ?? theme.brightness;
11211122
final TextEditingController controller = _effectiveController;
@@ -1133,7 +1134,7 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
11331134
final bool paintCursorAboveText;
11341135
final bool cursorOpacityAnimates;
11351136
Offset? cursorOffset;
1136-
final Color cursorColor;
1137+
Color? cursorColor = widget.cursorColor;
11371138
final Color selectionColor;
11381139
Color? autocorrectionTextRectColor;
11391140
Radius? cursorRadius = widget.cursorRadius;
@@ -1146,8 +1147,8 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
11461147
textSelectionControls ??= cupertinoTextSelectionControls;
11471148
paintCursorAboveText = true;
11481149
cursorOpacityAnimates = true;
1149-
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? cupertinoTheme.primaryColor;
1150-
selectionColor = selectionStyle.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40);
1150+
cursorColor ??= selectionTheme.cursorColor ?? cupertinoTheme.primaryColor;
1151+
selectionColor = selectionTheme.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40);
11511152
cursorRadius ??= const Radius.circular(2.0);
11521153
cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0);
11531154
autocorrectionTextRectColor = selectionColor;
@@ -1159,8 +1160,8 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
11591160
textSelectionControls ??= cupertinoDesktopTextSelectionControls;
11601161
paintCursorAboveText = true;
11611162
cursorOpacityAnimates = true;
1162-
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? cupertinoTheme.primaryColor;
1163-
selectionColor = selectionStyle.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40);
1163+
cursorColor ??= selectionTheme.cursorColor ?? cupertinoTheme.primaryColor;
1164+
selectionColor = selectionTheme.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40);
11641165
cursorRadius ??= const Radius.circular(2.0);
11651166
cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0);
11661167
handleDidGainAccessibilityFocus = () {
@@ -1177,26 +1178,26 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
11771178
textSelectionControls ??= materialTextSelectionControls;
11781179
paintCursorAboveText = false;
11791180
cursorOpacityAnimates = false;
1180-
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? theme.colorScheme.primary;
1181-
selectionColor = selectionStyle.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
1181+
cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;
1182+
selectionColor = selectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
11821183
break;
11831184

11841185
case TargetPlatform.linux:
11851186
forcePressEnabled = false;
11861187
textSelectionControls ??= desktopTextSelectionControls;
11871188
paintCursorAboveText = false;
11881189
cursorOpacityAnimates = false;
1189-
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? theme.colorScheme.primary;
1190-
selectionColor = selectionStyle.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
1190+
cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;
1191+
selectionColor = selectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
11911192
break;
11921193

11931194
case TargetPlatform.windows:
11941195
forcePressEnabled = false;
11951196
textSelectionControls ??= desktopTextSelectionControls;
11961197
paintCursorAboveText = false;
11971198
cursorOpacityAnimates = false;
1198-
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? theme.colorScheme.primary;
1199-
selectionColor = selectionStyle.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
1199+
cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;
1200+
selectionColor = selectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
12001201
handleDidGainAccessibilityFocus = () {
12011202
// Automatically activate the TextField when it receives accessibility focus.
12021203
if (!_effectiveFocusNode.hasFocus && _effectiveFocusNode.canRequestFocus) {

packages/flutter/lib/src/material/text_selection_theme.dart

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,6 @@ class TextSelectionThemeData with Diagnosticable {
129129
/// )
130130
/// ```
131131
/// {@end-tool}
132-
///
133-
/// This widget also creates a [DefaultSelectionStyle] for its subtree with
134-
/// [data].
135132
class TextSelectionTheme extends InheritedTheme {
136133
/// Creates a text selection theme widget that specifies the text
137134
/// selection properties for all widgets below it in the widget tree.
@@ -140,29 +137,12 @@ class TextSelectionTheme extends InheritedTheme {
140137
const TextSelectionTheme({
141138
super.key,
142139
required this.data,
143-
required Widget child,
144-
}) : assert(data != null),
145-
_child = child,
146-
// See `get child` override below.
147-
super(child: const _NullWidget());
140+
required super.child,
141+
}) : assert(data != null);
148142

149143
/// The properties for descendant [TextField] and [SelectableText] widgets.
150144
final TextSelectionThemeData data;
151145

152-
// Overriding the getter to insert `DefaultSelectionStyle` into the subtree
153-
// without breaking API. In general, this approach should be avoided
154-
// because it relies on an implementation detail of ProxyWidget. This
155-
// workaround is necessary because TextSelectionTheme is const.
156-
@override
157-
Widget get child {
158-
return DefaultSelectionStyle(
159-
selectionColor: data.selectionColor,
160-
cursorColor: data.cursorColor,
161-
child: _child,
162-
);
163-
}
164-
final Widget _child;
165-
166146
/// Returns the [data] from the closest [TextSelectionTheme] ancestor. If
167147
/// there is no ancestor, it returns [ThemeData.textSelectionTheme].
168148
/// Applications can assume that the returned value will not be null.
@@ -185,10 +165,3 @@ class TextSelectionTheme extends InheritedTheme {
185165
@override
186166
bool updateShouldNotify(TextSelectionTheme oldWidget) => data != oldWidget.data;
187167
}
188-
189-
class _NullWidget extends Widget {
190-
const _NullWidget();
191-
192-
@override
193-
Element createElement() => throw UnimplementedError();
194-
}

0 commit comments

Comments
 (0)