mirror of
https://github.com/flutter/samples.git
synced 2025-11-09 06:18:49 +00:00
Custom context menu example (#1463)
* flutter create * Copied in from 'contextual-menu-examples' repo * Works again with the latest updates to the branch * Clean up anywhere example * Clean up other examples * Add to CI * Updated with release version of context menus, and added platform switcher * Generated files * Home icon on your original platform * Remove web_dashboard from ci, not sure why that change was there... * Add context_menu to beta channel, but commented out, for the future * +x permissions on the master script, which I may have accidentally changed before? * Actual bash comment * dart format * Natural default platform' * A working test, for the email page * Import order fix * Test some pages * More tests * dart format
This commit is contained in:
committed by
GitHub
parent
070ce7303a
commit
41571eae07
98
experimental/context_menus/lib/modified_action_page.dart
Normal file
98
experimental/context_menus/lib/modified_action_page.dart
Normal file
@@ -0,0 +1,98 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import 'constants.dart';
|
||||
import 'platform_selector.dart';
|
||||
|
||||
class ModifiedActionPage extends StatelessWidget {
|
||||
ModifiedActionPage({
|
||||
Key? key,
|
||||
required this.onChangedPlatform,
|
||||
}) : super(key: key);
|
||||
|
||||
static const String route = 'modified-action';
|
||||
static const String title = 'Modified Action';
|
||||
static const String subtitle =
|
||||
'The copy button copies but also shows a menu.';
|
||||
static const String url = '$kCodeUrl/modified_action_page.dart';
|
||||
|
||||
final PlatformCallback onChangedPlatform;
|
||||
|
||||
final TextEditingController _controller = TextEditingController(
|
||||
text: 'Try using the copy button.',
|
||||
);
|
||||
|
||||
DialogRoute _showDialog(BuildContext context) {
|
||||
return DialogRoute<void>(
|
||||
context: context,
|
||||
builder: (BuildContext context) => const AlertDialog(
|
||||
title: Text('Copied, but also showed this dialog.')),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text(ModifiedActionPage.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: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
const Text(
|
||||
'This example shows adding to the behavior of a default button.',
|
||||
),
|
||||
const SizedBox(
|
||||
height: 30.0,
|
||||
),
|
||||
TextField(
|
||||
controller: _controller,
|
||||
contextMenuBuilder: (BuildContext context,
|
||||
EditableTextState editableTextState) {
|
||||
final List<ContextMenuButtonItem> buttonItems =
|
||||
editableTextState.contextMenuButtonItems;
|
||||
// Modify the copy buttonItem to show a dialog after copying.
|
||||
final int copyButtonIndex = buttonItems.indexWhere(
|
||||
(ContextMenuButtonItem buttonItem) {
|
||||
return buttonItem.type == ContextMenuButtonType.copy;
|
||||
},
|
||||
);
|
||||
if (copyButtonIndex >= 0) {
|
||||
final ContextMenuButtonItem copyButtonItem =
|
||||
buttonItems[copyButtonIndex];
|
||||
buttonItems[copyButtonIndex] = copyButtonItem.copyWith(
|
||||
onPressed: () {
|
||||
copyButtonItem.onPressed();
|
||||
Navigator.of(context).push(_showDialog(context));
|
||||
},
|
||||
);
|
||||
}
|
||||
return AdaptiveTextSelectionToolbar.buttonItems(
|
||||
anchors: editableTextState.contextMenuAnchors,
|
||||
buttonItems: buttonItems,
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user