Unverified Commit 95e52dac authored by chunhtai's avatar chunhtai Committed by GitHub

Revert "Add default selection style (#100719)" (#101921)

* Revert "Add default selection style (#100719)"

This reverts commit c8057bc5.

* update

* update
parent a63402a6
...@@ -43,12 +43,6 @@ import 'theme.dart'; ...@@ -43,12 +43,6 @@ import 'theme.dart';
/// widget, which the [CupertinoApp] composes. If you use Material widgets, a /// widget, which the [CupertinoApp] composes. If you use Material widgets, a
/// [MaterialApp] also creates the needed dependencies for Cupertino widgets. /// [MaterialApp] also creates the needed dependencies for Cupertino widgets.
/// ///
/// {@template flutter.cupertino.CupertinoApp.defaultSelectionStyle}
/// The [CupertinoApp] automatically creates a [DefaultSelectionStyle] with
/// selectionColor sets to [CupertinoThemeData.primaryColor] with 0.2 opacity
/// and cursorColor sets to [CupertinoThemeData.primaryColor].
/// {@endtemplate}
///
/// Use this widget with caution on Android since it may produce behaviors /// Use this widget with caution on Android since it may produce behaviors
/// Android users are not expecting such as: /// Android users are not expecting such as:
/// ///
...@@ -590,14 +584,10 @@ class _CupertinoAppState extends State<CupertinoApp> { ...@@ -590,14 +584,10 @@ class _CupertinoAppState extends State<CupertinoApp> {
data: CupertinoUserInterfaceLevelData.base, data: CupertinoUserInterfaceLevelData.base,
child: CupertinoTheme( child: CupertinoTheme(
data: effectiveThemeData, data: effectiveThemeData,
child: DefaultSelectionStyle( child: HeroControllerScope(
selectionColor: effectiveThemeData.primaryColor.withOpacity(0.2), controller: _heroController,
cursorColor: effectiveThemeData.primaryColor, child: Builder(
child: HeroControllerScope( builder: _buildWidgetApp,
controller: _heroController,
child: Builder(
builder: _buildWidgetApp,
),
), ),
), ),
), ),
......
...@@ -710,8 +710,7 @@ class CupertinoTextField extends StatefulWidget { ...@@ -710,8 +710,7 @@ class CupertinoTextField extends StatefulWidget {
/// The color to use when painting the cursor. /// The color to use when painting the cursor.
/// ///
/// Defaults to the [DefaultSelectionStyle.cursorColor]. If that color is /// Defaults to the [CupertinoThemeData.primaryColor] of the ambient theme,
/// null, it uses the [CupertinoThemeData.primaryColor] of the ambient theme,
/// which itself defaults to [CupertinoColors.activeBlue] in the light theme /// which itself defaults to [CupertinoColors.activeBlue] in the light theme
/// and [CupertinoColors.activeOrange] in the dark theme. /// and [CupertinoColors.activeOrange] in the dark theme.
final Color? cursorColor; final Color? cursorColor;
...@@ -1179,11 +1178,7 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> with Restoratio ...@@ -1179,11 +1178,7 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> with Restoratio
final TextStyle placeholderStyle = textStyle.merge(resolvedPlaceholderStyle); final TextStyle placeholderStyle = textStyle.merge(resolvedPlaceholderStyle);
final Brightness keyboardAppearance = widget.keyboardAppearance ?? CupertinoTheme.brightnessOf(context); final Brightness keyboardAppearance = widget.keyboardAppearance ?? CupertinoTheme.brightnessOf(context);
final Color cursorColor = CupertinoDynamicColor.maybeResolve( final Color cursorColor = CupertinoDynamicColor.maybeResolve(widget.cursorColor, context) ?? themeData.primaryColor;
widget.cursorColor ?? DefaultSelectionStyle.of(context).cursorColor,
context,
) ?? themeData.primaryColor;
final Color disabledColor = CupertinoDynamicColor.resolve(_kDisabledBackground, context); final Color disabledColor = CupertinoDynamicColor.resolve(_kDisabledBackground, context);
final Color? decorationColor = CupertinoDynamicColor.maybeResolve(widget.decoration?.color, context); final Color? decorationColor = CupertinoDynamicColor.maybeResolve(widget.decoration?.color, context);
...@@ -1211,10 +1206,7 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> with Restoratio ...@@ -1211,10 +1206,7 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> with Restoratio
color: enabled ? decorationColor : disabledColor, color: enabled ? decorationColor : disabledColor,
); );
final Color selectionColor = CupertinoDynamicColor.maybeResolve( final Color selectionColor = CupertinoTheme.of(context).primaryColor.withOpacity(0.2);
DefaultSelectionStyle.of(context).selectionColor,
context,
) ?? CupertinoTheme.of(context).primaryColor.withOpacity(0.2);
final Widget paddedEditable = Padding( final Widget paddedEditable = Padding(
padding: widget.padding, padding: widget.padding,
......
...@@ -82,14 +82,6 @@ enum ThemeMode { ...@@ -82,14 +82,6 @@ enum ThemeMode {
/// This widget also configures the observer of the top-level [Navigator] (if /// This widget also configures the observer of the top-level [Navigator] (if
/// any) to perform [Hero] animations. /// any) to perform [Hero] animations.
/// ///
/// {@template flutter.material.MaterialApp.defaultSelectionStyle}
/// The [MaterialApp] automatically creates a [DefaultSelectionStyle]. It uses
/// the colors in the [ThemeData.textSelectionTheme] if they are not null;
/// otherwise, the [MaterialApp] sets [DefaultSelectionStyle.selectionColor] to
/// [ColorScheme.primary] with 0.4 opacity and
/// [DefaultSelectionStyle.cursorColor] to [ColorScheme.primary].
/// {@endtemplate}
///
/// If [home], [routes], [onGenerateRoute], and [onUnknownRoute] are all null, /// If [home], [routes], [onGenerateRoute], and [onUnknownRoute] are all null,
/// and [builder] is not null, then no [Navigator] is created. /// and [builder] is not null, then no [Navigator] is created.
/// ///
...@@ -879,35 +871,29 @@ class _MaterialAppState extends State<MaterialApp> { ...@@ -879,35 +871,29 @@ class _MaterialAppState extends State<MaterialApp> {
theme = widget.highContrastTheme; theme = widget.highContrastTheme;
} }
theme ??= widget.theme ?? ThemeData.light(); theme ??= widget.theme ?? ThemeData.light();
final Color effectiveSelectionColor = theme.textSelectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
final Color effectiveCursorColor = theme.textSelectionTheme.cursorColor ?? theme.colorScheme.primary;
return ScaffoldMessenger( return ScaffoldMessenger(
key: widget.scaffoldMessengerKey, key: widget.scaffoldMessengerKey,
child: DefaultSelectionStyle( child: AnimatedTheme(
selectionColor: effectiveSelectionColor, data: theme,
cursorColor: effectiveCursorColor, child: widget.builder != null
child: AnimatedTheme( ? Builder(
data: theme, builder: (BuildContext context) {
child: widget.builder != null // Why are we surrounding a builder with a builder?
? Builder( //
builder: (BuildContext context) { // The widget.builder may contain code that invokes
// Why are we surrounding a builder with a builder? // Theme.of(), which should return the theme we selected
// // above in AnimatedTheme. However, if we invoke
// The widget.builder may contain code that invokes // widget.builder() directly as the child of AnimatedTheme
// Theme.of(), which should return the theme we selected // then there is no Context separating them, and the
// above in AnimatedTheme. However, if we invoke // widget.builder() will not find the theme. Therefore, we
// widget.builder() directly as the child of AnimatedTheme // surround widget.builder with yet another builder so that
// then there is no Context separating them, and the // a context separates them and Theme.of() correctly
// widget.builder() will not find the theme. Therefore, we // resolves to the theme we passed to AnimatedTheme.
// surround widget.builder with yet another builder so that return widget.builder!(context, child);
// a context separates them and Theme.of() correctly },
// resolves to the theme we passed to AnimatedTheme. )
return widget.builder!(context, child); : child ?? const SizedBox.shrink(),
},
)
: child ?? const SizedBox.shrink(),
),
), ),
); );
} }
......
...@@ -12,6 +12,7 @@ import 'package:flutter/rendering.dart'; ...@@ -12,6 +12,7 @@ import 'package:flutter/rendering.dart';
import 'desktop_text_selection.dart'; import 'desktop_text_selection.dart';
import 'feedback.dart'; import 'feedback.dart';
import 'text_selection.dart'; import 'text_selection.dart';
import 'text_selection_theme.dart';
import 'theme.dart'; import 'theme.dart';
/// An eyeballed value that moves the cursor slightly left of where it is /// An eyeballed value that moves the cursor slightly left of where it is
...@@ -356,14 +357,9 @@ class SelectableText extends StatefulWidget { ...@@ -356,14 +357,9 @@ class SelectableText extends StatefulWidget {
/// {@macro flutter.widgets.editableText.cursorRadius} /// {@macro flutter.widgets.editableText.cursorRadius}
final Radius? cursorRadius; final Radius? cursorRadius;
/// The color of the cursor. /// The color to use when painting the cursor.
/// ///
/// The cursor indicates the current text insertion point. /// Defaults to the theme's `cursorColor` when null.
///
/// If null then [DefaultSelectionStyle.cursorColor] is used. If that is also
/// null and [ThemeData.platform] is [TargetPlatform.iOS] or
/// [TargetPlatform.macOS], then [CupertinoThemeData.primaryColor] is used.
/// Otherwise [ColorScheme.primary] of [ThemeData.colorScheme] is used.
final Color? cursorColor; final Color? cursorColor;
/// Controls how tall the selection highlight boxes are computed to be. /// Controls how tall the selection highlight boxes are computed to be.
...@@ -599,14 +595,14 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio ...@@ -599,14 +595,14 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio
); );
final ThemeData theme = Theme.of(context); final ThemeData theme = Theme.of(context);
final DefaultSelectionStyle selectionStyle = DefaultSelectionStyle.of(context); final TextSelectionThemeData selectionTheme = TextSelectionTheme.of(context);
final FocusNode focusNode = _effectiveFocusNode; final FocusNode focusNode = _effectiveFocusNode;
TextSelectionControls? textSelectionControls = widget.selectionControls; TextSelectionControls? textSelectionControls = widget.selectionControls;
final bool paintCursorAboveText; final bool paintCursorAboveText;
final bool cursorOpacityAnimates; final bool cursorOpacityAnimates;
Offset? cursorOffset; Offset? cursorOffset;
final Color cursorColor; Color? cursorColor = widget.cursorColor;
final Color selectionColor; final Color selectionColor;
Radius? cursorRadius = widget.cursorRadius; Radius? cursorRadius = widget.cursorRadius;
...@@ -617,8 +613,8 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio ...@@ -617,8 +613,8 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio
textSelectionControls ??= cupertinoTextSelectionControls; textSelectionControls ??= cupertinoTextSelectionControls;
paintCursorAboveText = true; paintCursorAboveText = true;
cursorOpacityAnimates = true; cursorOpacityAnimates = true;
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? cupertinoTheme.primaryColor; cursorColor ??= selectionTheme.cursorColor ?? cupertinoTheme.primaryColor;
selectionColor = selectionStyle.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40); selectionColor = selectionTheme.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40);
cursorRadius ??= const Radius.circular(2.0); cursorRadius ??= const Radius.circular(2.0);
cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0); cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0);
break; break;
...@@ -629,8 +625,8 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio ...@@ -629,8 +625,8 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio
textSelectionControls ??= cupertinoDesktopTextSelectionControls; textSelectionControls ??= cupertinoDesktopTextSelectionControls;
paintCursorAboveText = true; paintCursorAboveText = true;
cursorOpacityAnimates = true; cursorOpacityAnimates = true;
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? cupertinoTheme.primaryColor; cursorColor ??= selectionTheme.cursorColor ?? cupertinoTheme.primaryColor;
selectionColor = selectionStyle.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40); selectionColor = selectionTheme.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40);
cursorRadius ??= const Radius.circular(2.0); cursorRadius ??= const Radius.circular(2.0);
cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0); cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0);
break; break;
...@@ -641,8 +637,8 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio ...@@ -641,8 +637,8 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio
textSelectionControls ??= materialTextSelectionControls; textSelectionControls ??= materialTextSelectionControls;
paintCursorAboveText = false; paintCursorAboveText = false;
cursorOpacityAnimates = false; cursorOpacityAnimates = false;
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? theme.colorScheme.primary; cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;
selectionColor = selectionStyle.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40); selectionColor = selectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
break; break;
case TargetPlatform.linux: case TargetPlatform.linux:
...@@ -651,8 +647,8 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio ...@@ -651,8 +647,8 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio
textSelectionControls ??= desktopTextSelectionControls; textSelectionControls ??= desktopTextSelectionControls;
paintCursorAboveText = false; paintCursorAboveText = false;
cursorOpacityAnimates = false; cursorOpacityAnimates = false;
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? theme.colorScheme.primary; cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;
selectionColor = selectionStyle.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40); selectionColor = selectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
break; break;
} }
......
...@@ -19,6 +19,7 @@ import 'material_localizations.dart'; ...@@ -19,6 +19,7 @@ import 'material_localizations.dart';
import 'material_state.dart'; import 'material_state.dart';
import 'selectable_text.dart' show iOSHorizontalOffset; import 'selectable_text.dart' show iOSHorizontalOffset;
import 'text_selection.dart'; import 'text_selection.dart';
import 'text_selection_theme.dart';
import 'theme.dart'; import 'theme.dart';
export 'package:flutter/services.dart' show TextInputType, TextInputAction, TextCapitalization, SmartQuotesType, SmartDashesType; export 'package:flutter/services.dart' show TextInputType, TextInputAction, TextCapitalization, SmartQuotesType, SmartDashesType;
...@@ -613,7 +614,7 @@ class TextField extends StatefulWidget { ...@@ -613,7 +614,7 @@ class TextField extends StatefulWidget {
/// the field. /// the field.
/// ///
/// If this is null it will default to the ambient /// If this is null it will default to the ambient
/// [DefaultSelectionStyle.cursorColor]. If that is null, and the /// [TextSelectionThemeData.cursorColor]. If that is null, and the
/// [ThemeData.platform] is [TargetPlatform.iOS] or [TargetPlatform.macOS] /// [ThemeData.platform] is [TargetPlatform.iOS] or [TargetPlatform.macOS]
/// it will use [CupertinoThemeData.primaryColor]. Otherwise it will use /// it will use [CupertinoThemeData.primaryColor]. Otherwise it will use
/// the value of [ColorScheme.primary] of [ThemeData.colorScheme]. /// the value of [ColorScheme.primary] of [ThemeData.colorScheme].
...@@ -1115,7 +1116,7 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements ...@@ -1115,7 +1116,7 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
); );
final ThemeData theme = Theme.of(context); final ThemeData theme = Theme.of(context);
final DefaultSelectionStyle selectionStyle = DefaultSelectionStyle.of(context); final TextSelectionThemeData selectionTheme = TextSelectionTheme.of(context);
final TextStyle style = theme.textTheme.subtitle1!.merge(widget.style); final TextStyle style = theme.textTheme.subtitle1!.merge(widget.style);
final Brightness keyboardAppearance = widget.keyboardAppearance ?? theme.brightness; final Brightness keyboardAppearance = widget.keyboardAppearance ?? theme.brightness;
final TextEditingController controller = _effectiveController; final TextEditingController controller = _effectiveController;
...@@ -1133,7 +1134,7 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements ...@@ -1133,7 +1134,7 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
final bool paintCursorAboveText; final bool paintCursorAboveText;
final bool cursorOpacityAnimates; final bool cursorOpacityAnimates;
Offset? cursorOffset; Offset? cursorOffset;
final Color cursorColor; Color? cursorColor = widget.cursorColor;
final Color selectionColor; final Color selectionColor;
Color? autocorrectionTextRectColor; Color? autocorrectionTextRectColor;
Radius? cursorRadius = widget.cursorRadius; Radius? cursorRadius = widget.cursorRadius;
...@@ -1146,8 +1147,8 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements ...@@ -1146,8 +1147,8 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
textSelectionControls ??= cupertinoTextSelectionControls; textSelectionControls ??= cupertinoTextSelectionControls;
paintCursorAboveText = true; paintCursorAboveText = true;
cursorOpacityAnimates = true; cursorOpacityAnimates = true;
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? cupertinoTheme.primaryColor; cursorColor ??= selectionTheme.cursorColor ?? cupertinoTheme.primaryColor;
selectionColor = selectionStyle.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40); selectionColor = selectionTheme.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40);
cursorRadius ??= const Radius.circular(2.0); cursorRadius ??= const Radius.circular(2.0);
cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0); cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0);
autocorrectionTextRectColor = selectionColor; autocorrectionTextRectColor = selectionColor;
...@@ -1159,8 +1160,8 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements ...@@ -1159,8 +1160,8 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
textSelectionControls ??= cupertinoDesktopTextSelectionControls; textSelectionControls ??= cupertinoDesktopTextSelectionControls;
paintCursorAboveText = true; paintCursorAboveText = true;
cursorOpacityAnimates = true; cursorOpacityAnimates = true;
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? cupertinoTheme.primaryColor; cursorColor ??= selectionTheme.cursorColor ?? cupertinoTheme.primaryColor;
selectionColor = selectionStyle.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40); selectionColor = selectionTheme.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40);
cursorRadius ??= const Radius.circular(2.0); cursorRadius ??= const Radius.circular(2.0);
cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0); cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0);
handleDidGainAccessibilityFocus = () { handleDidGainAccessibilityFocus = () {
...@@ -1177,8 +1178,8 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements ...@@ -1177,8 +1178,8 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
textSelectionControls ??= materialTextSelectionControls; textSelectionControls ??= materialTextSelectionControls;
paintCursorAboveText = false; paintCursorAboveText = false;
cursorOpacityAnimates = false; cursorOpacityAnimates = false;
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? theme.colorScheme.primary; cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;
selectionColor = selectionStyle.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40); selectionColor = selectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
break; break;
case TargetPlatform.linux: case TargetPlatform.linux:
...@@ -1186,8 +1187,8 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements ...@@ -1186,8 +1187,8 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
textSelectionControls ??= desktopTextSelectionControls; textSelectionControls ??= desktopTextSelectionControls;
paintCursorAboveText = false; paintCursorAboveText = false;
cursorOpacityAnimates = false; cursorOpacityAnimates = false;
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? theme.colorScheme.primary; cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;
selectionColor = selectionStyle.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40); selectionColor = selectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
break; break;
case TargetPlatform.windows: case TargetPlatform.windows:
...@@ -1195,8 +1196,8 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements ...@@ -1195,8 +1196,8 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
textSelectionControls ??= desktopTextSelectionControls; textSelectionControls ??= desktopTextSelectionControls;
paintCursorAboveText = false; paintCursorAboveText = false;
cursorOpacityAnimates = false; cursorOpacityAnimates = false;
cursorColor = widget.cursorColor ?? selectionStyle.cursorColor ?? theme.colorScheme.primary; cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;
selectionColor = selectionStyle.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40); selectionColor = selectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
handleDidGainAccessibilityFocus = () { handleDidGainAccessibilityFocus = () {
// Automatically activate the TextField when it receives accessibility focus. // Automatically activate the TextField when it receives accessibility focus.
if (!_effectiveFocusNode.hasFocus && _effectiveFocusNode.canRequestFocus) { if (!_effectiveFocusNode.hasFocus && _effectiveFocusNode.canRequestFocus) {
......
...@@ -129,9 +129,6 @@ class TextSelectionThemeData with Diagnosticable { ...@@ -129,9 +129,6 @@ class TextSelectionThemeData with Diagnosticable {
/// ) /// )
/// ``` /// ```
/// {@end-tool} /// {@end-tool}
///
/// This widget also creates a [DefaultSelectionStyle] for its subtree with
/// [data].
class TextSelectionTheme extends InheritedTheme { class TextSelectionTheme extends InheritedTheme {
/// Creates a text selection theme widget that specifies the text /// Creates a text selection theme widget that specifies the text
/// selection properties for all widgets below it in the widget tree. /// selection properties for all widgets below it in the widget tree.
...@@ -140,29 +137,12 @@ class TextSelectionTheme extends InheritedTheme { ...@@ -140,29 +137,12 @@ class TextSelectionTheme extends InheritedTheme {
const TextSelectionTheme({ const TextSelectionTheme({
super.key, super.key,
required this.data, required this.data,
required Widget child, required super.child,
}) : assert(data != null), }) : assert(data != null);
_child = child,
// See `get child` override below.
super(child: const _NullWidget());
/// The properties for descendant [TextField] and [SelectableText] widgets. /// The properties for descendant [TextField] and [SelectableText] widgets.
final TextSelectionThemeData data; final TextSelectionThemeData data;
// Overriding the getter to insert `DefaultSelectionStyle` into the subtree
// without breaking API. In general, this approach should be avoided
// because it relies on an implementation detail of ProxyWidget. This
// workaround is necessary because TextSelectionTheme is const.
@override
Widget get child {
return DefaultSelectionStyle(
selectionColor: data.selectionColor,
cursorColor: data.cursorColor,
child: _child,
);
}
final Widget _child;
/// Returns the [data] from the closest [TextSelectionTheme] ancestor. If /// Returns the [data] from the closest [TextSelectionTheme] ancestor. If
/// there is no ancestor, it returns [ThemeData.textSelectionTheme]. /// there is no ancestor, it returns [ThemeData.textSelectionTheme].
/// Applications can assume that the returned value will not be null. /// Applications can assume that the returned value will not be null.
...@@ -185,10 +165,3 @@ class TextSelectionTheme extends InheritedTheme { ...@@ -185,10 +165,3 @@ class TextSelectionTheme extends InheritedTheme {
@override @override
bool updateShouldNotify(TextSelectionTheme oldWidget) => data != oldWidget.data; bool updateShouldNotify(TextSelectionTheme oldWidget) => data != oldWidget.data;
} }
class _NullWidget extends Widget {
const _NullWidget();
@override
Element createElement() => throw UnimplementedError();
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:ui';
import 'framework.dart';
import 'inherited_theme.dart';
/// The selection style to apply to descendant [EditableText] widgets which
/// don't have an explicit style.
///
/// {@macro flutter.cupertino.CupertinoApp.defaultSelectionStyle}
///
/// {@macro flutter.material.MaterialApp.defaultSelectionStyle}
///
/// See also:
/// * [TextSelectionTheme]: which also creates a [DefaultSelectionStyle] for
/// the subtree.
class DefaultSelectionStyle extends InheritedTheme {
/// Creates a default selection style widget that specifies the selection
/// properties for all widgets below it in the widget tree.
const DefaultSelectionStyle({
super.key,
this.cursorColor,
this.selectionColor,
required super.child,
});
/// A const-constructable default selection style that provides fallback
/// values.
///
/// Returned from [of] when the given [BuildContext] doesn't have an enclosing
/// default selection style.
///
/// This constructor creates a [DefaultTextStyle] with an invalid [child],
/// which means the constructed value cannot be incorporated into the tree.
const DefaultSelectionStyle.fallback({ super.key })
: cursorColor = null,
selectionColor = null,
super(child: const _NullWidget());
/// The color of the text field's cursor.
///
/// The cursor indicates the current location of the text insertion point in
/// the field.
final Color? cursorColor;
/// The background color of selected text.
final Color? selectionColor;
/// The closest instance of this class that encloses the given context.
///
/// If no such instance exists, returns an instance created by
/// [DefaultSelectionStyle.fallback], which contains fallback values.
///
/// Typical usage is as follows:
///
/// ```dart
/// DefaultSelectionStyle style = DefaultSelectionStyle.of(context);
/// ```
static DefaultSelectionStyle of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<DefaultSelectionStyle>() ?? const DefaultSelectionStyle.fallback();
}
@override
Widget wrap(BuildContext context, Widget child) {
return DefaultSelectionStyle(
cursorColor: cursorColor,
selectionColor: selectionColor,
child: child
);
}
@override
bool updateShouldNotify(DefaultSelectionStyle oldWidget) {
return cursorColor != oldWidget.cursorColor ||
selectionColor != oldWidget.selectionColor;
}
}
class _NullWidget extends StatelessWidget {
const _NullWidget();
@override
Widget build(BuildContext context) {
throw FlutterError(
'A DefaultTextStyle constructed with DefaultTextStyle.fallback cannot be incorporated into the widget tree, '
'it is meant only to provide a fallback value returned by DefaultTextStyle.of() '
'when no enclosing default text style is present in a BuildContext.',
);
}
}
...@@ -20,7 +20,6 @@ import 'basic.dart'; ...@@ -20,7 +20,6 @@ import 'basic.dart';
import 'binding.dart'; import 'binding.dart';
import 'constants.dart'; import 'constants.dart';
import 'debug.dart'; import 'debug.dart';
import 'default_selection_style.dart';
import 'focus_manager.dart'; import 'focus_manager.dart';
import 'focus_scope.dart'; import 'focus_scope.dart';
import 'focus_traversal.dart'; import 'focus_traversal.dart';
...@@ -961,9 +960,6 @@ class EditableText extends StatefulWidget { ...@@ -961,9 +960,6 @@ class EditableText extends StatefulWidget {
/// The color to use when painting the selection. /// The color to use when painting the selection.
/// ///
/// If this property is null, this widget gets the selection color from the
/// [DefaultSelectionStyle].
///
/// For [CupertinoTextField]s, the value is set to the ambient /// For [CupertinoTextField]s, the value is set to the ambient
/// [CupertinoThemeData.primaryColor] with 20% opacity. For [TextField]s, the /// [CupertinoThemeData.primaryColor] with 20% opacity. For [TextField]s, the
/// value is set to the ambient [TextSelectionThemeData.selectionColor]. /// value is set to the ambient [TextSelectionThemeData.selectionColor].
...@@ -3253,8 +3249,6 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -3253,8 +3249,6 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
assert(debugCheckHasMediaQuery(context)); assert(debugCheckHasMediaQuery(context));
super.build(context); // See AutomaticKeepAliveClientMixin. super.build(context); // See AutomaticKeepAliveClientMixin.
final Color? effectiveSelectionColor = widget.selectionColor ?? DefaultSelectionStyle.of(context).selectionColor;
final TextSelectionControls? controls = widget.selectionControls; final TextSelectionControls? controls = widget.selectionControls;
return MouseRegion( return MouseRegion(
cursor: widget.mouseCursor ?? SystemMouseCursors.text, cursor: widget.mouseCursor ?? SystemMouseCursors.text,
...@@ -3316,7 +3310,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -3316,7 +3310,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
minLines: widget.minLines, minLines: widget.minLines,
expands: widget.expands, expands: widget.expands,
strutStyle: widget.strutStyle, strutStyle: widget.strutStyle,
selectionColor: effectiveSelectionColor, selectionColor: widget.selectionColor,
textScaleFactor: widget.textScaleFactor ?? MediaQuery.textScaleFactorOf(context), textScaleFactor: widget.textScaleFactor ?? MediaQuery.textScaleFactorOf(context),
textAlign: widget.textAlign, textAlign: widget.textAlign,
textDirection: _textDirection, textDirection: _textDirection,
......
...@@ -34,7 +34,6 @@ export 'src/widgets/bottom_navigation_bar_item.dart'; ...@@ -34,7 +34,6 @@ export 'src/widgets/bottom_navigation_bar_item.dart';
export 'src/widgets/color_filter.dart'; export 'src/widgets/color_filter.dart';
export 'src/widgets/container.dart'; export 'src/widgets/container.dart';
export 'src/widgets/debug.dart'; export 'src/widgets/debug.dart';
export 'src/widgets/default_selection_style.dart';
export 'src/widgets/default_text_editing_shortcuts.dart'; export 'src/widgets/default_text_editing_shortcuts.dart';
export 'src/widgets/desktop_text_selection_toolbar_layout_delegate.dart'; export 'src/widgets/desktop_text_selection_toolbar_layout_delegate.dart';
export 'src/widgets/dismissible.dart'; export 'src/widgets/dismissible.dart';
......
...@@ -17,7 +17,6 @@ import 'dart:ui' as ui show BoxHeightStyle, BoxWidthStyle, Color; ...@@ -17,7 +17,6 @@ import 'dart:ui' as ui show BoxHeightStyle, BoxWidthStyle, Color;
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart' show DragStartBehavior, PointerDeviceKind, kSecondaryMouseButton, kDoubleTapTimeout; import 'package:flutter/gestures.dart' show DragStartBehavior, PointerDeviceKind, kSecondaryMouseButton, kDoubleTapTimeout;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
...@@ -407,32 +406,6 @@ void main() { ...@@ -407,32 +406,6 @@ void main() {
}, },
); );
testWidgets(
'uses DefaultSelectionStyle for selection and cursor colors if provided',
(WidgetTester tester) async {
const Color selectionColor = Colors.black;
const Color cursorColor = Colors.white;
await tester.pumpWidget(
const CupertinoApp(
home: Center(
child: DefaultSelectionStyle(
selectionColor: selectionColor,
cursorColor: cursorColor,
child: CupertinoTextField(
autofocus: true,
)
),
),
),
);
await tester.pump();
final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText));
expect(state.widget.selectionColor, selectionColor);
expect(state.widget.cursorColor, cursorColor);
},
);
testWidgets( testWidgets(
'multi-lined text fields are intrinsically taller no-strut', 'multi-lined text fields are intrinsically taller no-strut',
(WidgetTester tester) async { (WidgetTester tester) async {
......
...@@ -170,7 +170,6 @@ void main() { ...@@ -170,7 +170,6 @@ void main() {
' _InheritedTheme\n' ' _InheritedTheme\n'
' Theme\n' ' Theme\n'
' AnimatedTheme\n' ' AnimatedTheme\n'
' DefaultSelectionStyle\n'
' _ScaffoldMessengerScope\n' ' _ScaffoldMessengerScope\n'
' ScaffoldMessenger\n' ' ScaffoldMessenger\n'
' Builder\n' ' Builder\n'
......
...@@ -286,27 +286,6 @@ void main() { ...@@ -286,27 +286,6 @@ void main() {
skip: isContextMenuProvidedByPlatform, // [intended] only applies to platforms where we supply the context menu. skip: isContextMenuProvidedByPlatform, // [intended] only applies to platforms where we supply the context menu.
); );
testWidgets('uses DefaultSelectionStyle for selection and cursor colors if provided', (WidgetTester tester) async {
const Color selectionColor = Colors.orange;
const Color cursorColor = Colors.red;
await tester.pumpWidget(
const MaterialApp(
home: Material(
child: DefaultSelectionStyle(
selectionColor: selectionColor,
cursorColor: cursorColor,
child: TextField(autofocus: true),
),
),
),
);
await tester.pump();
final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText));
expect(state.widget.selectionColor, selectionColor);
expect(state.widget.cursorColor, cursorColor);
});
testWidgets('Activates the text field when receives semantics focus on Mac, Windows', (WidgetTester tester) async { testWidgets('Activates the text field when receives semantics focus on Mac, Windows', (WidgetTester tester) async {
final SemanticsTester semantics = SemanticsTester(tester); final SemanticsTester semantics = SemanticsTester(tester);
final SemanticsOwner semanticsOwner = tester.binding.pipelineOwner.semanticsOwner!; final SemanticsOwner semanticsOwner = tester.binding.pipelineOwner.semanticsOwner!;
......
...@@ -262,43 +262,4 @@ void main() { ...@@ -262,43 +262,4 @@ void main() {
final RenderEditable renderSelectable = selectableTextState.renderEditable; final RenderEditable renderSelectable = selectableTextState.renderEditable;
expect(renderSelectable.cursorColor, cursorColor.withAlpha(0)); expect(renderSelectable.cursorColor, cursorColor.withAlpha(0));
}); });
testWidgets('TextSelectionThem overrides DefaultSelectionStyle', (WidgetTester tester) async {
const Color themeSelectionColor = Color(0xffaabbcc);
const Color themeCursorColor = Color(0x00ccbbaa);
const Color defaultSelectionColor = Color(0xffaa1111);
const Color defaultCursorColor = Color(0x00cc2222);
final Key defaultSelectionStyle = UniqueKey();
final Key themeStyle = UniqueKey();
// Test TextField's cursor color.
await tester.pumpWidget(
MaterialApp(
home: DefaultSelectionStyle(
selectionColor: defaultSelectionColor,
cursorColor: defaultCursorColor,
child: Container(
key: defaultSelectionStyle,
child: TextSelectionTheme(
data: const TextSelectionThemeData(
selectionColor: themeSelectionColor,
cursorColor: themeCursorColor,
),
child: Placeholder(
key: themeStyle,
),
),
)
),
),
);
final BuildContext defaultSelectionStyleContext = tester.element(find.byKey(defaultSelectionStyle));
DefaultSelectionStyle style = DefaultSelectionStyle.of(defaultSelectionStyleContext);
expect(style.selectionColor, defaultSelectionColor);
expect(style.cursorColor, defaultCursorColor);
final BuildContext themeStyleContext = tester.element(find.byKey(themeStyle));
style = DefaultSelectionStyle.of(themeStyleContext);
expect(style.selectionColor, themeSelectionColor);
expect(style.cursorColor, themeCursorColor);
});
} }
...@@ -660,36 +660,6 @@ void main() { ...@@ -660,36 +660,6 @@ void main() {
expect(focusNode.hasFocus, isFalse); expect(focusNode.hasFocus, isFalse);
}); });
testWidgets('use DefaultSelectionStyle for selection color', (WidgetTester tester) async {
const TextEditingValue value = TextEditingValue(
text: 'test test',
selection: TextSelection(affinity: TextAffinity.upstream, baseOffset: 5, extentOffset: 7),
);
const Color selectionColor = Colors.orange;
controller.value = value;
await tester.pumpWidget(
DefaultSelectionStyle(
selectionColor: selectionColor,
child: MediaQuery(
data: const MediaQueryData(),
child: Directionality(
textDirection: TextDirection.ltr,
child: EditableText(
controller: controller,
backgroundCursorColor: Colors.grey,
focusNode: focusNode,
keyboardType: TextInputType.multiline,
style: textStyle,
cursorColor: cursorColor,
),
),
)
),
);
final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText));
expect(state.renderEditable.selectionColor, selectionColor);
});
testWidgets('visiblePassword keyboard is requested when set explicitly', (WidgetTester tester) async { testWidgets('visiblePassword keyboard is requested when set explicitly', (WidgetTester tester) async {
await tester.pumpWidget( await tester.pumpWidget(
MediaQuery( MediaQuery(
......
...@@ -297,27 +297,6 @@ void main() { ...@@ -297,27 +297,6 @@ void main() {
expect(tester.testTextInput.hasAnyClients, false); expect(tester.testTextInput.hasAnyClients, false);
}); });
testWidgets('uses DefaultSelectionStyle for selection and cursor colors if provided', (WidgetTester tester) async {
const Color selectionColor = Colors.orange;
const Color cursorColor = Colors.red;
await tester.pumpWidget(
const MaterialApp(
home: Material(
child: DefaultSelectionStyle(
selectionColor: selectionColor,
cursorColor: cursorColor,
child: SelectableText('text'),
),
),
),
);
await tester.pump();
final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText));
expect(state.widget.selectionColor, selectionColor);
expect(state.widget.cursorColor, cursorColor);
});
testWidgets('Selectable Text has adaptive size', (WidgetTester tester) async { testWidgets('Selectable Text has adaptive size', (WidgetTester tester) async {
await tester.pumpWidget( await tester.pumpWidget(
boilerplate( boilerplate(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment