selectable_text.dart 24.1 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
8
import 'package:flutter/rendering.dart';
9

10
import 'desktop_text_selection.dart';
11 12
import 'feedback.dart';
import 'text_selection.dart';
13
import 'text_selection_theme.dart';
14 15 16 17 18 19 20 21 22 23 24
import 'theme.dart';

/// An eyeballed value that moves the cursor slightly left of where it is
/// rendered for text on Android so its positioning more accurately matches the
/// native iOS text cursor positioning.
///
/// This value is in device pixels, not logical pixels as is typically used
/// throughout the codebase.
const int iOSHorizontalOffset = -2;

class _TextSpanEditingController extends TextEditingController {
25
  _TextSpanEditingController({required TextSpan textSpan}):
26 27
    assert(textSpan != null),
    _textSpan = textSpan,
28
    super(text: textSpan.toPlainText(includeSemanticsLabels: false));
29 30 31 32

  final TextSpan _textSpan;

  @override
33
  TextSpan buildTextSpan({required BuildContext context, TextStyle? style, required bool withComposing}) {
34
    // This does not care about composing.
35 36 37 38 39 40 41
    return TextSpan(
      style: style,
      children: <TextSpan>[_textSpan],
    );
  }

  @override
42
  set text(String? newText) {
43 44
    // This should never be reached.
    throw UnimplementedError();
45 46 47 48 49
  }
}

class _SelectableTextSelectionGestureDetectorBuilder extends TextSelectionGestureDetectorBuilder {
  _SelectableTextSelectionGestureDetectorBuilder({
50
    required _SelectableTextState state,
51
  }) : _state = state,
52
       super(delegate: state);
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

  final _SelectableTextState _state;

  @override
  void onForcePressStart(ForcePressDetails details) {
    super.onForcePressStart(details);
    if (delegate.selectionEnabled && shouldShowSelectionToolbar) {
      editableText.showToolbar();
    }
  }

  @override
  void onForcePressEnd(ForcePressDetails details) {
    // Not required.
  }

  @override
  void onSingleLongTapMoveUpdate(LongPressMoveUpdateDetails details) {
    if (delegate.selectionEnabled) {
72 73 74 75 76
      renderEditable.selectWordsInRange(
        from: details.globalPosition - details.offsetFromOrigin,
        to: details.globalPosition,
        cause: SelectionChangedCause.longPress,
      );
77 78 79 80 81 82 83
    }
  }

  @override
  void onSingleTapUp(TapUpDetails details) {
    editableText.hideToolbar();
    if (delegate.selectionEnabled) {
84
      switch (Theme.of(_state.context).platform) {
85
        case TargetPlatform.iOS:
86
        case TargetPlatform.macOS:
87 88 89 90
          renderEditable.selectWordEdge(cause: SelectionChangedCause.tap);
          break;
        case TargetPlatform.android:
        case TargetPlatform.fuchsia:
91 92
        case TargetPlatform.linux:
        case TargetPlatform.windows:
93 94 95 96
          renderEditable.selectPosition(cause: SelectionChangedCause.tap);
          break;
      }
    }
97
    _state.widget.onTap?.call();
98 99 100 101 102
  }

  @override
  void onSingleLongTapStart(LongPressStartDetails details) {
    if (delegate.selectionEnabled) {
103 104
      renderEditable.selectWord(cause: SelectionChangedCause.longPress);
      Feedback.forLongPress(_state.context);
105 106 107 108 109 110 111 112 113 114
    }
  }
}

