builder.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import 'package:flutter/gestures.dart';
  2. import 'package:flutter/material.dart';
  3. import '../widget/raw_editor.dart';
  4. import '../widget/selection.dart';
  5. import '../rendering/editor.dart';
  6. /* ----------------------- Selection Gesture Detector ----------------------- */
  7. abstract class EditorTextSelectionGestureDetectorBuilderDelegate {
  8. GlobalKey<EditorState> getEditableTextKey();
  9. bool getForcePressEnabled();
  10. bool getSelectionEnabled();
  11. }
  12. class EditorTextSelectionGestureDetectorBuilder {
  13. EditorTextSelectionGestureDetectorBuilder(this.delegate);
  14. final EditorTextSelectionGestureDetectorBuilderDelegate delegate;
  15. bool shouldShowSelectionToolbar = true;
  16. EditorState? getEditor() {
  17. return delegate.getEditableTextKey().currentState;
  18. }
  19. RenderEditor? getRenderEditor() {
  20. return getEditor()!.getRenderEditor();
  21. }
  22. void onForcePressStart(ForcePressDetails details) {
  23. assert(delegate.getForcePressEnabled());
  24. shouldShowSelectionToolbar = true;
  25. if (delegate.getSelectionEnabled()) {
  26. getRenderEditor()!.selectWordsInRange(
  27. details.globalPosition,
  28. null,
  29. SelectionChangedCause.forcePress,
  30. );
  31. }
  32. }
  33. void onForcePressEnd(ForcePressDetails details) {
  34. assert(delegate.getForcePressEnabled());
  35. getRenderEditor()!.selectWordsInRange(
  36. details.globalPosition,
  37. null,
  38. SelectionChangedCause.forcePress,
  39. );
  40. if (shouldShowSelectionToolbar) {
  41. getEditor()!.showToolbar();
  42. }
  43. }
  44. void onTapDown(TapDownDetails details) {
  45. getRenderEditor()!.handleTapDown(details);
  46. final kind = details.kind;
  47. shouldShowSelectionToolbar = kind == null || kind == PointerDeviceKind.touch || kind == PointerDeviceKind.stylus;
  48. }
  49. void onTapUp(TapUpDetails details) {
  50. if (delegate.getSelectionEnabled()) {
  51. getRenderEditor()!.selectWordEdge(SelectionChangedCause.tap);
  52. }
  53. }
  54. void onTapCancel() {}
  55. void onLongPressStart(LongPressStartDetails details) {
  56. if (delegate.getSelectionEnabled()) {
  57. getRenderEditor()!.selectPositionAt(
  58. details.globalPosition,
  59. null,
  60. SelectionChangedCause.longPress,
  61. );
  62. }
  63. }
  64. void onLongPressMoveUpdate(LongPressMoveUpdateDetails details) {
  65. if (delegate.getSelectionEnabled()) {
  66. getRenderEditor()!.selectPositionAt(
  67. details.globalPosition,
  68. null,
  69. SelectionChangedCause.longPress,
  70. );
  71. }
  72. }
  73. void onLongPressEnd(LongPressEndDetails details) {
  74. if (shouldShowSelectionToolbar) {
  75. getEditor()!.showToolbar();
  76. }
  77. }
  78. void onDoubleTapDown(TapDownDetails details) {
  79. if (delegate.getSelectionEnabled()) {
  80. getRenderEditor()!.selectWord(SelectionChangedCause.tap);
  81. if (shouldShowSelectionToolbar) {
  82. getEditor()!.showToolbar();
  83. }
  84. }
  85. }
  86. void onDragSelectionStart(DragStartDetails details) {
  87. getRenderEditor()!.selectPositionAt(
  88. details.globalPosition,
  89. null,
  90. SelectionChangedCause.drag,
  91. );
  92. }
  93. void onDragSelectionUpdate(DragStartDetails startDetails, DragUpdateDetails updateDetails) {
  94. getRenderEditor()!.selectPositionAt(
  95. startDetails.globalPosition,
  96. updateDetails.globalPosition,
  97. SelectionChangedCause.drag,
  98. );
  99. }
  100. void onDragSelectionEnd(DragEndDetails details) {}
  101. Widget build(HitTestBehavior behavior, Widget child) {
  102. return EditorTextSelectionGestureDetector(
  103. onTapUp: onTapUp,
  104. onTapDown: onTapDown,
  105. onTapCancel: onTapCancel,
  106. onForcePressStart: delegate.getForcePressEnabled() ? onForcePressStart : null,
  107. onForcePressEnd: delegate.getForcePressEnabled() ? onForcePressEnd : null,
  108. onLongPressStart: onLongPressStart,
  109. onLongPressMoveUpdate: onLongPressMoveUpdate,
  110. onLongPressEnd: onLongPressEnd,
  111. onDoubleTapDown: onDoubleTapDown,
  112. onDragSelectionStart: onDragSelectionStart,
  113. onDragSelectionUpdate: onDragSelectionUpdate,
  114. onDragSelectionEnd: onDragSelectionEnd,
  115. behavior: behavior,
  116. child: child,
  117. );
  118. }
  119. }