Material Desktop Context Menu (#74286)

Very simple right-click context menu for Windows and Linux in a Material-esque style.
This commit is contained in:
Justin McCandless 2021-01-20 16:29:01 -08:00 committed by GitHub
parent aed4518569
commit a8471a61f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 498 additions and 54 deletions

View file

@ -57,6 +57,7 @@ export 'src/material/date.dart';
export 'src/material/date_picker.dart'; export 'src/material/date_picker.dart';
export 'src/material/date_picker_deprecated.dart'; export 'src/material/date_picker_deprecated.dart';
export 'src/material/debug.dart'; export 'src/material/debug.dart';
export 'src/material/desktop_text_selection.dart';
export 'src/material/dialog.dart'; export 'src/material/dialog.dart';
export 'src/material/dialog_theme.dart'; export 'src/material/dialog_theme.dart';
export 'src/material/divider.dart'; export 'src/material/divider.dart';

View file

@ -307,7 +307,7 @@ class _CupertinoDesktopTextSelectionToolbar extends StatelessWidget {
_kToolbarScreenPadding, _kToolbarScreenPadding,
), ),
child: CustomSingleChildLayout( child: CustomSingleChildLayout(
delegate: _DesktopTextSelectionToolbarLayoutDelegate( delegate: DesktopTextSelectionToolbarLayoutDelegate(
anchor: anchor - localAdjustment, anchor: anchor - localAdjustment,
), ),
child: toolbarBuilder(context, Column( child: toolbarBuilder(context, Column(
@ -319,48 +319,6 @@ class _CupertinoDesktopTextSelectionToolbar extends StatelessWidget {
} }
} }
// Positions the toolbar at [anchor] if it fits, otherwise moves it so that it
// just fits fully on-screen.
//
// See also:
//
// * [CupertinoDesktopTextSelectionToolbar], which uses this to position itself.
// * [TextSelectionToolbarLayoutDelegate], which does a similar layout for
// the mobile text selection toolbars.
class _DesktopTextSelectionToolbarLayoutDelegate extends SingleChildLayoutDelegate {
/// Creates an instance of TextSelectionToolbarLayoutDelegate.
_DesktopTextSelectionToolbarLayoutDelegate({
required this.anchor,
});
/// The point at which to render the menu, if possible.
///
/// Should be provided in local coordinates.
final Offset anchor;
@override
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
return constraints.loosen();
}
@override
Offset getPositionForChild(Size size, Size childSize) {
final Offset overhang = Offset(
anchor.dx + childSize.width - size.width,
anchor.dy + childSize.height - size.height,
);
return Offset(
overhang.dx > 0.0 ? anchor.dx - overhang.dx : anchor.dx,
overhang.dy > 0.0 ? anchor.dy - overhang.dy : anchor.dy,
);
}
@override
bool shouldRelayout(_DesktopTextSelectionToolbarLayoutDelegate oldDelegate) {
return anchor != oldDelegate.anchor;
}
}
// These values were measured from a screenshot of TextEdit on MacOS 10.15.7 on // These values were measured from a screenshot of TextEdit on MacOS 10.15.7 on
// a Macbook Pro. // a Macbook Pro.
const TextStyle _kToolbarButtonFontStyle = TextStyle( const TextStyle _kToolbarButtonFontStyle = TextStyle(

View file

@ -0,0 +1,372 @@
// 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 'package:flutter/gestures.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'colors.dart';
import 'constants.dart';
import 'debug.dart';
import 'material.dart';
import 'material_localizations.dart';
import 'text_button.dart';
import 'text_selection_toolbar.dart';
import 'theme.dart';
const double _kToolbarScreenPadding = 8.0;
const double _kToolbarWidth = 222.0;
class _DesktopTextSelectionControls extends TextSelectionControls {
/// Desktop has no text selection handles.
@override
Size getHandleSize(double textLineHeight) {
return Size.zero;
}
/// Builder for the Material-style desktop copy/paste text selection toolbar.
@override
Widget buildToolbar(
BuildContext context,
Rect globalEditableRegion,
double textLineHeight,
Offset selectionMidpoint,
List<TextSelectionPoint> endpoints,
TextSelectionDelegate delegate,
ClipboardStatusNotifier clipboardStatus,
Offset? lastSecondaryTapDownPosition,
) {
return _DesktopTextSelectionControlsToolbar(
clipboardStatus: clipboardStatus,
endpoints: endpoints,
globalEditableRegion: globalEditableRegion,
handleCut: canCut(delegate) ? () => handleCut(delegate) : null,
handleCopy: canCopy(delegate) ? () => handleCopy(delegate, clipboardStatus) : null,
handlePaste: canPaste(delegate) ? () => handlePaste(delegate) : null,
handleSelectAll: canSelectAll(delegate) ? () => handleSelectAll(delegate) : null,
selectionMidpoint: selectionMidpoint,
lastSecondaryTapDownPosition: lastSecondaryTapDownPosition,
textLineHeight: textLineHeight,
);
}
/// Builds the text selection handles, but desktop has none.
@override
Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight) {
return const SizedBox.shrink();
}
/// Gets the position for the text selection handles, but desktop has none.
@override
Offset getHandleAnchor(TextSelectionHandleType type, double textLineHeight) {
return Offset.zero;
}
@override
bool canSelectAll(TextSelectionDelegate delegate) {
// Allow SelectAll when selection is not collapsed, unless everything has
// already been selected. Same behavior as Android.
final TextEditingValue value = delegate.textEditingValue;
return delegate.selectAllEnabled &&
value.text.isNotEmpty &&
!(value.selection.start == 0 && value.selection.end == value.text.length);
}
}
/// Text selection controls that loosely follows Material design conventions.
final TextSelectionControls desktopTextSelectionControls =
_DesktopTextSelectionControls();
// Generates the child that's passed into DesktopTextSelectionToolbar.
class _DesktopTextSelectionControlsToolbar extends StatefulWidget {
const _DesktopTextSelectionControlsToolbar({
Key? key,
required this.clipboardStatus,
required this.endpoints,
required this.globalEditableRegion,
required this.handleCopy,
required this.handleCut,
required this.handlePaste,
required this.handleSelectAll,
required this.selectionMidpoint,
required this.textLineHeight,
required this.lastSecondaryTapDownPosition,
}) : super(key: key);
final ClipboardStatusNotifier? clipboardStatus;
final List<TextSelectionPoint> endpoints;
final Rect globalEditableRegion;
final VoidCallback? handleCopy;
final VoidCallback? handleCut;
final VoidCallback? handlePaste;
final VoidCallback? handleSelectAll;
final Offset? lastSecondaryTapDownPosition;
final Offset selectionMidpoint;
final double textLineHeight;
@override
_DesktopTextSelectionControlsToolbarState createState() => _DesktopTextSelectionControlsToolbarState();
}
class _DesktopTextSelectionControlsToolbarState extends State<_DesktopTextSelectionControlsToolbar> {
ClipboardStatusNotifier? _clipboardStatus;
void _onChangedClipboardStatus() {
setState(() {
// Inform the widget that the value of clipboardStatus has changed.
});
}
@override
void initState() {
super.initState();
if (widget.handlePaste != null) {
_clipboardStatus = widget.clipboardStatus ?? ClipboardStatusNotifier();
_clipboardStatus!.addListener(_onChangedClipboardStatus);
_clipboardStatus!.update();
}
}
@override
void didUpdateWidget(_DesktopTextSelectionControlsToolbar oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.clipboardStatus != widget.clipboardStatus) {
if (_clipboardStatus != null) {
_clipboardStatus!.removeListener(_onChangedClipboardStatus);
_clipboardStatus!.dispose();
}
_clipboardStatus = widget.clipboardStatus ?? ClipboardStatusNotifier();
_clipboardStatus!.addListener(_onChangedClipboardStatus);
if (widget.handlePaste != null) {
_clipboardStatus!.update();
}
}
}
@override
void dispose() {
super.dispose();
// When used in an Overlay, this can be disposed after its creator has
// already disposed _clipboardStatus.
if (_clipboardStatus != null && !_clipboardStatus!.disposed) {
_clipboardStatus!.removeListener(_onChangedClipboardStatus);
if (widget.clipboardStatus == null) {
_clipboardStatus!.dispose();
}
}
}
@override
Widget build(BuildContext context) {
// Don't render the menu until the state of the clipboard is known.
if (widget.handlePaste != null && _clipboardStatus!.value == ClipboardStatus.unknown) {
return const SizedBox(width: 0.0, height: 0.0);
}
assert(debugCheckHasMediaQuery(context));
final MediaQueryData mediaQuery = MediaQuery.of(context);
final Offset midpointAnchor = Offset(
(widget.selectionMidpoint.dx - widget.globalEditableRegion.left).clamp(
mediaQuery.padding.left,
mediaQuery.size.width - mediaQuery.padding.right,
),
widget.selectionMidpoint.dy - widget.globalEditableRegion.top,
);
assert(debugCheckHasMaterialLocalizations(context));
final MaterialLocalizations localizations = MaterialLocalizations.of(context);
final List<Widget> items = <Widget>[];
void addToolbarButton(
String text,
VoidCallback onPressed,
) {
items.add(_DesktopTextSelectionToolbarButton.text(
context: context,
onPressed: onPressed,
text: text,
));
}
if (widget.handleCut != null) {
addToolbarButton(localizations.cutButtonLabel, widget.handleCut!);
}
if (widget.handleCopy != null) {
addToolbarButton(localizations.copyButtonLabel, widget.handleCopy!);
}
if (widget.handlePaste != null
&& _clipboardStatus!.value == ClipboardStatus.pasteable) {
addToolbarButton(localizations.pasteButtonLabel, widget.handlePaste!);
}
if (widget.handleSelectAll != null) {
addToolbarButton(localizations.selectAllButtonLabel, widget.handleSelectAll!);
}
// If there is no option available, build an empty widget.
if (items.isEmpty) {
return const SizedBox(width: 0.0, height: 0.0);
}
return _DesktopTextSelectionToolbar(
anchor: widget.lastSecondaryTapDownPosition ?? midpointAnchor,
children: items,
);
}
}
/// A Material-style desktop text selection toolbar.
///
/// Typically displays buttons for text manipulation, e.g. copying and pasting
/// text.
///
/// Tries to position itself as closesly as possible to [anchor] while remaining
/// fully on-screen.
///
/// See also:
///
/// * [_DesktopTextSelectionControls.buildToolbar], where this is used by
/// default to build a Material-style desktop toolbar.
/// * [TextSelectionToolbar], which is similar, but builds an Android-style
/// toolbar.
class _DesktopTextSelectionToolbar extends StatelessWidget {
/// Creates an instance of _DesktopTextSelectionToolbar.
const _DesktopTextSelectionToolbar({
Key? key,
required this.anchor,
required this.children,
this.toolbarBuilder = _defaultToolbarBuilder,
}) : assert(children.length > 0),
super(key: key);
/// The point at which the toolbar will attempt to position itself as closely
/// as possible.
final Offset anchor;
/// {@macro flutter.material.TextSelectionToolbar.children}
///
/// See also:
/// * [DesktopTextSelectionToolbarButton], which builds a default
/// Material-style desktop text selection toolbar text button.
final List<Widget> children;
/// {@macro flutter.material.TextSelectionToolbar.toolbarBuilder}
///
/// The given anchor and isAbove can be used to position an arrow, as in the
/// default toolbar.
final ToolbarBuilder toolbarBuilder;
// Builds a desktop toolbar in the Material style.
static Widget _defaultToolbarBuilder(BuildContext context, Widget child) {
return SizedBox(
width: _kToolbarWidth,
child: Material(
borderRadius: const BorderRadius.all(Radius.circular(7.0)),
clipBehavior: Clip.antiAlias,
elevation: 1.0,
type: MaterialType.card,
child: child,
),
);
}
@override
Widget build(BuildContext context) {
assert(debugCheckHasMediaQuery(context));
final MediaQueryData mediaQuery = MediaQuery.of(context);
final double paddingAbove = mediaQuery.padding.top + _kToolbarScreenPadding;
final Offset localAdjustment = Offset(_kToolbarScreenPadding, paddingAbove);
return Padding(
padding: EdgeInsets.fromLTRB(
_kToolbarScreenPadding,
paddingAbove,
_kToolbarScreenPadding,
_kToolbarScreenPadding,
),
child: CustomSingleChildLayout(
delegate: DesktopTextSelectionToolbarLayoutDelegate(
anchor: anchor - localAdjustment,
),
child: toolbarBuilder(context, Column(
mainAxisSize: MainAxisSize.min,
children: children,
)),
),
);
}
}
const TextStyle _kToolbarButtonFontStyle = TextStyle(
inherit: false,
fontSize: 14.0,
letterSpacing: -0.15,
fontWeight: FontWeight.w400,
);
const EdgeInsets _kToolbarButtonPadding = EdgeInsets.fromLTRB(
20.0,
0.0,
20.0,
3.0,
);
/// A [TextButton] for the Material desktop text selection toolbar.
class _DesktopTextSelectionToolbarButton extends StatelessWidget {
/// Creates an instance of DesktopTextSelectionToolbarButton.
const _DesktopTextSelectionToolbarButton({
Key? key,
required this.onPressed,
required this.child,
}) : super(key: key);
/// Create an instance of [_DesktopTextSelectionToolbarButton] whose child is
/// a [Text] widget in the style of the Material text selection toolbar.
_DesktopTextSelectionToolbarButton.text({
Key? key,
required BuildContext context,
required this.onPressed,
required String text,
}) : child = Text(
text,
overflow: TextOverflow.ellipsis,
style: _kToolbarButtonFontStyle.copyWith(
color: Theme.of(context).colorScheme.brightness == Brightness.dark
? Colors.white
: Colors.black87,
),
),
super(key: key);
/// {@macro flutter.material.TextSelectionToolbarTextButton.onPressed}
final VoidCallback onPressed;
/// {@macro flutter.material.TextSelectionToolbarTextButton.child}
final Widget child;
@override
Widget build(BuildContext context) {
// TODO(hansmuller): Should be colorScheme.onSurface
final ThemeData theme = Theme.of(context);
final bool isDark = theme.colorScheme.brightness == Brightness.dark;
final Color primary = isDark ? Colors.white : Colors.black87;
return SizedBox(
width: double.infinity,
child: TextButton(
style: TextButton.styleFrom(
alignment: Alignment.centerLeft,
primary: primary,
shape: const RoundedRectangleBorder(),
minimumSize: const Size(kMinInteractiveDimension, 36.0),
padding: _kToolbarButtonPadding,
),
onPressed: onPressed,
child: child,
),
);
}
}

View file

@ -9,6 +9,7 @@ import 'package:flutter/widgets.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.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 'text_selection_theme.dart';
@ -606,10 +607,18 @@ class _SelectableTextState extends State<SelectableText> with AutomaticKeepAlive
case TargetPlatform.android: case TargetPlatform.android:
case TargetPlatform.fuchsia: case TargetPlatform.fuchsia:
forcePressEnabled = false;
textSelectionControls ??= materialTextSelectionControls;
paintCursorAboveText = false;
cursorOpacityAnimates = false;
cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;
selectionColor = selectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
break;
case TargetPlatform.linux: case TargetPlatform.linux:
case TargetPlatform.windows: case TargetPlatform.windows:
forcePressEnabled = false; forcePressEnabled = false;
textSelectionControls ??= materialTextSelectionControls; textSelectionControls ??= desktopTextSelectionControls;
paintCursorAboveText = false; paintCursorAboveText = false;
cursorOpacityAnimates = false; cursorOpacityAnimates = false;
cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary; cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;

View file

@ -13,6 +13,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.dart';
import 'debug.dart'; import 'debug.dart';
import 'desktop_text_selection.dart';
import 'feedback.dart'; import 'feedback.dart';
import 'input_decorator.dart'; import 'input_decorator.dart';
import 'material.dart'; import 'material.dart';
@ -1169,10 +1170,18 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
case TargetPlatform.android: case TargetPlatform.android:
case TargetPlatform.fuchsia: case TargetPlatform.fuchsia:
forcePressEnabled = false;
textSelectionControls ??= materialTextSelectionControls;
paintCursorAboveText = false;
cursorOpacityAnimates = false;
cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;
selectionColor = selectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
break;
case TargetPlatform.linux: case TargetPlatform.linux:
case TargetPlatform.windows: case TargetPlatform.windows:
forcePressEnabled = false; forcePressEnabled = false;
textSelectionControls ??= materialTextSelectionControls; textSelectionControls ??= desktopTextSelectionControls;
paintCursorAboveText = false; paintCursorAboveText = false;
cursorOpacityAnimates = false; cursorOpacityAnimates = false;
cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary; cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;

View file

@ -38,12 +38,16 @@ class TextSelectionToolbarTextButton extends StatelessWidget {
static const double _kMiddlePadding = 9.5; static const double _kMiddlePadding = 9.5;
static const double _kEndPadding = 14.5; static const double _kEndPadding = 14.5;
/// {@template flutter.material.TextSelectionToolbarTextButton.child}
/// The child of this button. /// The child of this button.
/// ///
/// Usually a [Text]. /// Usually a [Text].
/// {@endtemplate}
final Widget child; final Widget child;
/// {@template flutter.material.TextSelectionToolbarTextButton.onPressed}
/// Called when this button is pressed. /// Called when this button is pressed.
/// {@endtemplate}
final VoidCallback? onPressed; final VoidCallback? onPressed;
/// The padding between the button's edge and its child. /// The padding between the button's edge and its child.

View file

@ -0,0 +1,51 @@
// 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 'package:flutter/rendering.dart';
/// Positions the toolbar at [anchor] if it fits, otherwise moves it so that it
/// just fits fully on-screen.
///
/// See also:
///
/// * [desktopTextSelectionControls], which uses this to position
/// itself.
/// * [cupertinoDesktopTextSelectionControls], which uses this to position
/// itself.
/// * [TextSelectionToolbarLayoutDelegate], which does a similar layout for
/// the mobile text selection toolbars.
class DesktopTextSelectionToolbarLayoutDelegate extends SingleChildLayoutDelegate {
/// Creates an instance of TextSelectionToolbarLayoutDelegate.
DesktopTextSelectionToolbarLayoutDelegate({
required this.anchor,
});
/// The point at which to render the menu, if possible.
///
/// Should be provided in local coordinates.
final Offset anchor;
@override
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
return constraints.loosen();
}
@override
Offset getPositionForChild(Size size, Size childSize) {
final Offset overhang = Offset(
anchor.dx + childSize.width - size.width,
anchor.dy + childSize.height - size.height,
);
return Offset(
overhang.dx > 0.0 ? anchor.dx - overhang.dx : anchor.dx,
overhang.dy > 0.0 ? anchor.dy - overhang.dy : anchor.dy,
);
}
@override
bool shouldRelayout(DesktopTextSelectionToolbarLayoutDelegate oldDelegate) {
return anchor != oldDelegate.anchor;
}
}

View file

@ -33,6 +33,7 @@ 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/desktop_text_selection_toolbar_layout_delegate.dart';
export 'src/widgets/dismissible.dart'; export 'src/widgets/dismissible.dart';
export 'src/widgets/disposable_build_context.dart'; export 'src/widgets/disposable_build_context.dart';
export 'src/widgets/drag_target.dart'; export 'src/widgets/drag_target.dart';

View file

@ -238,7 +238,7 @@ void main() {
expect(controller.text, ' blah2blah1'); expect(controller.text, ' blah2blah1');
expect(controller.selection, const TextSelection(baseOffset: 0, extentOffset: 0)); expect(controller.selection, const TextSelection(baseOffset: 0, extentOffset: 0));
expect(find.byType(CupertinoButton), findsNothing); expect(find.byType(CupertinoButton), findsNothing);
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.macOS }), skip: kIsWeb); }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.macOS, TargetPlatform.windows, TargetPlatform.linux }), skip: kIsWeb);
testWidgets('TextField passes onEditingComplete to EditableText', (WidgetTester tester) async { testWidgets('TextField passes onEditingComplete to EditableText', (WidgetTester tester) async {
final VoidCallback onEditingComplete = () { }; final VoidCallback onEditingComplete = () { };
@ -7296,7 +7296,7 @@ void main() {
); );
// The text selection toolbar isn't shown on Mac without a right click. // The text selection toolbar isn't shown on Mac without a right click.
expect(find.byType(CupertinoButton), findsNothing); expect(find.byType(CupertinoButton), findsNothing);
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.macOS }), skip: kIsWeb); }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.macOS, TargetPlatform.windows, TargetPlatform.linux }), skip: kIsWeb);
testWidgets('double tap chains work', (WidgetTester tester) async { testWidgets('double tap chains work', (WidgetTester tester) async {
final TextEditingController controller = TextEditingController( final TextEditingController controller = TextEditingController(
@ -7445,7 +7445,7 @@ void main() {
const TextSelection(baseOffset: 8, extentOffset: 12), const TextSelection(baseOffset: 8, extentOffset: 12),
); );
expect(find.byType(CupertinoButton), findsNothing); expect(find.byType(CupertinoButton), findsNothing);
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.macOS }), skip: kIsWeb); }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.macOS, TargetPlatform.windows, TargetPlatform.linux }), skip: kIsWeb);
testWidgets('double tapping a space selects the previous word on iOS', (WidgetTester tester) async { testWidgets('double tapping a space selects the previous word on iOS', (WidgetTester tester) async {
final TextEditingController controller = TextEditingController( final TextEditingController controller = TextEditingController(
@ -7639,7 +7639,7 @@ void main() {
expect(controller.value.selection, isNotNull); expect(controller.value.selection, isNotNull);
expect(controller.value.selection.baseOffset, 0); expect(controller.value.selection.baseOffset, 0);
expect(controller.value.selection.extentOffset, 1); expect(controller.value.selection.extentOffset, 1);
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.macOS })); }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.macOS, TargetPlatform.windows, TargetPlatform.linux }), skip: kIsWeb);
testWidgets('force press does not select a word', (WidgetTester tester) async { testWidgets('force press does not select a word', (WidgetTester tester) async {
final TextEditingController controller = TextEditingController( final TextEditingController controller = TextEditingController(

View file

@ -112,7 +112,7 @@ void main() {
expect(controller.text, ' blah2blah1'); expect(controller.text, ' blah2blah1');
expect(controller.selection, const TextSelection(baseOffset: 0, extentOffset: 0)); expect(controller.selection, const TextSelection(baseOffset: 0, extentOffset: 0));
expect(find.byType(CupertinoButton), findsNothing); expect(find.byType(CupertinoButton), findsNothing);
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.macOS }), skip: kIsWeb); }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.macOS, TargetPlatform.windows, TargetPlatform.linux }), skip: kIsWeb);
testWidgets('Passes textAlign to underlying TextField', (WidgetTester tester) async { testWidgets('Passes textAlign to underlying TextField', (WidgetTester tester) async {
const TextAlign alignment = TextAlign.center; const TextAlign alignment = TextAlign.center;

View file

@ -255,7 +255,7 @@ void main() {
expect(controller.text, ' blah2blah1'); expect(controller.text, ' blah2blah1');
expect(controller.selection, const TextSelection(baseOffset: 0, extentOffset: 0)); expect(controller.selection, const TextSelection(baseOffset: 0, extentOffset: 0));
expect(find.byType(CupertinoButton), findsNothing); expect(find.byType(CupertinoButton), findsNothing);
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.macOS }), skip: kIsWeb); }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.macOS, TargetPlatform.windows, TargetPlatform.linux }), skip: kIsWeb);
testWidgets('has expected defaults', (WidgetTester tester) async { testWidgets('has expected defaults', (WidgetTester tester) async {
await tester.pumpWidget( await tester.pumpWidget(
@ -4329,8 +4329,6 @@ void main() {
variant: const TargetPlatformVariant(<TargetPlatform>{ variant: const TargetPlatformVariant(<TargetPlatform>{
TargetPlatform.android, TargetPlatform.android,
TargetPlatform.fuchsia, TargetPlatform.fuchsia,
TargetPlatform.linux,
TargetPlatform.windows,
}), }),
); );
@ -4367,8 +4365,49 @@ void main() {
variant: const TargetPlatformVariant(<TargetPlatform>{ variant: const TargetPlatformVariant(<TargetPlatform>{
TargetPlatform.android, TargetPlatform.android,
TargetPlatform.fuchsia, TargetPlatform.fuchsia,
TargetPlatform.linux, }),
);
testWidgets('The Select All calls on selection changed with a mouse on windows and linux', (WidgetTester tester) async {
const String string = 'abc def ghi';
TextSelection? newSelection;
await tester.pumpWidget(
MaterialApp(
home: Material(
child: SelectableText(
string,
onSelectionChanged: (TextSelection selection, SelectionChangedCause? cause) {
expect(newSelection, isNull);
newSelection = selection;
},
),
),
),
);
// Right-click on the 'e' in 'def'.
final Offset ePos = textOffsetToPosition(tester, 5);
final TestGesture gesture = await tester.startGesture(
ePos,
kind: PointerDeviceKind.mouse,
buttons: kSecondaryMouseButton,
);
addTearDown(gesture.removePointer);
await tester.pump();
await gesture.up();
await tester.pumpAndSettle();
expect(newSelection!.baseOffset, 4);
expect(newSelection!.extentOffset, 7);
newSelection = null;
await tester.tap(find.text('Select all'));
await tester.pump();
expect(newSelection!.baseOffset, 0);
expect(newSelection!.extentOffset, 11);
},
variant: const TargetPlatformVariant(<TargetPlatform>{
TargetPlatform.windows, TargetPlatform.windows,
TargetPlatform.linux,
}), }),
); );