/// A run of selectable text with a single style.
///
/// The [SelectableText] widget displays a string of text with a single style.
/// The string might break across multiple lines or might all be displayed on
/// the same line depending on the layout constraints.
///
115 116
/// {@youtube 560 315 https://www.youtube.com/watch?v=ZSU3ZXOs6hc}
///
117 118 119 120 121 122 123
/// The [style] argument is optional. When omitted, the text will use the style
/// from the closest enclosing [DefaultTextStyle]. If the given style's
/// [TextStyle.inherit] property is true (the default), the given style will
/// be merged with the closest enclosing [DefaultTextStyle]. This merging
/// behavior is useful, for example, to make the text bold while using the
/// default font family and size.
///
124
/// {@tool snippet}
125 126
///
/// ```dart
127
/// const SelectableText(
128 129 130 131 132 133 134 135 136 137 138 139
///   'Hello! How are you?',
///   textAlign: TextAlign.center,
///   style: TextStyle(fontWeight: FontWeight.bold),
/// )
/// ```
/// {@end-tool}
///
/// Using the [SelectableText.rich] constructor, the [SelectableText] widget can
/// display a paragraph with differently styled [TextSpan]s. The sample
/// that follows displays "Hello beautiful world" with different styles
/// for each word.
///
140
/// {@tool snippet}
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
///
/// ```dart
/// const SelectableText.rich(
///   TextSpan(
///     text: 'Hello', // default text style
///     children: <TextSpan>[
///       TextSpan(text: ' beautiful ', style: TextStyle(fontStyle: FontStyle.italic)),
///       TextSpan(text: 'world', style: TextStyle(fontWeight: FontWeight.bold)),
///     ],
///   ),
/// )
/// ```
/// {@end-tool}
///
/// ## Interactivity
///
/// To make [SelectableText] react to touch events, use callback [onTap] to achieve
/// the desired behavior.
///
/// See also:
///
///  * [Text], which is the non selectable version of this widget.
///  * [TextField], which is the editable version of this widget.
class SelectableText extends StatefulWidget {
  /// Creates a selectable text widget.
  ///
  /// If the [style] argument is null, the text will use the style from the
  /// closest enclosing [DefaultTextStyle].
  ///
170 171 172 173

  /// The [showCursor], [autofocus], [dragStartBehavior], and [data] parameters
  /// must not be null. If specified, the [maxLines] argument must be greater
  /// than zero.
174
  const SelectableText(
175
    String this.data, {
176
    Key? key,
177 178 179 180 181
    this.focusNode,
    this.style,
    this.strutStyle,
    this.textAlign,
    this.textDirection,
182
    this.textScaleFactor,
183 184
    this.showCursor = false,
    this.autofocus = false,
185
    ToolbarOptions? toolbarOptions,
186
    this.minLines,
187 188
    this.maxLines,
    this.cursorWidth = 2.0,
189
    this.cursorHeight,
190 191 192 193
    this.cursorRadius,
    this.cursorColor,
    this.dragStartBehavior = DragStartBehavior.start,
    this.enableInteractiveSelection = true,
194
    this.selectionControls,
195 196
    this.onTap,
    this.scrollPhysics,
197
    this.textHeightBehavior,
198
    this.textWidthBasis,
199
    this.onSelectionChanged,
200 201 202 203
  }) :  assert(showCursor != null),
        assert(autofocus != null),
        assert(dragStartBehavior != null),
        assert(maxLines == null || maxLines > 0),
204 205 206
        assert(minLines == null || minLines > 0),
        assert(
          (maxLines == null) || (minLines == null) || (maxLines >= minLines),
207
          "minLines can't be greater than maxLines",
208
        ),
209 210 211 212 213
        assert(
          data != null,
          'A non-null String must be provided to a SelectableText widget.',
        ),
        textSpan = null,
214 215 216 217 218
        toolbarOptions = toolbarOptions ??
          const ToolbarOptions(
            selectAll: true,
            copy: true,
          ),
219 220 221 222 223 224
        super(key: key);

