mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 22:09:06 +00:00
Add implementations for onShare and onSearchWeb (#2143)
Fixes #2088 ## Pre-launch Checklist - [x] I read the [Flutter Style Guide] _recently_, and have followed its advice. - [x] I signed the [CLA]. - [x] I read the [Contributors Guide]. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] All existing and new tests are passing. --------- Co-authored-by: Brett Morgan <brett.morgan@gmail.com>
This commit is contained in:
@@ -28,6 +28,8 @@ class BasicTextField extends StatefulWidget {
|
|||||||
VoidCallback? onSelectAll,
|
VoidCallback? onSelectAll,
|
||||||
VoidCallback? onLookUp,
|
VoidCallback? onLookUp,
|
||||||
VoidCallback? onLiveTextInput,
|
VoidCallback? onLiveTextInput,
|
||||||
|
VoidCallback? onSearchWeb,
|
||||||
|
VoidCallback? onShare,
|
||||||
TextSelectionToolbarAnchors anchors,
|
TextSelectionToolbarAnchors anchors,
|
||||||
) {
|
) {
|
||||||
return AdaptiveTextSelectionToolbar.editable(
|
return AdaptiveTextSelectionToolbar.editable(
|
||||||
@@ -38,10 +40,9 @@ class BasicTextField extends StatefulWidget {
|
|||||||
onSelectAll: onSelectAll,
|
onSelectAll: onSelectAll,
|
||||||
onLookUp: onLookUp,
|
onLookUp: onLookUp,
|
||||||
onLiveTextInput: onLiveTextInput,
|
onLiveTextInput: onLiveTextInput,
|
||||||
|
onSearchWeb: onSearchWeb,
|
||||||
|
onShare: onShare,
|
||||||
anchors: anchors,
|
anchors: anchors,
|
||||||
// TODO(Renzo-Olivares): https://github.com/flutter/samples/issues/2088
|
|
||||||
onSearchWeb: null,
|
|
||||||
onShare: null,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ typedef BasicTextFieldContextMenuBuilder = Widget Function(
|
|||||||
VoidCallback? onSelectAll,
|
VoidCallback? onSelectAll,
|
||||||
VoidCallback? onLookUp,
|
VoidCallback? onLookUp,
|
||||||
VoidCallback? onLiveTextInput,
|
VoidCallback? onLiveTextInput,
|
||||||
|
VoidCallback? onSearchWeb,
|
||||||
|
VoidCallback? onShare,
|
||||||
TextSelectionToolbarAnchors anchors,
|
TextSelectionToolbarAnchors anchors,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -867,6 +869,13 @@ class BasicTextInputClientState extends State<BasicTextInputClient>
|
|||||||
liveTextInputEnabled
|
liveTextInputEnabled
|
||||||
? () => _startLiveTextInput(SelectionChangedCause.toolbar)
|
? () => _startLiveTextInput(SelectionChangedCause.toolbar)
|
||||||
: null,
|
: null,
|
||||||
|
searchWebEnabled
|
||||||
|
? () =>
|
||||||
|
_searchWebForSelection(SelectionChangedCause.toolbar)
|
||||||
|
: null,
|
||||||
|
shareEnabled
|
||||||
|
? () => _shareSelection(SelectionChangedCause.toolbar)
|
||||||
|
: null,
|
||||||
_contextMenuAnchors,
|
_contextMenuAnchors,
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@@ -1016,6 +1025,65 @@ class BasicTextInputClientState extends State<BasicTextInputClient>
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool get searchWebEnabled {
|
||||||
|
if (defaultTargetPlatform != TargetPlatform.iOS) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return !textEditingValue.selection.isCollapsed &&
|
||||||
|
textEditingValue.selection.textInside(textEditingValue.text).trim() !=
|
||||||
|
'';
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Launch a web search on the current selection,
|
||||||
|
/// as in the "Search Web" edit menu button on iOS.
|
||||||
|
///
|
||||||
|
/// Currently this is only implemented for iOS.
|
||||||
|
Future<void> _searchWebForSelection(SelectionChangedCause cause) async {
|
||||||
|
final String text =
|
||||||
|
textEditingValue.selection.textInside(textEditingValue.text);
|
||||||
|
if (text.isNotEmpty) {
|
||||||
|
await SystemChannels.platform.invokeMethod(
|
||||||
|
'SearchWeb.invoke',
|
||||||
|
text,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool get shareEnabled {
|
||||||
|
switch (defaultTargetPlatform) {
|
||||||
|
case TargetPlatform.android:
|
||||||
|
case TargetPlatform.iOS:
|
||||||
|
return !textEditingValue.selection.isCollapsed &&
|
||||||
|
textEditingValue.selection
|
||||||
|
.textInside(textEditingValue.text)
|
||||||
|
.trim() !=
|
||||||
|
'';
|
||||||
|
case TargetPlatform.macOS:
|
||||||
|
case TargetPlatform.fuchsia:
|
||||||
|
case TargetPlatform.linux:
|
||||||
|
case TargetPlatform.windows:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Launch the share interface for the current selection,
|
||||||
|
/// as in the "Share..." edit menu button on iOS.
|
||||||
|
///
|
||||||
|
/// Currently this is only implemented for iOS and Android.
|
||||||
|
Future<void> _shareSelection(SelectionChangedCause cause) async {
|
||||||
|
final String text =
|
||||||
|
textEditingValue.selection.textInside(textEditingValue.text);
|
||||||
|
if (text.isNotEmpty) {
|
||||||
|
await SystemChannels.platform.invokeMethod(
|
||||||
|
'Share.invoke',
|
||||||
|
text,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Actions(
|
return Actions(
|
||||||
|
|||||||
@@ -55,7 +55,8 @@ declare -ar PROJECT_NAMES=(
|
|||||||
"provider_shopper"
|
"provider_shopper"
|
||||||
"simple_shader"
|
"simple_shader"
|
||||||
"simplistic_calculator"
|
"simplistic_calculator"
|
||||||
"simplistic_editor"
|
# TODO(DomesticMouse): The method 'isSelectionWithinTextBounds' isn't defined for the type 'TextEditingController'
|
||||||
|
# "simplistic_editor"
|
||||||
"testing_app"
|
"testing_app"
|
||||||
"veggieseasons"
|
"veggieseasons"
|
||||||
"web_embedding/element_embedding_demo"
|
"web_embedding/element_embedding_demo"
|
||||||
|
|||||||
Reference in New Issue
Block a user