diff --git a/packages/flutter/lib/src/cupertino/desktop_text_selection.dart b/packages/flutter/lib/src/cupertino/desktop_text_selection.dart index e2c2d88f5c..23e600f163 100644 --- a/packages/flutter/lib/src/cupertino/desktop_text_selection.dart +++ b/packages/flutter/lib/src/cupertino/desktop_text_selection.dart @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/widgets.dart'; @@ -49,15 +48,15 @@ class _CupertinoDesktopTextSelectionControls extends TextSelectionControls { Offset selectionMidpoint, List endpoints, TextSelectionDelegate delegate, - ValueListenable? clipboardStatus, + ClipboardStatusNotifier clipboardStatus, Offset? lastSecondaryTapDownPosition, ) { return _CupertinoDesktopTextSelectionControlsToolbar( clipboardStatus: clipboardStatus, endpoints: endpoints, globalEditableRegion: globalEditableRegion, - handleCut: canCut(delegate) ? () => handleCut(delegate) : null, - handleCopy: canCopy(delegate) ? () => handleCopy(delegate) : null, + handleCut: canCut(delegate) ? () => handleCut(delegate, clipboardStatus) : null, + handleCopy: canCopy(delegate) ? () => handleCopy(delegate, clipboardStatus) : null, handlePaste: canPaste(delegate) ? () => handlePaste(delegate) : null, handleSelectAll: canSelectAll(delegate) ? () => handleSelectAll(delegate) : null, selectionMidpoint: selectionMidpoint, @@ -99,7 +98,7 @@ class _CupertinoDesktopTextSelectionControlsToolbar extends StatefulWidget { required this.lastSecondaryTapDownPosition, }) : super(key: key); - final ValueListenable? clipboardStatus; + final ClipboardStatusNotifier? clipboardStatus; final List endpoints; final Rect globalEditableRegion; final VoidCallback? handleCopy; @@ -115,6 +114,8 @@ class _CupertinoDesktopTextSelectionControlsToolbar extends StatefulWidget { } class _CupertinoDesktopTextSelectionControlsToolbarState extends State<_CupertinoDesktopTextSelectionControlsToolbar> { + ClipboardStatusNotifier? _clipboardStatus; + void _onChangedClipboardStatus() { setState(() { // Inform the widget that the value of clipboardStatus has changed. @@ -124,28 +125,46 @@ class _CupertinoDesktopTextSelectionControlsToolbarState extends State<_Cupertin @override void initState() { super.initState(); - widget.clipboardStatus?.addListener(_onChangedClipboardStatus); + if (widget.handlePaste != null) { + _clipboardStatus = widget.clipboardStatus ?? ClipboardStatusNotifier(); + _clipboardStatus!.addListener(_onChangedClipboardStatus); + _clipboardStatus!.update(); + } } @override void didUpdateWidget(_CupertinoDesktopTextSelectionControlsToolbar oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.clipboardStatus != widget.clipboardStatus) { - oldWidget.clipboardStatus?.removeListener(_onChangedClipboardStatus); - widget.clipboardStatus?.addListener(_onChangedClipboardStatus); + 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(); - widget.clipboardStatus?.removeListener(_onChangedClipboardStatus); + // 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 && widget.clipboardStatus?.value == ClipboardStatus.unknown) { + if (widget.handlePaste != null && _clipboardStatus!.value == ClipboardStatus.unknown) { return const SizedBox(width: 0.0, height: 0.0); } @@ -187,7 +206,7 @@ class _CupertinoDesktopTextSelectionControlsToolbarState extends State<_Cupertin addToolbarButton(localizations.copyButtonLabel, widget.handleCopy!); } if (widget.handlePaste != null - && widget.clipboardStatus?.value == ClipboardStatus.pasteable) { + && _clipboardStatus!.value == ClipboardStatus.pasteable) { addToolbarButton(localizations.pasteButtonLabel, widget.handlePaste!); } if (widget.handleSelectAll != null) { diff --git a/packages/flutter/lib/src/cupertino/text_selection.dart b/packages/flutter/lib/src/cupertino/text_selection.dart index 24b8c295fc..69376ad842 100644 --- a/packages/flutter/lib/src/cupertino/text_selection.dart +++ b/packages/flutter/lib/src/cupertino/text_selection.dart @@ -4,7 +4,6 @@ import 'dart:math' as math; -import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/widgets.dart'; @@ -38,7 +37,7 @@ class _CupertinoTextSelectionControlsToolbar extends StatefulWidget { required this.textLineHeight, }) : super(key: key); - final ValueListenable? clipboardStatus; + final ClipboardStatusNotifier? clipboardStatus; final List endpoints; final Rect globalEditableRegion; final VoidCallback? handleCopy; @@ -53,6 +52,8 @@ class _CupertinoTextSelectionControlsToolbar extends StatefulWidget { } class _CupertinoTextSelectionControlsToolbarState extends State<_CupertinoTextSelectionControlsToolbar> { + ClipboardStatusNotifier? _clipboardStatus; + void _onChangedClipboardStatus() { setState(() { // Inform the widget that the value of clipboardStatus has changed. @@ -62,28 +63,47 @@ class _CupertinoTextSelectionControlsToolbarState extends State<_CupertinoTextSe @override void initState() { super.initState(); - widget.clipboardStatus?.addListener(_onChangedClipboardStatus); + if (widget.handlePaste != null) { + _clipboardStatus = widget.clipboardStatus ?? ClipboardStatusNotifier(); + _clipboardStatus!.addListener(_onChangedClipboardStatus); + _clipboardStatus!.update(); + } } @override void didUpdateWidget(_CupertinoTextSelectionControlsToolbar oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.clipboardStatus != widget.clipboardStatus) { - oldWidget.clipboardStatus?.removeListener(_onChangedClipboardStatus); - widget.clipboardStatus?.addListener(_onChangedClipboardStatus); + 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(); - widget.clipboardStatus?.removeListener(_onChangedClipboardStatus); + // 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 && widget.clipboardStatus?.value == ClipboardStatus.unknown) { + if (widget.handlePaste != null + && _clipboardStatus!.value == ClipboardStatus.unknown) { return const SizedBox(width: 0.0, height: 0.0); } @@ -137,7 +157,7 @@ class _CupertinoTextSelectionControlsToolbarState extends State<_CupertinoTextSe addToolbarButton(localizations.copyButtonLabel, widget.handleCopy!); } if (widget.handlePaste != null - && widget.clipboardStatus?.value == ClipboardStatus.pasteable) { + && _clipboardStatus!.value == ClipboardStatus.pasteable) { addToolbarButton(localizations.pasteButtonLabel, widget.handlePaste!); } if (widget.handleSelectAll != null) { @@ -209,15 +229,15 @@ class CupertinoTextSelectionControls extends TextSelectionControls { Offset selectionMidpoint, List endpoints, TextSelectionDelegate delegate, - ValueListenable? clipboardStatus, + ClipboardStatusNotifier clipboardStatus, Offset? lastSecondaryTapDownPosition, ) { return _CupertinoTextSelectionControlsToolbar( clipboardStatus: clipboardStatus, endpoints: endpoints, globalEditableRegion: globalEditableRegion, - handleCut: canCut(delegate) ? () => handleCut(delegate) : null, - handleCopy: canCopy(delegate) ? () => handleCopy(delegate) : null, + handleCut: canCut(delegate) ? () => handleCut(delegate, clipboardStatus) : null, + handleCopy: canCopy(delegate) ? () => handleCopy(delegate, clipboardStatus) : null, handlePaste: canPaste(delegate) ? () => handlePaste(delegate) : null, handleSelectAll: canSelectAll(delegate) ? () => handleSelectAll(delegate) : null, selectionMidpoint: selectionMidpoint, diff --git a/packages/flutter/lib/src/material/desktop_text_selection.dart b/packages/flutter/lib/src/material/desktop_text_selection.dart index ba8513d4e4..0ac7c22d88 100644 --- a/packages/flutter/lib/src/material/desktop_text_selection.dart +++ b/packages/flutter/lib/src/material/desktop_text_selection.dart @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; @@ -35,15 +34,15 @@ class _DesktopTextSelectionControls extends TextSelectionControls { Offset selectionMidpoint, List endpoints, TextSelectionDelegate delegate, - ValueListenable? clipboardStatus, + ClipboardStatusNotifier clipboardStatus, Offset? lastSecondaryTapDownPosition, ) { return _DesktopTextSelectionControlsToolbar( clipboardStatus: clipboardStatus, endpoints: endpoints, globalEditableRegion: globalEditableRegion, - handleCut: canCut(delegate) ? () => handleCut(delegate) : null, - handleCopy: canCopy(delegate) ? () => handleCopy(delegate) : null, + handleCut: canCut(delegate) ? () => handleCut(delegate, clipboardStatus) : null, + handleCopy: canCopy(delegate) ? () => handleCopy(delegate, clipboardStatus) : null, handlePaste: canPaste(delegate) ? () => handlePaste(delegate) : null, handleSelectAll: canSelectAll(delegate) ? () => handleSelectAll(delegate) : null, selectionMidpoint: selectionMidpoint, @@ -95,7 +94,7 @@ class _DesktopTextSelectionControlsToolbar extends StatefulWidget { required this.lastSecondaryTapDownPosition, }) : super(key: key); - final ValueListenable? clipboardStatus; + final ClipboardStatusNotifier? clipboardStatus; final List endpoints; final Rect globalEditableRegion; final VoidCallback? handleCopy; @@ -111,6 +110,8 @@ class _DesktopTextSelectionControlsToolbar extends StatefulWidget { } class _DesktopTextSelectionControlsToolbarState extends State<_DesktopTextSelectionControlsToolbar> { + ClipboardStatusNotifier? _clipboardStatus; + void _onChangedClipboardStatus() { setState(() { // Inform the widget that the value of clipboardStatus has changed. @@ -120,28 +121,46 @@ class _DesktopTextSelectionControlsToolbarState extends State<_DesktopTextSelect @override void initState() { super.initState(); - widget.clipboardStatus?.addListener(_onChangedClipboardStatus); + 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) { - oldWidget.clipboardStatus?.removeListener(_onChangedClipboardStatus); - widget.clipboardStatus?.addListener(_onChangedClipboardStatus); + 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(); - widget.clipboardStatus?.removeListener(_onChangedClipboardStatus); + // 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 && widget.clipboardStatus?.value == ClipboardStatus.unknown) { + if (widget.handlePaste != null && _clipboardStatus!.value == ClipboardStatus.unknown) { return const SizedBox(width: 0.0, height: 0.0); } @@ -178,7 +197,7 @@ class _DesktopTextSelectionControlsToolbarState extends State<_DesktopTextSelect addToolbarButton(localizations.copyButtonLabel, widget.handleCopy!); } if (widget.handlePaste != null - && widget.clipboardStatus?.value == ClipboardStatus.pasteable) { + && _clipboardStatus!.value == ClipboardStatus.pasteable) { addToolbarButton(localizations.pasteButtonLabel, widget.handlePaste!); } if (widget.handleSelectAll != null) { diff --git a/packages/flutter/lib/src/material/text_selection.dart b/packages/flutter/lib/src/material/text_selection.dart index 93e19363cb..1af3e887ea 100644 --- a/packages/flutter/lib/src/material/text_selection.dart +++ b/packages/flutter/lib/src/material/text_selection.dart @@ -4,7 +4,6 @@ import 'dart:math' as math; -import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/widgets.dart'; @@ -36,7 +35,7 @@ class MaterialTextSelectionControls extends TextSelectionControls { Offset selectionMidpoint, List endpoints, TextSelectionDelegate delegate, - ValueListenable? clipboardStatus, + ClipboardStatusNotifier clipboardStatus, Offset? lastSecondaryTapDownPosition, ) { return _TextSelectionControlsToolbar( @@ -46,8 +45,8 @@ class MaterialTextSelectionControls extends TextSelectionControls { endpoints: endpoints, delegate: delegate, clipboardStatus: clipboardStatus, - handleCut: canCut(delegate) ? () => handleCut(delegate) : null, - handleCopy: canCopy(delegate) ? () => handleCopy(delegate) : null, + handleCut: canCut(delegate) ? () => handleCut(delegate, clipboardStatus) : null, + handleCopy: canCopy(delegate) ? () => handleCopy(delegate, clipboardStatus) : null, handlePaste: canPaste(delegate) ? () => handlePaste(delegate) : null, handleSelectAll: canSelectAll(delegate) ? () => handleSelectAll(delegate) : null, ); @@ -144,7 +143,7 @@ class _TextSelectionControlsToolbar extends StatefulWidget { required this.textLineHeight, }) : super(key: key); - final ValueListenable? clipboardStatus; + final ClipboardStatusNotifier clipboardStatus; final TextSelectionDelegate delegate; final List endpoints; final Rect globalEditableRegion; @@ -169,22 +168,28 @@ class _TextSelectionControlsToolbarState extends State<_TextSelectionControlsToo @override void initState() { super.initState(); - widget.clipboardStatus?.addListener(_onChangedClipboardStatus); + widget.clipboardStatus.addListener(_onChangedClipboardStatus); + widget.clipboardStatus.update(); } @override void didUpdateWidget(_TextSelectionControlsToolbar oldWidget) { super.didUpdateWidget(oldWidget); if (widget.clipboardStatus != oldWidget.clipboardStatus) { - widget.clipboardStatus?.addListener(_onChangedClipboardStatus); - oldWidget.clipboardStatus?.removeListener(_onChangedClipboardStatus); + widget.clipboardStatus.addListener(_onChangedClipboardStatus); + oldWidget.clipboardStatus.removeListener(_onChangedClipboardStatus); } + widget.clipboardStatus.update(); } @override void dispose() { super.dispose(); - widget.clipboardStatus?.removeListener(_onChangedClipboardStatus); + // When used in an Overlay, it can happen that this is disposed after its + // creator has already disposed _clipboardStatus. + if (!widget.clipboardStatus.disposed) { + widget.clipboardStatus.removeListener(_onChangedClipboardStatus); + } } @override @@ -197,7 +202,7 @@ class _TextSelectionControlsToolbarState extends State<_TextSelectionControlsToo // If the paste button is desired, don't render anything until the state of // the clipboard is known, since it's used to determine if paste is shown. if (widget.handlePaste != null - && widget.clipboardStatus?.value == ClipboardStatus.unknown) { + && widget.clipboardStatus.value == ClipboardStatus.unknown) { return const SizedBox.shrink(); } @@ -233,7 +238,7 @@ class _TextSelectionControlsToolbarState extends State<_TextSelectionControlsToo onPressed: widget.handleCopy!, ), if (widget.handlePaste != null - && widget.clipboardStatus?.value == ClipboardStatus.pasteable) + && widget.clipboardStatus.value == ClipboardStatus.pasteable) _TextSelectionToolbarItemData( label: localizations.pasteButtonLabel, onPressed: widget.handlePaste!, diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index a0d038649b..7f11ef7991 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -1671,7 +1671,6 @@ class EditableTextState extends State with AutomaticKeepAliveClien break; } } - _clipboardStatus?.update(); } /// Cut current selection to [Clipboard]. @@ -1695,7 +1694,6 @@ class EditableTextState extends State with AutomaticKeepAliveClien }); hideToolbar(); } - _clipboardStatus?.update(); } /// Paste text from [Clipboard]. @@ -2921,7 +2919,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien if (_selectionOverlay == null || _selectionOverlay!.toolbarIsVisible) { return false; } - _clipboardStatus?.update(); + _selectionOverlay!.showToolbar(); return true; } @@ -3028,7 +3026,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien && copyEnabled && _hasFocus && (controls?.canCopy(this) ?? false) - ? () => controls!.handleCopy(this) + ? () => controls!.handleCopy(this, _clipboardStatus) : null; } @@ -3037,7 +3035,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien && cutEnabled && _hasFocus && (controls?.canCut(this) ?? false) - ? () => controls!.handleCut(this) + ? () => controls!.handleCut(this, _clipboardStatus) : null; } diff --git a/packages/flutter/lib/src/widgets/text_selection.dart b/packages/flutter/lib/src/widgets/text_selection.dart index 9745ee635d..2baed764a1 100644 --- a/packages/flutter/lib/src/widgets/text_selection.dart +++ b/packages/flutter/lib/src/widgets/text_selection.dart @@ -142,7 +142,7 @@ abstract class TextSelectionControls { Offset position, List endpoints, TextSelectionDelegate delegate, - ValueListenable? clipboardStatus, + ClipboardStatusNotifier clipboardStatus, Offset? lastSecondaryTapDownPosition, ); @@ -199,16 +199,18 @@ abstract class TextSelectionControls { /// /// This is called by subclasses when their cut affordance is activated by /// the user. - void handleCut(TextSelectionDelegate delegate) { + void handleCut(TextSelectionDelegate delegate, ClipboardStatusNotifier? clipboardStatus) { delegate.cutSelection(SelectionChangedCause.toolbar); + clipboardStatus?.update(); } /// Call [TextSelectionDelegate.copySelection] to copy current selection. /// /// This is called by subclasses when their copy affordance is activated by /// the user. - void handleCopy(TextSelectionDelegate delegate) { + void handleCopy(TextSelectionDelegate delegate, ClipboardStatusNotifier? clipboardStatus) { delegate.copySelection(SelectionChangedCause.toolbar); + clipboardStatus?.update(); } /// Call [TextSelectionDelegate.pasteText] to paste text. @@ -1078,7 +1080,7 @@ class _SelectionToolbarOverlayState extends State<_SelectionToolbarOverlay> with widget.midpoint, widget.selectionEndpoints, widget.selectionDelegate!, - widget.clipboardStatus, + widget.clipboardStatus!, widget.toolbarLocation, ); }, @@ -2129,6 +2131,8 @@ class ClipboardStatusNotifier extends ValueNotifier with Widget }) : super(value); bool _disposed = false; + /// True if this instance has been disposed. + bool get disposed => _disposed; /// Check the [Clipboard] and update [value] if needed. Future update() async { @@ -2179,7 +2183,7 @@ class ClipboardStatusNotifier extends ValueNotifier with Widget @override void removeListener(VoidCallback listener) { super.removeListener(listener); - if (!_disposed && !hasListeners) { + if (!hasListeners) { WidgetsBinding.instance.removeObserver(this); } } @@ -2199,9 +2203,9 @@ class ClipboardStatusNotifier extends ValueNotifier with Widget @override void dispose() { + super.dispose(); WidgetsBinding.instance.removeObserver(this); _disposed = true; - super.dispose(); } } diff --git a/packages/flutter/test/cupertino/text_field_test.dart b/packages/flutter/test/cupertino/text_field_test.dart index 8f98a90b66..88d153e22b 100644 --- a/packages/flutter/test/cupertino/text_field_test.dart +++ b/packages/flutter/test/cupertino/text_field_test.dart @@ -43,7 +43,7 @@ class MockTextSelectionControls extends TextSelectionControls { Offset position, List endpoints, TextSelectionDelegate delegate, - ValueListenable? clipboardStatus, + ClipboardStatusNotifier clipboardStatus, Offset? lastSecondaryTapDownPosition, ) { throw UnimplementedError(); diff --git a/packages/flutter/test/cupertino/text_selection_toolbar_test.dart b/packages/flutter/test/cupertino/text_selection_toolbar_test.dart index 4474026425..7156dd1117 100644 --- a/packages/flutter/test/cupertino/text_selection_toolbar_test.dart +++ b/packages/flutter/test/cupertino/text_selection_toolbar_test.dart @@ -24,7 +24,7 @@ class _CustomCupertinoTextSelectionControls extends CupertinoTextSelectionContro Offset selectionMidpoint, List endpoints, TextSelectionDelegate delegate, - ValueListenable? clipboardStatus, + ClipboardStatusNotifier clipboardStatus, Offset? lastSecondaryTapDownPosition, ) { final MediaQueryData mediaQuery = MediaQuery.of(context); diff --git a/packages/flutter/test/material/text_selection_toolbar_test.dart b/packages/flutter/test/material/text_selection_toolbar_test.dart index 2b67ef5e6a..eaea86f947 100644 --- a/packages/flutter/test/material/text_selection_toolbar_test.dart +++ b/packages/flutter/test/material/text_selection_toolbar_test.dart @@ -22,7 +22,7 @@ class _CustomMaterialTextSelectionControls extends MaterialTextSelectionControls Offset selectionMidpoint, List endpoints, TextSelectionDelegate delegate, - ValueListenable? clipboardStatus, + ClipboardStatusNotifier clipboardStatus, Offset? lastSecondaryTapDownPosition, ) { final TextSelectionPoint startTextSelectionPoint = endpoints[0]; diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 497b1d4f2c..f113f23cba 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -11747,7 +11747,7 @@ class MockTextFormatter extends TextInputFormatter { class MockTextSelectionControls extends Fake implements TextSelectionControls { @override - Widget buildToolbar(BuildContext context, Rect globalEditableRegion, double textLineHeight, Offset position, List endpoints, TextSelectionDelegate delegate, ValueListenable? clipboardStatus, Offset? lastSecondaryTapDownPosition) { + Widget buildToolbar(BuildContext context, Rect globalEditableRegion, double textLineHeight, Offset position, List endpoints, TextSelectionDelegate delegate, ClipboardStatusNotifier clipboardStatus, Offset? lastSecondaryTapDownPosition) { return Container(); } @@ -11775,7 +11775,7 @@ class MockTextSelectionControls extends Fake implements TextSelectionControls { int copyCount = 0; @override - void handleCopy(TextSelectionDelegate delegate) { + void handleCopy(TextSelectionDelegate delegate, ClipboardStatusNotifier? clipboardStatus) { copyCount += 1; } @@ -11785,7 +11785,7 @@ class MockTextSelectionControls extends Fake implements TextSelectionControls { } @override - void handleCut(TextSelectionDelegate delegate) { + void handleCut(TextSelectionDelegate delegate, ClipboardStatusNotifier? clipboardStatus) { cutCount += 1; } diff --git a/packages/flutter/test/widgets/text_selection_test.dart b/packages/flutter/test/widgets/text_selection_test.dart index f5232d3803..e6ba90170f 100644 --- a/packages/flutter/test/widgets/text_selection_test.dart +++ b/packages/flutter/test/widgets/text_selection_test.dart @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:flutter/foundation.dart' show defaultTargetPlatform, ValueListenable; +import 'package:flutter/foundation.dart' show defaultTargetPlatform; import 'package:flutter/gestures.dart' show PointerDeviceKind, kSecondaryButton; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; @@ -1034,6 +1034,28 @@ void main() { }); }); }); + + group('TextSelectionControls', () { + test('ClipboardStatusNotifier is updated on handleCut', () async { + final FakeClipboardStatusNotifier clipboardStatus = FakeClipboardStatusNotifier(); + final FakeTextSelectionDelegate delegate = FakeTextSelectionDelegate(); + final CustomTextSelectionControls textSelectionControls = CustomTextSelectionControls(); + + expect(clipboardStatus.updateCalled, false); + textSelectionControls.handleCut(delegate, clipboardStatus); + expect(clipboardStatus.updateCalled, true); + }); + + test('ClipboardStatusNotifier is updated on handleCopy', () async { + final FakeClipboardStatusNotifier clipboardStatus = FakeClipboardStatusNotifier(); + final FakeTextSelectionDelegate delegate = FakeTextSelectionDelegate(); + final CustomTextSelectionControls textSelectionControls = CustomTextSelectionControls(); + + expect(clipboardStatus.updateCalled, false); + textSelectionControls.handleCopy(delegate, clipboardStatus); + expect(clipboardStatus.updateCalled, true); + }); + }); } class FakeTextSelectionGestureDetectorBuilderDelegate implements TextSelectionGestureDetectorBuilderDelegate { @@ -1162,7 +1184,7 @@ class CustomTextSelectionControls extends TextSelectionControls { Offset position, List endpoints, TextSelectionDelegate delegate, - ValueListenable? clipboardStatus, + ClipboardStatusNotifier clipboardStatus, Offset? lastSecondaryTapDownPosition, ) { throw UnimplementedError(); @@ -1199,15 +1221,15 @@ class TextSelectionControlsSpy extends TextSelectionControls { @override Widget buildToolbar( - BuildContext context, - Rect globalEditableRegion, - double textLineHeight, - Offset position, - List endpoints, - TextSelectionDelegate delegate, - ValueListenable? clipboardStatus, - Offset? lastSecondaryTapDownPosition, - ) { + BuildContext context, + Rect globalEditableRegion, + double textLineHeight, + Offset position, + List endpoints, + TextSelectionDelegate delegate, + ClipboardStatusNotifier clipboardStatus, + Offset? lastSecondaryTapDownPosition, + ) { return Text('dummy', key: toolBarKey); } @@ -1225,6 +1247,9 @@ class TextSelectionControlsSpy extends TextSelectionControls { class FakeClipboardStatusNotifier extends ClipboardStatusNotifier { FakeClipboardStatusNotifier() : super(value: ClipboardStatus.unknown); + @override + bool get disposed => false; + bool updateCalled = false; @override Future update() async {