  /// Creates a selectable text widget with a [TextSpan].
  ///
  /// The [textSpan] parameter must not be null and only contain [TextSpan] in
  /// [textSpan.children]. Other type of [InlineSpan] is not allowed.
225 226
  ///
  /// The [autofocus] and [dragStartBehavior] arguments must not be null.
227
  const SelectableText.rich(
228
    TextSpan this.textSpan, {
229
    Key? key,
230 231 232 233 234
    this.focusNode,
    this.style,
    this.strutStyle,
    this.textAlign,
    this.textDirection,
235
    this.textScaleFactor,
236 237
    this.showCursor = false,
    this.autofocus = false,
238
    ToolbarOptions? toolbarOptions,
239
    this.minLines,
240 241
    this.maxLines,
    this.cursorWidth = 2.0,
242
    this.cursorHeight,
243 244 245 246
    this.cursorRadius,
    this.cursorColor,
    this.dragStartBehavior = DragStartBehavior.start,
    this.enableInteractiveSelection = true,
247
    this.selectionControls,
248 249
    this.onTap,
    this.scrollPhysics,
250
    this.textHeightBehavior,
251
    this.textWidthBasis,
252
    this.onSelectionChanged,
253 254 255 256
  }) :  assert(showCursor != null),
    assert(autofocus != null),
    assert(dragStartBehavior != null),
    assert(maxLines == null || maxLines > 0),
257 258 259
    assert(minLines == null || minLines > 0),
    assert(
      (maxLines == null) || (minLines == null) || (maxLines >= minLines),
260
      "minLines can't be greater than maxLines",
261
    ),
262 263 264 265 266
    assert(
      textSpan != null,
      'A non-null TextSpan must be provided to a SelectableText.rich widget.',
    ),
    data = null,
267 268 269 270 271
    toolbarOptions = toolbarOptions ??
      const ToolbarOptions(
        selectAll: true,
        copy: true,
      ),
272 273 274 275 276
    super(key: key);

  /// The text to display.
  ///
  /// This will be null if a [textSpan] is provided instead.
277
  final String? data;
278 279 280 281

  /// The text to display as a [TextSpan].
  ///
  /// This will be null if [data] is provided instead.
282
  final TextSpan? textSpan;
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

  /// Defines the focus for this widget.
  ///
  /// Text is only selectable when widget is focused.
  ///
  /// The [focusNode] is a long-lived object that's typically managed by a
  /// [StatefulWidget] parent. See [FocusNode] for more information.
  ///
  /// To give the focus to this widget, provide a [focusNode] and then
  /// use the current [FocusScope] to request the focus:
  ///
  /// ```dart
  /// FocusScope.of(context).requestFocus(myFocusNode);
  /// ```
  ///
  /// This happens automatically when the widget is tapped.
  ///
  /// To be notified when the widget gains or loses the focus, add a listener
  /// to the [focusNode]:
  ///
  /// ```dart
  /// focusNode.addListener(() { print(myFocusNode.hasFocus); });
  /// ```
  ///
307 308 309
  /// If null, this widget will create its own [FocusNode] with
  /// [FocusNode.skipTraversal] parameter set to `true`, which causes the widget
  /// to be skipped over during focus traversal.
310
  final FocusNode? focusNode;
311 312 313 314

  /// The style to use for the text.
  ///
  /// If null, defaults [DefaultTextStyle] of context.
315
  final TextStyle? style;
316 317

  /// {@macro flutter.widgets.editableText.strutStyle}
318
  final StrutStyle? strutStyle;
319 320

  /// {@macro flutter.widgets.editableText.textAlign}
321
  final TextAlign? textAlign;
322 323

  /// {@macro flutter.widgets.editableText.textDirection}
324
  final TextDirection? textDirection;
325

326
  /// {@macro flutter.widgets.editableText.textScaleFactor}
327
  final double? textScaleFactor;
328

329 330 331
  /// {@macro flutter.widgets.editableText.autofocus}
  final bool autofocus;

332
  /// {@macro flutter.widgets.editableText.minLines}
333
  final int? minLines;
334

335
  /// {@macro flutter.widgets.editableText.maxLines}
336
  final int? maxLines;
337 338 339 340 341 342 343

