-
Notifications
You must be signed in to change notification settings - Fork 26.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It's now possible to use the native-rendered text selection context menu on iOS. This sacrifices customizability in exchange for avoiding showing a notification when the user presses "Paste". It's off by default, but to enable, see the example system_context_menu.0.dart.
- Loading branch information
Showing
13 changed files
with
1,321 additions
and
43 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
examples/api/lib/widgets/system_context_menu/system_context_menu.0.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// 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/material.dart'; | ||
|
||
/// Flutter code sample for [SystemContextMenu]. | ||
void main() => runApp(const SystemContextMenuExampleApp()); | ||
|
||
class SystemContextMenuExampleApp extends StatelessWidget { | ||
const SystemContextMenuExampleApp({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
home: Scaffold( | ||
appBar: AppBar( | ||
title: const Text('SystemContextMenu Basic Example'), | ||
), | ||
body: Center( | ||
child: TextField( | ||
contextMenuBuilder: (BuildContext context, EditableTextState editableTextState) { | ||
// If supported, show the system context menu. | ||
if (SystemContextMenu.isSupported(context)) { | ||
return SystemContextMenu.editableText( | ||
editableTextState: editableTextState, | ||
); | ||
} | ||
// Otherwise, show the flutter-rendered context menu for the current | ||
// platform. | ||
return AdaptiveTextSelectionToolbar.editableText( | ||
editableTextState: editableTextState, | ||
); | ||
}, | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
examples/api/test/widgets/system_context_menu/system_context_menu.0_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// 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/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_api_samples/widgets/system_context_menu/system_context_menu.0.dart' as example; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
testWidgets('only shows the system context menu on iOS when MediaQuery says it is supported', (WidgetTester tester) async { | ||
await tester.pumpWidget( | ||
Builder( | ||
builder: (BuildContext context) { | ||
final MediaQueryData mediaQueryData = MediaQuery.of(context); | ||
return MediaQuery( | ||
data: mediaQueryData.copyWith( | ||
// Faking this value, which is usually set to true only on | ||
// devices running iOS 16+. | ||
supportsShowingSystemContextMenu: defaultTargetPlatform == TargetPlatform.iOS, | ||
), | ||
child: const example.SystemContextMenuExampleApp(), | ||
); | ||
}, | ||
), | ||
); | ||
|
||
expect(find.byType(SystemContextMenu), findsNothing); | ||
|
||
// Show the context menu. | ||
final Finder textFinder = find.byType(EditableText); | ||
await tester.longPress(textFinder); | ||
tester.state<EditableTextState>(textFinder).showToolbar(); | ||
await tester.pumpAndSettle(); | ||
|
||
switch (defaultTargetPlatform) { | ||
case TargetPlatform.iOS: | ||
expect(find.byType(SystemContextMenu), findsOneWidget); | ||
expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing); | ||
case TargetPlatform.android: | ||
case TargetPlatform.fuchsia: | ||
case TargetPlatform.linux: | ||
case TargetPlatform.macOS: | ||
case TargetPlatform.windows: | ||
expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget); | ||
expect(find.byType(SystemContextMenu), findsNothing); | ||
} | ||
}, variant: TargetPlatformVariant.all(), skip: kIsWeb); // [intended] | ||
|
||
testWidgets('does not show the system context menu when not supported', (WidgetTester tester) async { | ||
await tester.pumpWidget( | ||
// By default, MediaQueryData.supportsShowingSystemContextMenu is false. | ||
const example.SystemContextMenuExampleApp(), | ||
); | ||
|
||
expect(find.byType(SystemContextMenu), findsNothing); | ||
|
||
// Show the context menu. | ||
final Finder textFinder = find.byType(EditableText); | ||
await tester.longPress(textFinder); | ||
tester.state<EditableTextState>(textFinder).showToolbar(); | ||
await tester.pumpAndSettle(); | ||
|
||
expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget); | ||
expect(find.byType(SystemContextMenu), findsNothing); | ||
}, variant: TargetPlatformVariant.all(), skip: kIsWeb); // [intended] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.