Allow for custom context menu (#1320)

* Persist boolean logic for showSelectionToolbar

* Add test for custom context menu

* Adjust test to reflect custom context menu implementation

---------

Co-authored-by: operatorultra <105389680+operatorultra@users.noreply.github.com>
pull/1321/head
Juno Burger 2 years ago committed by GitHub
parent a363d9b62d
commit 1557a5de1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      lib/src/widgets/editor.dart
  2. 51
      test/widgets/editor_test.dart

@ -191,6 +191,7 @@ class QuillEditor extends StatefulWidget {
this.customLinkPrefixes = const <String>[],
this.dialogTheme,
this.contentInsertionConfiguration,
this.contextMenuBuilder,
Key? key,
}) : super(key: key);
@ -432,6 +433,9 @@ class QuillEditor extends StatefulWidget {
/// Configures the dialog theme.
final QuillDialogTheme? dialogTheme;
// Allows for creating a custom context menu
final QuillEditorContextMenuBuilder? contextMenuBuilder;
/// Configuration of handler for media content inserted via the system input
/// method.
///
@ -503,8 +507,9 @@ class QuillEditorState extends State<QuillEditor>
readOnly: widget.readOnly,
placeholder: widget.placeholder,
onLaunchUrl: widget.onLaunchUrl,
contextMenuBuilder:
showSelectionToolbar ? RawEditor.defaultContextMenuBuilder : null,
contextMenuBuilder: showSelectionToolbar
? (widget.contextMenuBuilder ?? RawEditor.defaultContextMenuBuilder)
: null,
showSelectionHandles: isMobile(theme.platform),
showCursor: widget.showCursor,
cursorStyle: CursorStyle(

@ -4,10 +4,12 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_quill/flutter_quill.dart';
import 'package:flutter_quill/flutter_quill_test.dart';
import 'package:flutter_quill/src/widgets/raw_editor.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
late QuillController controller;
var didCopy = false;
setUp(() {
controller = QuillController.basic();
@ -78,5 +80,54 @@ void main() {
expect(error, isNull);
expect(latestUri, equals(uri));
});
Widget customBuilder(BuildContext context, RawEditorState state) {
return AdaptiveTextSelectionToolbar(
anchors: state.contextMenuAnchors,
children: [
Container(
height: 50,
color: Colors.white,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(
onPressed: () {
didCopy = true;
},
icon: const Icon(Icons.copy),
),
],
),
),
],
);
}
testWidgets('custom context menu builder', (tester) async {
await tester.pumpWidget(MaterialApp(
home: QuillEditor(
controller: controller,
focusNode: FocusNode(),
scrollController: ScrollController(),
scrollable: true,
padding: EdgeInsets.zero,
autoFocus: true,
readOnly: false,
expands: true,
contextMenuBuilder: customBuilder,
),
));
// Long press to show menu
await tester.longPress(find.byType(QuillEditor));
await tester.pumpAndSettle();
// Verify custom widget shows
expect(find.byIcon(Icons.copy), findsOneWidget);
await tester.tap(find.byIcon(Icons.copy));
expect(didCopy, isTrue);
});
});
}

Loading…
Cancel
Save