  /// {@macro flutter.widgets.editableText.showCursor}
  final bool showCursor;

  /// {@macro flutter.widgets.editableText.cursorWidth}
  final double cursorWidth;

344
  /// {@macro flutter.widgets.editableText.cursorHeight}
345
  final double? cursorHeight;
346

347
  /// {@macro flutter.widgets.editableText.cursorRadius}
348
  final Radius? cursorRadius;
349 350 351 352

  /// The color to use when painting the cursor.
  ///
  /// Defaults to the theme's `cursorColor` when null.
353
  final Color? cursorColor;
354 355 356 357

  /// {@macro flutter.widgets.editableText.enableInteractiveSelection}
  final bool enableInteractiveSelection;

358 359 360
  /// {@macro flutter.widgets.editableText.selectionControls}
  final TextSelectionControls? selectionControls;

361 362 363
  /// {@macro flutter.widgets.scrollable.dragStartBehavior}
  final DragStartBehavior dragStartBehavior;

364 365 366 367 368 369 370
  /// Configuration of toolbar options.
  ///
  /// Paste and cut will be disabled regardless.
  ///
  /// If not set, select all and copy will be enabled by default.
  final ToolbarOptions toolbarOptions;

371 372
  /// {@macro flutter.widgets.editableText.selectionEnabled}
  bool get selectionEnabled => enableInteractiveSelection;
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

  /// Called when the user taps on this selectable text.
  ///
  /// The selectable text builds a [GestureDetector] to handle input events like tap,
  /// to trigger focus requests, to move the caret, adjust the selection, etc.
  /// Handling some of those events by wrapping the selectable text with a competing
  /// GestureDetector is problematic.
  ///
  /// To unconditionally handle taps, without interfering with the selectable text's
  /// internal gesture detector, provide this callback.
  ///
  /// To be notified when the text field gains or loses the focus, provide a
  /// [focusNode] and add a listener to that.
  ///
  /// To listen to arbitrary pointer events without competing with the
  /// selectable text's internal gesture detector, use a [Listener].
389
  final GestureTapCallback? onTap;
390

Dan Field's avatar
Dan Field committed
391
  /// {@macro flutter.widgets.editableText.scrollPhysics}
392
  final ScrollPhysics? scrollPhysics;
393

394
  /// {@macro flutter.dart:ui.textHeightBehavior}
395
  final TextHeightBehavior? textHeightBehavior;
396

397
  /// {@macro flutter.painting.textPainter.textWidthBasis}
398
  final TextWidthBasis? textWidthBasis;
399

400
  /// {@macro flutter.widgets.editableText.onSelectionChanged}
401
  final SelectionChangedCallback? onSelectionChanged;
402

403
  @override
404
  State<SelectableText> createState() => _SelectableTextState();
405 406 407 408 409 410 411 412 413

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(DiagnosticsProperty<String>('data', data, defaultValue: null));
    properties.add(DiagnosticsProperty<FocusNode>('focusNode', focusNode, defaultValue: null));
    properties.add(DiagnosticsProperty<TextStyle>('style', style, defaultValue: null));
    properties.add(DiagnosticsProperty<bool>('autofocus', autofocus, defaultValue: false));
    properties.add(DiagnosticsProperty<bool>('showCursor', showCursor, defaultValue: false));
414
    properties.add(IntProperty('minLines', minLines, defaultValue: null));
415 416 417
    properties.add(IntProperty('maxLines', maxLines, defaultValue: null));
    properties.add(EnumProperty<TextAlign>('textAlign', textAlign, defaultValue: null));
    properties.add(EnumProperty<TextDirection>('textDirection', textDirection, defaultValue: null));
418
    properties.add(DoubleProperty('textScaleFactor', textScaleFactor, defaultValue: null));
419
    properties.add(DoubleProperty('cursorWidth', cursorWidth, defaultValue: 2.0));
420
    properties.add(DoubleProperty('cursorHeight', cursorHeight, defaultValue: null));
421 422 423
    properties.add(DiagnosticsProperty<Radius>('cursorRadius', cursorRadius, defaultValue: null));
    properties.add(DiagnosticsProperty<Color>('cursorColor', cursorColor, defaultValue: null));
    properties.add(FlagProperty('selectionEnabled', value: selectionEnabled, defaultValue: true, ifFalse: 'selection disabled'));
424
    properties.add(DiagnosticsProperty<TextSelectionControls>('selectionControls', selectionControls, defaultValue: null));
425
    properties.add(DiagnosticsProperty<ScrollPhysics>('scrollPhysics', scrollPhysics, defaultValue: null));
426
    properties.add(DiagnosticsProperty<TextHeightBehavior>('textHeightBehavior', textHeightBehavior, defaultValue: null));
427 428 429 430
  }
}

