mirror of https://github.com/flutter/samples.git
Another context menus example: custom menu (#1577)
parent
56c01c31be
commit
6bd2d930bb
@ -0,0 +1,97 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:url_launcher/url_launcher.dart';
|
||||||
|
|
||||||
|
import 'constants.dart';
|
||||||
|
import 'platform_selector.dart';
|
||||||
|
|
||||||
|
class CustomMenuPage extends StatelessWidget {
|
||||||
|
CustomMenuPage({
|
||||||
|
Key? key,
|
||||||
|
required this.onChangedPlatform,
|
||||||
|
}) : super(key: key);
|
||||||
|
|
||||||
|
static const String route = 'custom-menu';
|
||||||
|
static const String title = 'Custom Menu';
|
||||||
|
static const String subtitle =
|
||||||
|
'A custom menu built from scratch, but using the default buttons.';
|
||||||
|
|
||||||
|
final PlatformCallback onChangedPlatform;
|
||||||
|
|
||||||
|
final TextEditingController _controller = TextEditingController(
|
||||||
|
text: 'Show the menu to see a custom menu with the default buttons.',
|
||||||
|
);
|
||||||
|
|
||||||
|
static const String url = '$kCodeUrl/custom_menu_page.dart';
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: const Text(CustomMenuPage.title),
|
||||||
|
actions: <Widget>[
|
||||||
|
PlatformSelector(
|
||||||
|
onChangedPlatform: onChangedPlatform,
|
||||||
|
),
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(Icons.code),
|
||||||
|
onPressed: () async {
|
||||||
|
if (!await launchUrl(Uri.parse(url))) {
|
||||||
|
throw 'Could not launch $url';
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
body: Center(
|
||||||
|
child: SizedBox(
|
||||||
|
width: 300.0,
|
||||||
|
child: TextField(
|
||||||
|
controller: _controller,
|
||||||
|
maxLines: 4,
|
||||||
|
minLines: 2,
|
||||||
|
contextMenuBuilder:
|
||||||
|
(BuildContext context, EditableTextState editableTextState) {
|
||||||
|
return _MyContextMenu(
|
||||||
|
anchor: editableTextState.contextMenuAnchors.primaryAnchor,
|
||||||
|
children: AdaptiveTextSelectionToolbar.getAdaptiveButtons(
|
||||||
|
context,
|
||||||
|
editableTextState.contextMenuButtonItems,
|
||||||
|
).toList(),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _MyContextMenu extends StatelessWidget {
|
||||||
|
const _MyContextMenu({
|
||||||
|
required this.anchor,
|
||||||
|
required this.children,
|
||||||
|
});
|
||||||
|
|
||||||
|
final Offset anchor;
|
||||||
|
final List<Widget> children;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Stack(
|
||||||
|
children: <Widget>[
|
||||||
|
Positioned(
|
||||||
|
top: anchor.dy,
|
||||||
|
left: anchor.dx,
|
||||||
|
child: Container(
|
||||||
|
width: 200.0,
|
||||||
|
height: 200.0,
|
||||||
|
color: Colors.amberAccent,
|
||||||
|
child: Column(
|
||||||
|
children: children,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
import 'package:flutter/cupertino.dart';
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:flutter/gestures.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
|
||||||
|
import 'package:context_menus/main.dart';
|
||||||
|
import 'package:context_menus/custom_menu_page.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
testWidgets('Shows default buttons in a custom context menu',
|
||||||
|
(WidgetTester tester) async {
|
||||||
|
await tester.pumpWidget(const MyApp());
|
||||||
|
|
||||||
|
// Navigate to the CustomMenuPage example.
|
||||||
|
await tester.dragUntilVisible(
|
||||||
|
find.text(CustomMenuPage.title),
|
||||||
|
find.byType(ListView),
|
||||||
|
const Offset(0.0, -200.0),
|
||||||
|
);
|
||||||
|
await tester.tap(find.text(CustomMenuPage.title));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// Right click on the text field to show the context menu.
|
||||||
|
final TestGesture gesture = await tester.startGesture(
|
||||||
|
tester.getCenter(find.byType(EditableText)),
|
||||||
|
kind: PointerDeviceKind.mouse,
|
||||||
|
buttons: kSecondaryMouseButton,
|
||||||
|
);
|
||||||
|
await tester.pump();
|
||||||
|
await gesture.up();
|
||||||
|
await gesture.removePointer();
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// A custom context menu is shown, and the buttons are the default ones.
|
||||||
|
expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing);
|
||||||
|
expect(find.byType(CupertinoAdaptiveTextSelectionToolbar), findsNothing);
|
||||||
|
switch (defaultTargetPlatform) {
|
||||||
|
case TargetPlatform.iOS:
|
||||||
|
expect(
|
||||||
|
find.byType(CupertinoTextSelectionToolbarButton), findsNWidgets(2));
|
||||||
|
break;
|
||||||
|
case TargetPlatform.macOS:
|
||||||
|
expect(find.byType(CupertinoDesktopTextSelectionToolbarButton),
|
||||||
|
findsNWidgets(2));
|
||||||
|
break;
|
||||||
|
case TargetPlatform.android:
|
||||||
|
case TargetPlatform.fuchsia:
|
||||||
|
expect(find.byType(TextSelectionToolbarTextButton), findsNWidgets(1));
|
||||||
|
break;
|
||||||
|
case TargetPlatform.linux:
|
||||||
|
case TargetPlatform.windows:
|
||||||
|
expect(
|
||||||
|
find.byType(DesktopTextSelectionToolbarButton), findsNWidgets(1));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in new issue