Skip to content

Commit

Permalink
Add 'Share' button to the selection toolbar on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
bleroux committed Dec 5, 2023
1 parent 2c61350 commit 3e9b284
Show file tree
Hide file tree
Showing 10 changed files with 726 additions and 494 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class AdaptiveTextSelectionToolbar extends StatelessWidget {
case ContextMenuButtonType.searchWeb:
return localizations.searchWebButtonLabel;
case ContextMenuButtonType.share:
return localizations.searchWebButtonLabel;
return localizations.shareButtonLabel;
case ContextMenuButtonType.liveTextInput:
return localizations.scanTextButtonLabel;
case ContextMenuButtonType.custom:
Expand Down
32 changes: 23 additions & 9 deletions packages/flutter/lib/src/widgets/editable_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,10 @@ class EditableText extends StatefulWidget {
// If the paste button is enabled, don't render anything until the state
// of the clipboard is known, since it's used to determine if paste is
// shown.

// On Android, the share button is before the select all button.
final bool showShareBeforeSelectAll = defaultTargetPlatform == TargetPlatform.android;

resultButtonItem.addAll(<ContextMenuButtonItem>[
if (onCut != null)
ContextMenuButtonItem(
Expand All @@ -1898,6 +1902,11 @@ class EditableText extends StatefulWidget {
onPressed: onPaste,
type: ContextMenuButtonType.paste,
),
if (onShare != null && showShareBeforeSelectAll)
ContextMenuButtonItem(
onPressed: onShare,
type: ContextMenuButtonType.share,
),
if (onSelectAll != null)
ContextMenuButtonItem(
onPressed: onSelectAll,
Expand All @@ -1913,7 +1922,7 @@ class EditableText extends StatefulWidget {
onPressed: onSearchWeb,
type: ContextMenuButtonType.searchWeb,
),
if (onShare != null)
if (onShare != null && !showShareBeforeSelectAll)
ContextMenuButtonItem(
onPressed: onShare,
type: ContextMenuButtonType.share,
Expand Down Expand Up @@ -2300,13 +2309,18 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien

@override
bool get shareEnabled {
if (defaultTargetPlatform != TargetPlatform.iOS) {
return false;
switch (defaultTargetPlatform) {
case TargetPlatform.android:
case TargetPlatform.iOS:
return !widget.obscureText
&& !textEditingValue.selection.isCollapsed
&& textEditingValue.selection.textInside(textEditingValue.text).trim() != '';
case TargetPlatform.macOS:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
return false;
}

return !widget.obscureText
&& !textEditingValue.selection.isCollapsed
&& textEditingValue.selection.textInside(textEditingValue.text).trim() != '';
}

@override
Expand Down Expand Up @@ -2516,9 +2530,9 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
}

/// Launch the share interface for the current selection,
/// as in the "Share" edit menu button on iOS.
/// as in the "Share..." edit menu button on iOS.
///
/// Currently this is only implemented for iOS.
/// Currently this is only implemented for iOS and Android.
///
/// When 'obscureText' is true or the selection is empty,
/// this function will not do anything
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';

import '../widgets/clipboard_utils.dart';
import '../widgets/live_text_utils.dart';
import '../widgets/text_selection_toolbar_utils.dart';

void main() {
final MockClipboard mockClipboard = MockClipboard();
Expand All @@ -31,13 +32,6 @@ void main() {
);
});

Finder findOverflowNextButton() {
return find.byWidgetPredicate((Widget widget) =>
widget is CustomPaint &&
'${widget.painter?.runtimeType}' == '_RightCupertinoChevronPainter',
);
}

testWidgetsWithLeakTracking('Builds the right toolbar on each platform, including web, and shows buttonItems', (WidgetTester tester) async {
const String buttonText = 'Click me';

Expand Down Expand Up @@ -188,27 +182,55 @@ void main() {
));

expect(find.byKey(key), findsOneWidget);
expect(find.text('Copy'), findsOneWidget);
expect(find.text('Cut'), findsOneWidget);
expect(find.text('Select All'), findsOneWidget);
expect(find.text('Paste'), findsOneWidget);
expect(find.text('Look Up'), findsOneWidget);

switch (defaultTargetPlatform) {
case TargetPlatform.android:
expect(find.byType(CupertinoTextSelectionToolbarButton), findsNWidgets(6));
expect(find.text('Cut'), findsOneWidget);
expect(find.text('Copy'), findsOneWidget);
expect(find.text('Paste'), findsOneWidget);
expect(find.text('Select All'), findsOneWidget);
expect(find.text('Share...'), findsOneWidget);
expect(findCupertinoOverflowNextButton(), findsOneWidget);

await tapCupertinoOverflowNextButton(tester);

expect(find.byType(CupertinoTextSelectionToolbarButton), findsNWidgets(4));
expect(findCupertinoOverflowBackButton(), findsOneWidget);
expect(find.text('Look Up'), findsOneWidget);
expect(find.text('Search Web'), findsOneWidget);
expect(findLiveTextButton(), findsOneWidget);

case TargetPlatform.fuchsia:
expect(find.byType(CupertinoTextSelectionToolbarButton), findsNWidgets(6));
case TargetPlatform.iOS:
expect(find.byType(CupertinoTextSelectionToolbarButton), findsNWidgets(6));
expect(findOverflowNextButton(), findsOneWidget);
await tester.tapAt(tester.getCenter(findOverflowNextButton()));
await tester.pumpAndSettle();
expect(find.text('Cut'), findsOneWidget);
expect(find.text('Copy'), findsOneWidget);
expect(find.text('Paste'), findsOneWidget);
expect(find.text('Select All'), findsOneWidget);
expect(find.text('Look Up'), findsOneWidget);
expect(findCupertinoOverflowNextButton(), findsOneWidget);

await tapCupertinoOverflowNextButton(tester);

expect(find.byType(CupertinoTextSelectionToolbarButton), findsNWidgets(4));
expect(findCupertinoOverflowBackButton(), findsOneWidget);
expect(find.text('Search Web'), findsOneWidget);
expect(find.text('Share...'), findsOneWidget);
expect(findLiveTextButton(), findsOneWidget);

case TargetPlatform.macOS:
case TargetPlatform.linux:
case TargetPlatform.windows:
expect(find.byType(CupertinoDesktopTextSelectionToolbarButton), findsNWidgets(8));
expect(find.text('Cut'), findsOneWidget);
expect(find.text('Copy'), findsOneWidget);
expect(find.text('Paste'), findsOneWidget);
expect(find.text('Select All'), findsOneWidget);
expect(find.text('Share...'), findsOneWidget);
expect(find.text('Look Up'), findsOneWidget);
expect(find.text('Search Web'), findsOneWidget);
expect(findLiveTextButton(), findsOneWidget);
}
},
skip: kIsWeb, // [intended] on web the browser handles the context menu.
Expand Down

0 comments on commit 3e9b284

Please sign in to comment.