class _SelectableTextState extends State<SelectableText> with AutomaticKeepAliveClientMixin implements TextSelectionGestureDetectorBuilderDelegate {
431
  EditableTextState? get _editableText => editableTextKey.currentState;
432

433
  late _TextSpanEditingController _controller;
434

435
  FocusNode? _focusNode;
436 437
  FocusNode get _effectiveFocusNode =>
      widget.focusNode ?? (_focusNode ??= FocusNode(skipTraversal: true));
438 439 440

  bool _showSelectionHandles = false;

441
  late _SelectableTextSelectionGestureDetectorBuilder _selectionGestureDetectorBuilder;
442 443 444

  // API for TextSelectionGestureDetectorBuilderDelegate.
  @override
445
  late bool forcePressEnabled;
446 447 448 449 450 451 452 453 454 455 456 457 458

  @override
  final GlobalKey<EditableTextState> editableTextKey = GlobalKey<EditableTextState>();

  @override
  bool get selectionEnabled => widget.selectionEnabled;
  // End of API for TextSelectionGestureDetectorBuilderDelegate.

  @override
  void initState() {
    super.initState();
    _selectionGestureDetectorBuilder = _SelectableTextSelectionGestureDetectorBuilder(state: this);
    _controller = _TextSpanEditingController(
459
        textSpan: widget.textSpan ?? TextSpan(text: widget.data),
460
    );
461
    _controller.addListener(_onControllerChanged);
462 463 464 465 466 467
  }

  @override
  void didUpdateWidget(SelectableText oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.data != oldWidget.data || widget.textSpan != oldWidget.textSpan) {
468
      _controller.removeListener(_onControllerChanged);
469
      _controller = _TextSpanEditingController(
470
          textSpan: widget.textSpan ?? TextSpan(text: widget.data),
471
      );
472
      _controller.addListener(_onControllerChanged);
473 474
    }
    if (_effectiveFocusNode.hasFocus && _controller.selection.isCollapsed) {
475
      _showSelectionHandles = false;
476 477
    } else {
      _showSelectionHandles = true;
478 479 480 481 482 483
    }
  }

  @override
  void dispose() {
    _focusNode?.dispose();
484
    _controller.removeListener(_onControllerChanged);
485 486 487
    super.dispose();
  }

488 489 490 491 492 493 494 495 496 497 498
  void _onControllerChanged() {
    final bool showSelectionHandles = !_effectiveFocusNode.hasFocus
      || !_controller.selection.isCollapsed;
    if (showSelectionHandles == _showSelectionHandles) {
      return;
    }
    setState(() {
      _showSelectionHandles = showSelectionHandles;
    });
  }

499 500
  TextSelection? _lastSeenTextSelection;

