Skip to content

Commit

Permalink
Allow for custom context menu (singerdmx#1320)
Browse files Browse the repository at this point in the history
* Persist boolean logic for showSelectionToolbar

* Add test for custom context menu

* Adjust test to reflect custom context menu implementation

---------

Co-authored-by: operatorultra <[email protected]>
  • Loading branch information
Junoburger and operatorultra authored Jul 21, 2023
1 parent a363d9b commit 1557a5d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/src/widgets/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ class QuillEditor extends StatefulWidget {
this.customLinkPrefixes = const <String>[],
this.dialogTheme,
this.contentInsertionConfiguration,
this.contextMenuBuilder,
Key? key,
}) : super(key: key);

Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -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(
Expand Down
51 changes: 51 additions & 0 deletions test/widgets/editor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
});
});
}

0 comments on commit 1557a5d

Please sign in to comment.