501
  void _handleSelectionChanged(TextSelection selection, SelectionChangedCause? cause) {
502 503 504 505 506 507
    final bool willShowSelectionHandles = _shouldShowSelectionHandles(cause);
    if (willShowSelectionHandles != _showSelectionHandles) {
      setState(() {
        _showSelectionHandles = willShowSelectionHandles;
      });
    }
508 509 510
    // TODO(chunhtai): The selection may be the same. We should remove this
    // check once this is fixed https://github.com/flutter/flutter/issues/76349.
    if (widget.onSelectionChanged != null && _lastSeenTextSelection != selection) {
511
      widget.onSelectionChanged!(selection, cause);
512
    }
513
    _lastSeenTextSelection = selection;
514

515
    switch (Theme.of(context).platform) {
516
      case TargetPlatform.iOS:
517
      case TargetPlatform.macOS:
518 519 520 521 522 523
        if (cause == SelectionChangedCause.longPress) {
          _editableText?.bringIntoView(selection.base);
        }
        return;
      case TargetPlatform.android:
      case TargetPlatform.fuchsia:
524 525
      case TargetPlatform.linux:
      case TargetPlatform.windows:
526 527 528 529 530 531 532
      // Do nothing.
    }
  }

  /// Toggle the toolbar when a selection handle is tapped.
  void _handleSelectionHandleTapped() {
    if (_controller.selection.isCollapsed) {
533
      _editableText!.toggleToolbar();
534 535 536
    }
  }

537
  bool _shouldShowSelectionHandles(SelectionChangedCause? cause) {
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    // When the text field is activated by something that doesn't trigger the
    // selection overlay, we shouldn't show the handles either.
    if (!_selectionGestureDetectorBuilder.shouldShowSelectionToolbar)
      return false;

    if (_controller.selection.isCollapsed)
      return false;

    if (cause == SelectionChangedCause.keyboard)
      return false;

    if (cause == SelectionChangedCause.longPress)
      return true;

    if (_controller.text.isNotEmpty)
      return true;

    return false;
  }

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context); // See AutomaticKeepAliveClientMixin.
    assert(() {
      return _controller._textSpan.visitChildren((InlineSpan span) => span.runtimeType == TextSpan);
    }(), 'SelectableText only supports TextSpan; Other type of InlineSpan is not allowed');
    assert(debugCheckHasMediaQuery(context));
    assert(debugCheckHasDirectionality(context));
    assert(
570 571
      !(widget.style != null && widget.style!.inherit == false &&
          (widget.style!.fontSize == null || widget.style!.textBaseline == null)),
572 573 574
      'inherit false style must supply fontSize and textBaseline',
    );

575
    final ThemeData theme = Theme.of(context);
576
    final TextSelectionThemeData selectionTheme = TextSelectionTheme.of(context);
577 578
    final FocusNode focusNode = _effectiveFocusNode;

579
    TextSelectionControls? textSelectionControls =  widget.selectionControls;
580 581
    final bool paintCursorAboveText;
    final bool cursorOpacityAnimates;
582 583
    Offset? cursorOffset;
    Color? cursorColor = widget.cursorColor;
584
    final Color selectionColor;
585
    Radius? cursorRadius = widget.cursorRadius;
586

587
    switch (theme.platform) {
588
      case TargetPlatform.iOS:
589
        final CupertinoThemeData cupertinoTheme = CupertinoTheme.of(context);
590
        forcePressEnabled = true;
591
        textSelectionControls ??= cupertinoTextSelectionControls;
592 593
        paintCursorAboveText = true;
        cursorOpacityAnimates = true;
594 595
        cursorColor ??= selectionTheme.cursorColor ?? cupertinoTheme.primaryColor;
        selectionColor = selectionTheme.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40);
596
        cursorRadius ??= const Radius.circular(2.0);
597 598 599 600 601 602 603 604 605 606 607 608
        cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0);
        break;

      case TargetPlatform.macOS:
        final CupertinoThemeData cupertinoTheme = CupertinoTheme.of(context);
        forcePressEnabled = false;
        textSelectionControls ??= cupertinoDesktopTextSelectionControls;
        paintCursorAboveText = true;
        cursorOpacityAnimates = true;
        cursorColor ??= selectionTheme.cursorColor ?? cupertinoTheme.primaryColor;
        selectionColor = selectionTheme.selectionColor ?? cupertinoTheme.primaryColor.withOpacity(0.40);
        cursorRadius ??= const Radius.circular(2.0);
609
        cursorOffset = Offset(iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0);
610 611 612 613
        break;

      case TargetPlatform.android:
      case TargetPlatform.fuchsia:
614 615 616 617 618 619 620 621
        forcePressEnabled = false;
        textSelectionControls ??= materialTextSelectionControls;
        paintCursorAboveText = false;
        cursorOpacityAnimates = false;
        cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;
        selectionColor = selectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
        break;

622 623
      case TargetPlatform.linux:
      case TargetPlatform.windows:
624
        forcePressEnabled = false;
625
        textSelectionControls ??= desktopTextSelectionControls;
626 627
        paintCursorAboveText = false;
        cursorOpacityAnimates = false;
628 629
        cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;
        selectionColor = selectionTheme.selectionColor ?? theme.colorScheme.primary.withOpacity(0.40);
630 631 632 633
        break;
    }

    final DefaultTextStyle defaultTextStyle = DefaultTextStyle.of(context);
634 635
    TextStyle? effectiveTextStyle = widget.style;
    if (effectiveTextStyle == null || effectiveTextStyle.inherit)
636 637 638 639 640 641 642 643 644
      effectiveTextStyle = defaultTextStyle.style.merge(widget.style);
    if (MediaQuery.boldTextOverride(context))
      effectiveTextStyle = effectiveTextStyle.merge(const TextStyle(fontWeight: FontWeight.bold));
    final Widget child = RepaintBoundary(
      child: EditableText(
        key: editableTextKey,
        style: effectiveTextStyle,
        readOnly: true,
        textWidthBasis: widget.textWidthBasis ?? defaultTextStyle.textWidthBasis,
645
        textHeightBehavior: widget.textHeightBehavior ?? defaultTextStyle.textHeightBehavior,
646 647 648 649
        showSelectionHandles: _showSelectionHandles,
        showCursor: widget.showCursor,
        controller: _controller,
        focusNode: focusNode,
650
        strutStyle: widget.strutStyle ?? const StrutStyle(),
651 652
        textAlign: widget.textAlign ?? defaultTextStyle.textAlign ?? TextAlign.start,
        textDirection: widget.textDirection,
653
        textScaleFactor: widget.textScaleFactor,
654 655
        autofocus: widget.autofocus,
        forceLine: false,
656
        toolbarOptions: widget.toolbarOptions,
657
        minLines: widget.minLines,
658
        maxLines: widget.maxLines ?? defaultTextStyle.maxLines,
659
        selectionColor: selectionColor,
660 661 662 663 664
        selectionControls: widget.selectionEnabled ? textSelectionControls : null,
        onSelectionChanged: _handleSelectionChanged,
        onSelectionHandleTapped: _handleSelectionHandleTapped,
        rendererIgnoresPointer: true,
        cursorWidth: widget.cursorWidth,
665
        cursorHeight: widget.cursorHeight,
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
        cursorRadius: cursorRadius,
        cursorColor: cursorColor,
        cursorOpacityAnimates: cursorOpacityAnimates,
        cursorOffset: cursorOffset,
        paintCursorAboveText: paintCursorAboveText,
        backgroundCursorColor: CupertinoColors.inactiveGray,
        enableInteractiveSelection: widget.enableInteractiveSelection,
        dragStartBehavior: widget.dragStartBehavior,
        scrollPhysics: widget.scrollPhysics,
      ),
    );

    return Semantics(
      onLongPress: () {
        _effectiveFocusNode.requestFocus();
      },
      child: _selectionGestureDetectorBuilder.buildGestureDetector(
        behavior: HitTestBehavior.translucent,
        child: child,
      ),
    );
  }
}