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

5
import 'dart:collection';
xster's avatar
xster committed
6
import 'dart:math' as math;
7
import 'dart:ui' as ui;
xster's avatar
xster committed
8 9 10

import 'package:flutter/widgets.dart';
import 'package:flutter/rendering.dart';
11
import 'package:flutter/services.dart';
xster's avatar
xster committed
12 13

import 'button.dart';
14
import 'colors.dart';
15
import 'localizations.dart';
16
import 'theme.dart';
xster's avatar
xster committed
17

18 19
// Read off from the output on iOS 12. This color does not vary with the
// application's theme color.
20
const double _kSelectionHandleOverlap = 1.5;
21 22
// Extracted from https://developer.apple.com/design/resources/.
const double _kSelectionHandleRadius = 6;
23 24 25 26 27 28 29 30 31 32 33 34 35 36

// Minimal padding from all edges of the selection toolbar to all edges of the
// screen.
const double _kToolbarScreenPadding = 8.0;
// Minimal padding from tip of the selection toolbar arrow to horizontal edges of the
// screen. Eyeballed value.
const double _kArrowScreenPadding = 26.0;

// Vertical distance between the tip of the arrow and the line of text the arrow
// is pointing to. The value used here is eyeballed.
const double _kToolbarContentDistance = 8.0;
// Values derived from https://developer.apple.com/design/resources/.
// 92% Opacity ~= 0xEB

37
// Values extracted from https://developer.apple.com/design/resources/.
38 39 40 41
// The height of the toolbar, including the arrow.
const double _kToolbarHeight = 43.0;
const Size _kToolbarArrowSize = Size(14.0, 7.0);
const Radius _kToolbarBorderRadius = Radius.circular(8);
42 43 44 45 46
// Colors extracted from https://developer.apple.com/design/resources/.
// TODO(LongCatIsLooong): https://github.com/flutter/flutter/issues/41507.
const Color _kToolbarBackgroundColor = Color(0xEB202020);
const Color _kToolbarDividerColor = Color(0xFF808080);

47
const TextStyle _kToolbarButtonFontStyle = TextStyle(
48
  inherit: false,
xster's avatar
xster committed
49
  fontSize: 14.0,
50 51
  letterSpacing: -0.15,
  fontWeight: FontWeight.w400,
52
  color: CupertinoColors.white,
xster's avatar
xster committed
53 54
);

55 56 57 58 59 60 61 62
const TextStyle _kToolbarButtonDisabledFontStyle = TextStyle(
  inherit: false,
  fontSize: 14.0,
  letterSpacing: -0.15,
  fontWeight: FontWeight.w400,
  color: CupertinoColors.inactiveGray,
);

63 64 65
// Eyeballed value.
const EdgeInsets _kToolbarButtonPadding = EdgeInsets.symmetric(vertical: 10.0, horizontal: 18.0);

66 67 68
// Generates the child that's passed into CupertinoTextSelectionToolbar.
class _CupertinoTextSelectionToolbarWrapper extends StatefulWidget {
  const _CupertinoTextSelectionToolbarWrapper({
69 70 71
    Key? key,
    required this.arrowTipX,
    required this.barTopY,
72 73 74 75 76
    this.clipboardStatus,
    this.handleCut,
    this.handleCopy,
    this.handlePaste,
    this.handleSelectAll,
77
    required this.isArrowPointingDown,
78 79 80 81
  }) : super(key: key);

  final double arrowTipX;
  final double barTopY;
82 83 84 85 86
  final ClipboardStatusNotifier? clipboardStatus;
  final VoidCallback? handleCut;
  final VoidCallback? handleCopy;
  final VoidCallback? handlePaste;
  final VoidCallback? handleSelectAll;
87 88 89 90 91 92 93
  final bool isArrowPointingDown;

  @override
  _CupertinoTextSelectionToolbarWrapperState createState() => _CupertinoTextSelectionToolbarWrapperState();
}

class _CupertinoTextSelectionToolbarWrapperState extends State<_CupertinoTextSelectionToolbarWrapper> {
94
  late ClipboardStatusNotifier _clipboardStatus;
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

  void _onChangedClipboardStatus() {
    setState(() {
      // Inform the widget that the value of clipboardStatus has changed.
    });
  }

  @override
  void initState() {
    super.initState();
    _clipboardStatus = widget.clipboardStatus ?? ClipboardStatusNotifier();
    _clipboardStatus.addListener(_onChangedClipboardStatus);
    _clipboardStatus.update();
  }

  @override
  void didUpdateWidget(_CupertinoTextSelectionToolbarWrapper oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.clipboardStatus == null && widget.clipboardStatus != null) {
      _clipboardStatus.removeListener(_onChangedClipboardStatus);
      _clipboardStatus.dispose();
116
      _clipboardStatus = widget.clipboardStatus!;
117 118 119 120
    } else if (oldWidget.clipboardStatus != null) {
      if (widget.clipboardStatus == null) {
        _clipboardStatus = ClipboardStatusNotifier();
        _clipboardStatus.addListener(_onChangedClipboardStatus);
121
        oldWidget.clipboardStatus!.removeListener(_onChangedClipboardStatus);
122
      } else if (widget.clipboardStatus != oldWidget.clipboardStatus) {
123
        _clipboardStatus = widget.clipboardStatus!;
124
        _clipboardStatus.addListener(_onChangedClipboardStatus);
125
        oldWidget.clipboardStatus!.removeListener(_onChangedClipboardStatus);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
      }
    }
    if (widget.handlePaste != null) {
      _clipboardStatus.update();
    }
  }

  @override
  void dispose() {
    super.dispose();
    // When used in an Overlay, this can be disposed after its creator has
    // already disposed _clipboardStatus.
    if (!_clipboardStatus.disposed) {
      _clipboardStatus.removeListener(_onChangedClipboardStatus);
      if (widget.clipboardStatus == null) {
        _clipboardStatus.dispose();
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    // Don't render the menu until the state of the clipboard is known.
    if (widget.handlePaste != null
        && _clipboardStatus.value == ClipboardStatus.unknown) {
      return const SizedBox(width: 0.0, height: 0.0);
    }

    final List<Widget> items = <Widget>[];
155
    final CupertinoLocalizations? localizations = CupertinoLocalizations.of(context);
156 157 158 159
    final EdgeInsets arrowPadding = widget.isArrowPointingDown
      ? EdgeInsets.only(bottom: _kToolbarArrowSize.height)
      : EdgeInsets.only(top: _kToolbarArrowSize.height);
    final Widget onePhysicalPixelVerticalDivider =
160
        SizedBox(width: 1.0 / MediaQuery.of(context)!.devicePixelRatio);
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

    void addToolbarButton(
      String text,
      VoidCallback onPressed,
    ) {
      if (items.isNotEmpty) {
        items.add(onePhysicalPixelVerticalDivider);
      }

      items.add(CupertinoButton(
        child: Text(
          text,
          overflow: TextOverflow.ellipsis,
          style: _kToolbarButtonFontStyle,
        ),
        borderRadius: null,
        color: _kToolbarBackgroundColor,
        minSize: _kToolbarHeight,
        onPressed: onPressed,
        padding: _kToolbarButtonPadding.add(arrowPadding),
        pressedOpacity: 0.7,
      ));
    }

    if (widget.handleCut != null) {
186
      addToolbarButton(localizations!.cutButtonLabel, widget.handleCut!);
187 188
    }
    if (widget.handleCopy != null) {
189
      addToolbarButton(localizations!.copyButtonLabel, widget.handleCopy!);
190 191 192
    }
    if (widget.handlePaste != null
        && _clipboardStatus.value == ClipboardStatus.pasteable) {
193
      addToolbarButton(localizations!.pasteButtonLabel, widget.handlePaste!);
194 195
    }
    if (widget.handleSelectAll != null) {
196
      addToolbarButton(localizations!.selectAllButtonLabel, widget.handleSelectAll!);
197 198 199 200 201 202 203 204 205 206 207 208 209 210
    }

    return CupertinoTextSelectionToolbar._(
      barTopY: widget.barTopY,
      arrowTipX: widget.arrowTipX,
      isArrowPointingDown: widget.isArrowPointingDown,
      child: items.isEmpty ? null : _CupertinoTextSelectionToolbarContent(
        isArrowPointingDown: widget.isArrowPointingDown,
        children: items,
      ),
    );
  }
}

211 212 213 214 215
/// An iOS-style toolbar that appears in response to text selection.
///
/// Typically displays buttons for text manipulation, e.g. copying and pasting text.
///
/// See also:
216
///
217 218
///  * [TextSelectionControls.buildToolbar], where [CupertinoTextSelectionToolbar]
///    will be used to build an iOS-style toolbar.
219 220 221
@visibleForTesting
class CupertinoTextSelectionToolbar extends SingleChildRenderObjectWidget {
  const CupertinoTextSelectionToolbar._({
222 223 224 225 226
    Key? key,
    required double barTopY,
    required double arrowTipX,
    required bool isArrowPointingDown,
    Widget? child,
227 228 229 230 231 232 233
  }) : _barTopY = barTopY,
       _arrowTipX = arrowTipX,
       _isArrowPointingDown = isArrowPointingDown,
       super(key: key, child: child);

  // The y-coordinate of toolbar's top edge, in global coordinate system.
  final double _barTopY;
234

235 236
  // The y-coordinate of the tip of the arrow, in global coordinate system.
  final double _arrowTipX;
237

238 239 240
  // Whether the arrow should point down and be attached to the bottom
  // of the toolbar, or point up and be attached to the top of the toolbar.
  final bool _isArrowPointingDown;
241

xster's avatar
xster committed
242
  @override
243 244 245 246 247 248 249 250
  _ToolbarRenderBox createRenderObject(BuildContext context) => _ToolbarRenderBox(_barTopY, _arrowTipX, _isArrowPointingDown, null);

  @override
  void updateRenderObject(BuildContext context, _ToolbarRenderBox renderObject) {
    renderObject
      ..barTopY = _barTopY
      ..arrowTipX = _arrowTipX
      ..isArrowPointingDown = _isArrowPointingDown;
xster's avatar
xster committed
251
  }
252
}
xster's avatar
xster committed
253

254 255 256 257
class _ToolbarParentData extends BoxParentData {
  // The x offset from the tip of the arrow to the center of the toolbar.
  // Positive if the tip of the arrow has a larger x-coordinate than the
  // center of the toolbar.
258
  double? arrowXOffsetFromCenter;
xster's avatar
xster committed
259
  @override
260
  String toString() => 'offset=$offset, arrowXOffsetFromCenter=$arrowXOffsetFromCenter';
xster's avatar
xster committed
261 262
}

263 264 265 266 267
class _ToolbarRenderBox extends RenderShiftedBox {
  _ToolbarRenderBox(
    this._barTopY,
    this._arrowTipX,
    this._isArrowPointingDown,
268
    RenderBox? child,
269 270
  ) : super(child);

xster's avatar
xster committed
271 272

  @override
273
  bool get isRepaintBoundary => true;
xster's avatar
xster committed
274

275 276 277 278 279 280 281 282 283
  double _barTopY;
  set barTopY(double value) {
    if (_barTopY == value) {
      return;
    }
    _barTopY = value;
    markNeedsLayout();
    markNeedsSemanticsUpdate();
  }
284

285 286 287 288
  double _arrowTipX;
  set arrowTipX(double value) {
    if (_arrowTipX == value) {
      return;
xster's avatar
xster committed
289
    }
290 291 292 293
    _arrowTipX = value;
    markNeedsLayout();
    markNeedsSemanticsUpdate();
  }
xster's avatar
xster committed
294

295 296 297 298
  bool _isArrowPointingDown;
  set isArrowPointingDown(bool value) {
    if (_isArrowPointingDown == value) {
      return;
299
    }
300 301 302 303
    _isArrowPointingDown = value;
    markNeedsLayout();
    markNeedsSemanticsUpdate();
  }
xster's avatar
xster committed
304

305 306 307 308 309 310
  final BoxConstraints heightConstraint = const BoxConstraints.tightFor(height: _kToolbarHeight);

  @override
  void setupParentData(RenderObject child) {
    if (child.parentData is! _ToolbarParentData) {
      child.parentData = _ToolbarParentData();
xster's avatar
xster committed
311
    }
312 313 314 315
  }

  @override
  void performLayout() {
316
    final BoxConstraints constraints = this.constraints;
317 318 319 320
    size = constraints.biggest;

    if (child == null) {
      return;
321
    }
322 323 324
    final BoxConstraints enforcedConstraint = constraints
      .deflate(const EdgeInsets.symmetric(horizontal: _kToolbarScreenPadding))
      .loosen();
xster's avatar
xster committed
325

326
    child!.layout(heightConstraint.enforce(enforcedConstraint), parentUsesSize: true,);
327
    final _ToolbarParentData childParentData = child!.parentData! as _ToolbarParentData;
328

329
    // The local x-coordinate of the center of the toolbar.
330 331 332
    final double lowerBound = child!.size.width/2 + _kToolbarScreenPadding;
    final double upperBound = size.width - child!.size.width/2 - _kToolbarScreenPadding;
    final double adjustedCenterX = _arrowTipX.clamp(lowerBound, upperBound);
333

334
    childParentData.offset = Offset(adjustedCenterX - child!.size.width / 2, _barTopY);
335
    childParentData.arrowXOffsetFromCenter = _arrowTipX - adjustedCenterX;
xster's avatar
xster committed
336 337
  }

338 339
  // The path is described in the toolbar's coordinate system.
  Path _clipPath() {
340
    final _ToolbarParentData childParentData = child!.parentData! as _ToolbarParentData;
341 342 343
    final Path rrect = Path()
      ..addRRect(
        RRect.fromRectAndRadius(
344 345
          Offset(0, _isArrowPointingDown ? 0 : _kToolbarArrowSize.height)
            & Size(child!.size.width, child!.size.height - _kToolbarArrowSize.height),
346 347 348
          _kToolbarBorderRadius,
        ),
      );
xster's avatar
xster committed
349

350
    final double arrowTipX = child!.size.width / 2 + childParentData.arrowXOffsetFromCenter!;
xster's avatar
xster committed
351

352
    final double arrowBottomY = _isArrowPointingDown
353
      ? child!.size.height - _kToolbarArrowSize.height
354
      : _kToolbarArrowSize.height;
xster's avatar
xster committed
355

356
    final double arrowTipY = _isArrowPointingDown ? child!.size.height : 0;
xster's avatar
xster committed
357

358 359 360 361 362
    final Path arrow = Path()
      ..moveTo(arrowTipX, arrowTipY)
      ..lineTo(arrowTipX - _kToolbarArrowSize.width / 2, arrowBottomY)
      ..lineTo(arrowTipX + _kToolbarArrowSize.width / 2, arrowBottomY)
      ..close();
xster's avatar
xster committed
363

364
    return Path.combine(PathOperation.union, rrect, arrow);
xster's avatar
xster committed
365 366 367
  }

  @override
368 369 370 371
  void paint(PaintingContext context, Offset offset) {
    if (child == null) {
      return;
    }
xster's avatar
xster committed
372

373
    final _ToolbarParentData childParentData = child!.parentData! as _ToolbarParentData;
374 375 376
    context.pushClipPath(
      needsCompositing,
      offset + childParentData.offset,
377
      Offset.zero & child!.size,
378
      _clipPath(),
379
      (PaintingContext innerContext, Offset innerOffset) => innerContext.paintChild(child!, innerOffset),
380
    );
xster's avatar
xster committed
381 382
  }

383
  Paint? _debugPaint;
384

xster's avatar
xster committed
385
  @override
386 387
  void debugPaintSize(PaintingContext context, Offset offset) {
    assert(() {
388 389 390
      if (child == null) {
        return true;
      }
391 392

      _debugPaint ??= Paint()
393 394 395 396 397 398 399 400 401 402
        ..shader = ui.Gradient.linear(
          const Offset(0.0, 0.0),
          const Offset(10.0, 10.0),
          const <Color>[Color(0x00000000), Color(0xFFFF00FF), Color(0xFFFF00FF), Color(0x00000000)],
          const <double>[0.25, 0.25, 0.75, 0.75],
          TileMode.repeated,
        )
        ..strokeWidth = 2.0
        ..style = PaintingStyle.stroke;

403
      final _ToolbarParentData childParentData = child!.parentData! as _ToolbarParentData;
404
      context.canvas.drawPath(_clipPath().shift(offset + childParentData.offset), _debugPaint!);
405 406
      return true;
    }());
xster's avatar
xster committed
407 408 409 410 411
  }
}

/// Draws a single text selection handle with a bar and a ball.
class _TextSelectionHandlePainter extends CustomPainter {
412 413 414
  const _TextSelectionHandlePainter(this.color);

  final Color color;
xster's avatar
xster committed
415 416 417

  @override
  void paint(Canvas canvas, Size size) {
418 419 420 421 422
    const double halfStrokeWidth = 1.0;
    final Paint paint = Paint()..color = color;
    final Rect circle = Rect.fromCircle(
      center: const Offset(_kSelectionHandleRadius, _kSelectionHandleRadius),
      radius: _kSelectionHandleRadius,
423
    );
424
    final Rect line = Rect.fromPoints(
425
      const Offset(
426
        _kSelectionHandleRadius - halfStrokeWidth,
427 428
        2 * _kSelectionHandleRadius - _kSelectionHandleOverlap,
      ),
429
      Offset(_kSelectionHandleRadius + halfStrokeWidth, size.height),
xster's avatar
xster committed
430
    );
431 432 433 434 435
    final Path path = Path()
      ..addOval(circle)
    // Draw line so it slightly overlaps the circle.
      ..addRect(line);
    canvas.drawPath(path, paint);
xster's avatar
xster committed
436 437 438
  }

  @override
439
  bool shouldRepaint(_TextSelectionHandlePainter oldPainter) => color != oldPainter.color;
xster's avatar
xster committed
440 441 442
}

class _CupertinoTextSelectionControls extends TextSelectionControls {
443
  /// Returns the size of the Cupertino handle.
xster's avatar
xster committed
444
  @override
445 446 447 448 449 450
  Size getHandleSize(double textLineHeight) {
    return Size(
      _kSelectionHandleRadius * 2,
      textLineHeight + _kSelectionHandleRadius * 2 - _kSelectionHandleOverlap,
    );
  }
xster's avatar
xster committed
451 452 453

  /// Builder for iOS-style copy/paste text selection toolbar.
  @override
454 455 456
  Widget buildToolbar(
    BuildContext context,
    Rect globalEditableRegion,
457
    double textLineHeight,
458 459 460
    Offset position,
    List<TextSelectionPoint> endpoints,
    TextSelectionDelegate delegate,
461
    ClipboardStatusNotifier clipboardStatus,
462
  ) {
xster's avatar
xster committed
463
    assert(debugCheckHasMediaQuery(context));
464
    final MediaQueryData mediaQuery = MediaQuery.of(context)!;
465 466 467 468

    // The toolbar should appear below the TextField when there is not enough
    // space above the TextField to show it, assuming there's always enough space
    // at the bottom in this case.
469
    final double toolbarHeightNeeded = mediaQuery.padding.top
470 471
      + _kToolbarScreenPadding
      + _kToolbarHeight
472 473 474
      + _kToolbarContentDistance;
    final double availableHeight = globalEditableRegion.top + endpoints.first.point.dy - textLineHeight;
    final bool isArrowPointingDown = toolbarHeightNeeded <= availableHeight;
475 476 477 478

    final double arrowTipX = (position.dx + globalEditableRegion.left).clamp(
      _kArrowScreenPadding + mediaQuery.padding.left,
      mediaQuery.size.width - mediaQuery.padding.right - _kArrowScreenPadding,
479
    );
480

481
    // The y-coordinate has to be calculated instead of directly quoting position.dy,
482 483 484 485 486 487
    // since the caller (TextSelectionOverlay._buildToolbar) does not know whether
    // the toolbar is going to be facing up or down.
    final double localBarTopY = isArrowPointingDown
      ? endpoints.first.point.dy - textLineHeight - _kToolbarContentDistance - _kToolbarHeight
      : endpoints.last.point.dy + _kToolbarContentDistance;

488
    return _CupertinoTextSelectionToolbarWrapper(
489
      arrowTipX: arrowTipX,
490 491 492 493 494 495
      barTopY: localBarTopY + globalEditableRegion.top,
      clipboardStatus: clipboardStatus,
      handleCut: canCut(delegate) ? () => handleCut(delegate) : null,
      handleCopy: canCopy(delegate) ? () => handleCopy(delegate, clipboardStatus) : null,
      handlePaste: canPaste(delegate) ? () => handlePaste(delegate) : null,
      handleSelectAll: canSelectAll(delegate) ? () => handleSelectAll(delegate) : null,
496
      isArrowPointingDown: isArrowPointingDown,
xster's avatar
xster committed
497 498 499 500 501 502 503 504
    );
  }

  /// Builder for iOS text selection edges.
  @override
  Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight) {
    // We want a size that's a vertical line the height of the text plus a 18.0
    // padding in every direction that will constitute the selection drag area.
505
    final Size desiredSize = getHandleSize(textLineHeight);
xster's avatar
xster committed
506

507
    final Widget handle = SizedBox.fromSize(
xster's avatar
xster committed
508
      size: desiredSize,
509 510
      child: CustomPaint(
        painter: _TextSelectionHandlePainter(CupertinoTheme.of(context).primaryColor),
xster's avatar
xster committed
511 512 513 514 515 516 517
      ),
    );

    // [buildHandle]'s widget is positioned at the selection cursor's bottom
    // baseline. We transform the handle such that the SizedBox is superimposed
    // on top of the text selection endpoints.
    switch (type) {
518 519
      case TextSelectionHandleType.left:
        return handle;
xster's avatar
xster committed
520
      case TextSelectionHandleType.right:
521
        // Right handle is a vertical mirror of the left.
522
        return Transform(
523 524 525 526
          transform: Matrix4.identity()
            ..translate(desiredSize.width / 2, desiredSize.height / 2)
            ..rotateZ(math.pi)
            ..translate(-desiredSize.width / 2, -desiredSize.height / 2),
527
          child: handle,
xster's avatar
xster committed
528
        );
529 530
      // iOS doesn't draw anything for collapsed selections.
      case TextSelectionHandleType.collapsed:
531
        return const SizedBox();
xster's avatar
xster committed
532 533
    }
  }
534 535 536 537 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

  /// Gets anchor for cupertino-style text selection handles.
  ///
  /// See [TextSelectionControls.getHandleAnchor].
  @override
  Offset getHandleAnchor(TextSelectionHandleType type, double textLineHeight) {
    final Size handleSize = getHandleSize(textLineHeight);
    switch (type) {
      // The circle is at the top for the left handle, and the anchor point is
      // all the way at the bottom of the line.
      case TextSelectionHandleType.left:
        return Offset(
          handleSize.width / 2,
          handleSize.height,
        );
      // The right handle is vertically flipped, and the anchor point is near
      // the top of the circle to give slight overlap.
      case TextSelectionHandleType.right:
        return Offset(
          handleSize.width / 2,
          handleSize.height - 2 * _kSelectionHandleRadius + _kSelectionHandleOverlap,
        );
      // A collapsed handle anchors itself so that it's centered.
      default:
        return Offset(
          handleSize.width / 2,
          textLineHeight + (handleSize.height - textLineHeight) / 2,
        );
    }
  }
xster's avatar
xster committed
564 565
}

566 567 568
// Renders the content of the selection menu and maintains the page state.
class _CupertinoTextSelectionToolbarContent extends StatefulWidget {
  const _CupertinoTextSelectionToolbarContent({
569 570 571
    Key? key,
    required this.children,
    required this.isArrowPointingDown,
572 573 574 575 576 577 578 579 580 581 582 583 584 585
  }) : assert(children != null),
       // This ignore is used because .isNotEmpty isn't compatible with const.
       assert(children.length > 0), // ignore: prefer_is_empty
       super(key: key);

  final List<Widget> children;
  final bool isArrowPointingDown;

  @override
  _CupertinoTextSelectionToolbarContentState createState() => _CupertinoTextSelectionToolbarContentState();
}

class _CupertinoTextSelectionToolbarContentState extends State<_CupertinoTextSelectionToolbarContent> with TickerProviderStateMixin {
  // Controls the fading of the buttons within the menu during page transitions.
586
  late AnimationController _controller;
587
  int _page = 0;
588
  int? _nextPage;
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607

  void _handleNextPage() {
    _controller.reverse();
    _controller.addStatusListener(_statusListener);
    _nextPage = _page + 1;
  }

  void _handlePreviousPage() {
    _controller.reverse();
    _controller.addStatusListener(_statusListener);
    _nextPage = _page - 1;
  }

  void _statusListener(AnimationStatus status) {
    if (status != AnimationStatus.dismissed) {
      return;
    }

    setState(() {
608
      _page = _nextPage!;
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
      _nextPage = null;
    });
    _controller.forward();
    _controller.removeStatusListener(_statusListener);
  }

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      value: 1.0,
      vsync: this,
      // This was eyeballed on a physical iOS device running iOS 13.
      duration: const Duration(milliseconds: 150),
    );
  }

  @override
  void didUpdateWidget(_CupertinoTextSelectionToolbarContent oldWidget) {
    // If the children are changing, the current page should be reset.
    if (widget.children != oldWidget.children) {
      _page = 0;
      _nextPage = null;
      _controller.forward();
      _controller.removeStatusListener(_statusListener);
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final EdgeInsets arrowPadding = widget.isArrowPointingDown
      ? EdgeInsets.only(bottom: _kToolbarArrowSize.height)
      : EdgeInsets.only(top: _kToolbarArrowSize.height);

    return DecoratedBox(
      decoration: const BoxDecoration(color: _kToolbarDividerColor),
      child: FadeTransition(
        opacity: _controller,
        child: _CupertinoTextSelectionToolbarItems(
          page: _page,
          backButton: CupertinoButton(
            borderRadius: null,
            color: _kToolbarBackgroundColor,
            minSize: _kToolbarHeight,
            onPressed: _handlePreviousPage,
            padding: arrowPadding,
            pressedOpacity: 0.7,
            child: const Text('◀', style: _kToolbarButtonFontStyle),
          ),
665
          dividerWidth: 1.0 / MediaQuery.of(context)!.devicePixelRatio,
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
          nextButton: CupertinoButton(
            borderRadius: null,
            color: _kToolbarBackgroundColor,
            minSize: _kToolbarHeight,
            onPressed: _handleNextPage,
            padding: arrowPadding,
            pressedOpacity: 0.7,
            child: const Text('▶', style: _kToolbarButtonFontStyle),
          ),
          nextButtonDisabled: CupertinoButton(
            borderRadius: null,
            color: _kToolbarBackgroundColor,
            disabledColor: _kToolbarBackgroundColor,
            minSize: _kToolbarHeight,
            onPressed: null,
            padding: arrowPadding,
            pressedOpacity: 1.0,
            child: const Text('▶', style: _kToolbarButtonDisabledFontStyle),
          ),
          children: widget.children,
        ),
      ),
    );
  }
}

// The custom RenderObjectWidget that, together with
// _CupertinoTextSelectionToolbarItemsRenderBox and
// _CupertinoTextSelectionToolbarItemsElement, paginates the menu items.
class _CupertinoTextSelectionToolbarItems extends RenderObjectWidget {
  _CupertinoTextSelectionToolbarItems({
697 698 699 700 701 702 703
    Key? key,
    required this.page,
    required this.children,
    required this.backButton,
    required this.dividerWidth,
    required this.nextButton,
    required this.nextButtonDisabled,
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
  }) : assert(children != null),
       assert(children.isNotEmpty),
       assert(backButton != null),
       assert(dividerWidth != null),
       assert(nextButton != null),
       assert(nextButtonDisabled != null),
       assert(page != null),
       super(key: key);

  final Widget backButton;
  final List<Widget> children;
  final double dividerWidth;
  final Widget nextButton;
  final Widget nextButtonDisabled;
  final int page;

  @override
  _CupertinoTextSelectionToolbarItemsRenderBox createRenderObject(BuildContext context) {
    return _CupertinoTextSelectionToolbarItemsRenderBox(
      dividerWidth: dividerWidth,
      page: page,
    );
  }

  @override
  void updateRenderObject(BuildContext context, _CupertinoTextSelectionToolbarItemsRenderBox renderObject) {
    renderObject
      ..page = page
      ..dividerWidth = dividerWidth;
  }

  @override
  _CupertinoTextSelectionToolbarItemsElement createElement() => _CupertinoTextSelectionToolbarItemsElement(this);
}

// The custom RenderObjectElement that helps paginate the menu items.
class _CupertinoTextSelectionToolbarItemsElement extends RenderObjectElement {
  _CupertinoTextSelectionToolbarItemsElement(
    _CupertinoTextSelectionToolbarItems widget,
  ) : super(widget);

745
  late List<Element> _children;
746 747 748 749 750 751 752 753 754 755 756 757
  final Map<_CupertinoTextSelectionToolbarItemsSlot, Element> slotToChild = <_CupertinoTextSelectionToolbarItemsSlot, Element>{};

  // We keep a set of forgotten children to avoid O(n^2) work walking _children
  // repeatedly to remove children.
  final Set<Element> _forgottenChildren = HashSet<Element>();

  @override
  _CupertinoTextSelectionToolbarItems get widget => super.widget as _CupertinoTextSelectionToolbarItems;

  @override
  _CupertinoTextSelectionToolbarItemsRenderBox get renderObject => super.renderObject as _CupertinoTextSelectionToolbarItemsRenderBox;

758
  void _updateRenderObject(RenderBox? child, _CupertinoTextSelectionToolbarItemsSlot slot) {
759 760 761 762 763 764 765 766 767 768 769 770 771 772
    switch (slot) {
      case _CupertinoTextSelectionToolbarItemsSlot.backButton:
        renderObject.backButton = child;
        break;
      case _CupertinoTextSelectionToolbarItemsSlot.nextButton:
        renderObject.nextButton = child;
        break;
      case _CupertinoTextSelectionToolbarItemsSlot.nextButtonDisabled:
        renderObject.nextButtonDisabled = child;
        break;
    }
  }

  @override
773
  void insertRenderObjectChild(RenderObject child, dynamic slot) {
774 775 776
    if (slot is _CupertinoTextSelectionToolbarItemsSlot) {
      assert(child is RenderBox);
      _updateRenderObject(child as RenderBox, slot);
777
      assert(renderObject.slottedChildren.containsKey(slot));
778 779 780 781
      return;
    }
    if (slot is IndexedSlot) {
      assert(renderObject.debugValidateChild(child));
782
      renderObject.insert(child as RenderBox, after: slot.value?.renderObject as RenderBox);
783 784 785 786 787 788 789
      return;
    }
    assert(false, 'slot must be _CupertinoTextSelectionToolbarItemsSlot or IndexedSlot');
  }

  // This is not reachable for children that don't have an IndexedSlot.
  @override
790
  void moveRenderObjectChild(RenderObject child, IndexedSlot<Element> oldSlot, IndexedSlot<Element> newSlot) {
791
    assert(child.parent == renderObject);
792
    renderObject.move(child as RenderBox, after: newSlot.value.renderObject as RenderBox?);
793 794 795
  }

  static bool _shouldPaint(Element child) {
796
    return (child.renderObject!.parentData! as ToolbarItemsParentData).shouldPaint;
797 798 799
  }

  @override
800
  void removeRenderObjectChild(RenderObject child, dynamic slot) {
801
    // Check if the child is in a slot.
802
    if (slot is _CupertinoTextSelectionToolbarItemsSlot) {
803
      assert(child is RenderBox);
804
      assert(renderObject.slottedChildren.containsKey(slot));
805
      _updateRenderObject(null, slot);
806
      assert(!renderObject.slottedChildren.containsKey(slot));
807 808 809 810
      return;
    }

    // Otherwise look for it in the list of children.
811
    assert(slot is IndexedSlot);
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
    assert(child.parent == renderObject);
    renderObject.remove(child as RenderBox);
  }

  @override
  void visitChildren(ElementVisitor visitor) {
    slotToChild.values.forEach(visitor);
    for (final Element child in _children) {
      if (!_forgottenChildren.contains(child))
        visitor(child);
    }
  }

  @override
  void forgetChild(Element child) {
827
    assert(slotToChild.containsValue(child) || _children.contains(child));
828 829
    assert(!_forgottenChildren.contains(child));
    // Handle forgetting a child in children or in a slot.
830 831
    if (slotToChild.containsKey(child.slot)) {
      final _CupertinoTextSelectionToolbarItemsSlot slot = child.slot as _CupertinoTextSelectionToolbarItemsSlot;
832 833 834 835 836 837 838 839 840
      slotToChild.remove(slot);
    } else {
      _forgottenChildren.add(child);
    }
    super.forgetChild(child);
  }

  // Mount or update slotted child.
  void _mountChild(Widget widget, _CupertinoTextSelectionToolbarItemsSlot slot) {
841 842
    final Element? oldChild = slotToChild[slot];
    final Element? newChild = updateChild(oldChild, widget, slot);
843 844 845 846 847 848 849 850 851
    if (oldChild != null) {
      slotToChild.remove(slot);
    }
    if (newChild != null) {
      slotToChild[slot] = newChild;
    }
  }

  @override
852
  void mount(Element? parent, dynamic newSlot) {
853 854 855 856 857 858 859
    super.mount(parent, newSlot);
    // Mount slotted children.
    _mountChild(widget.backButton, _CupertinoTextSelectionToolbarItemsSlot.backButton);
    _mountChild(widget.nextButton, _CupertinoTextSelectionToolbarItemsSlot.nextButton);
    _mountChild(widget.nextButtonDisabled, _CupertinoTextSelectionToolbarItemsSlot.nextButtonDisabled);

    // Mount list children.
860 861
    _children = List<Element>.filled(widget.children.length, _NullElement.instance);
    Element? previousChild;
862
    for (int i = 0; i < _children.length; i += 1) {
863
      final Element newChild = inflateWidget(widget.children[i], IndexedSlot<Element?>(i, previousChild));
864 865 866 867 868 869 870 871
      _children[i] = newChild;
      previousChild = newChild;
    }
  }

  @override
  void debugVisitOnstageChildren(ElementVisitor visitor) {
    // Visit slot children.
872 873 874
    for (final Element child in slotToChild.values) {
      if (_shouldPaint(child) && !_forgottenChildren.contains(child)) {
        visitor(child);
875
      }
876
    }
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
    // Visit list children.
    _children
        .where((Element child) => !_forgottenChildren.contains(child) && _shouldPaint(child))
        .forEach(visitor);
  }

  @override
  void update(_CupertinoTextSelectionToolbarItems newWidget) {
    super.update(newWidget);
    assert(widget == newWidget);

    // Update slotted children.
    _mountChild(widget.backButton, _CupertinoTextSelectionToolbarItemsSlot.backButton);
    _mountChild(widget.nextButton, _CupertinoTextSelectionToolbarItemsSlot.nextButton);
    _mountChild(widget.nextButtonDisabled, _CupertinoTextSelectionToolbarItemsSlot.nextButtonDisabled);

    // Update list children.
    _children = updateChildren(_children, widget.children, forgottenChildren: _forgottenChildren);
    _forgottenChildren.clear();
  }
}

// The custom RenderBox that helps paginate the menu items.
class _CupertinoTextSelectionToolbarItemsRenderBox extends RenderBox with ContainerRenderObjectMixin<RenderBox, ToolbarItemsParentData>, RenderBoxContainerDefaultsMixin<RenderBox, ToolbarItemsParentData> {
  _CupertinoTextSelectionToolbarItemsRenderBox({
902 903
    required double dividerWidth,
    required int page,
904 905 906 907 908 909
  }) : assert(dividerWidth != null),
       assert(page != null),
       _dividerWidth = dividerWidth,
       _page = page,
       super();

910
  final Map<_CupertinoTextSelectionToolbarItemsSlot, RenderBox> slottedChildren = <_CupertinoTextSelectionToolbarItemsSlot, RenderBox>{};
911

912
  RenderBox? _updateChild(RenderBox? oldChild, RenderBox? newChild, _CupertinoTextSelectionToolbarItemsSlot slot) {
913 914
    if (oldChild != null) {
      dropChild(oldChild);
915
      slottedChildren.remove(slot);
916 917
    }
    if (newChild != null) {
918
      slottedChildren[slot] = newChild;
919 920 921 922 923
      adoptChild(newChild);
    }
    return newChild;
  }

924 925 926 927
  bool _isSlottedChild(RenderBox child) {
    return child == _backButton || child == _nextButton || child == _nextButtonDisabled;
  }

928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
  int _page;
  int get page => _page;
  set page(int value) {
    if (value == _page) {
      return;
    }
    _page = value;
    markNeedsLayout();
  }

  double _dividerWidth;
  double get dividerWidth => _dividerWidth;
  set dividerWidth(double value) {
    if (value == _dividerWidth) {
      return;
    }
    _dividerWidth = value;
    markNeedsLayout();
  }

948 949 950
  RenderBox? _backButton;
  RenderBox? get backButton => _backButton;
  set backButton(RenderBox? value) {
951 952 953
    _backButton = _updateChild(_backButton, value, _CupertinoTextSelectionToolbarItemsSlot.backButton);
  }

954 955 956
  RenderBox? _nextButton;
  RenderBox? get nextButton => _nextButton;
  set nextButton(RenderBox? value) {
957 958 959
    _nextButton = _updateChild(_nextButton, value, _CupertinoTextSelectionToolbarItemsSlot.nextButton);
  }

960 961 962
  RenderBox? _nextButtonDisabled;
  RenderBox? get nextButtonDisabled => _nextButtonDisabled;
  set nextButtonDisabled(RenderBox? value) {
963 964 965 966 967 968 969 970 971 972 973
    _nextButtonDisabled = _updateChild(_nextButtonDisabled, value, _CupertinoTextSelectionToolbarItemsSlot.nextButtonDisabled);
  }

  @override
  void performLayout() {
    if (firstChild == null) {
      performResize();
      return;
    }

    // Layout slotted children.
974 975 976
    _backButton!.layout(constraints.loosen(), parentUsesSize: true);
    _nextButton!.layout(constraints.loosen(), parentUsesSize: true);
    _nextButtonDisabled!.layout(constraints.loosen(), parentUsesSize: true);
977 978

    final double subsequentPageButtonsWidth =
979
        _backButton!.size.width + _nextButton!.size.width;
980
    double currentButtonPosition = 0.0;
981 982
    late double toolbarWidth; // The width of the whole widget.
    late double firstPageWidth;
983 984 985 986 987 988
    int currentPage = 0;
    int i = -1;
    visitChildren((RenderObject renderObjectChild) {
      i++;
      final RenderBox child = renderObjectChild as RenderBox;
      final ToolbarItemsParentData childParentData =
989
          child.parentData! as ToolbarItemsParentData;
990 991 992
      childParentData.shouldPaint = false;

      // Skip slotted children and children on pages after the visible page.
993
      if (_isSlottedChild(child) || currentPage > _page) {
994 995 996 997 998 999 1000
        return;
      }

      double paginationButtonsWidth = 0.0;
      if (currentPage == 0) {
        // If this is the last child, it's ok to fit without a forward button.
        paginationButtonsWidth =
1001
            i == childCount - 1 ? 0.0 : _nextButton!.size.width;
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
      } else {
        paginationButtonsWidth = subsequentPageButtonsWidth;
      }

      // The width of the menu is set by the first page.
      child.layout(
        BoxConstraints.loose(Size(
          (currentPage == 0 ? constraints.maxWidth : firstPageWidth) - paginationButtonsWidth,
          constraints.maxHeight,
        )),
        parentUsesSize: true,
      );

      // If this child causes the current page to overflow, move to the next
      // page and relayout the child.
      final double currentWidth =
          currentButtonPosition + paginationButtonsWidth + child.size.width;
      if (currentWidth > constraints.maxWidth) {
        currentPage++;
1021 1022
        currentButtonPosition = _backButton!.size.width + dividerWidth;
        paginationButtonsWidth = _backButton!.size.width + _nextButton!.size.width;
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
        child.layout(
          BoxConstraints.loose(Size(
            firstPageWidth - paginationButtonsWidth,
            constraints.maxHeight,
          )),
          parentUsesSize: true,
        );
      }
      childParentData.offset = Offset(currentButtonPosition, 0.0);
      currentButtonPosition += child.size.width + dividerWidth;
      childParentData.shouldPaint = currentPage == page;

      if (currentPage == 0) {
1036
        firstPageWidth = currentButtonPosition + _nextButton!.size.width;
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
      }
      if (currentPage == page) {
        toolbarWidth = currentButtonPosition;
      }
    });

    // It shouldn't be possible to navigate beyond the last page.
    assert(page <= currentPage);

    // Position page nav buttons.
    if (currentPage > 0) {
      final ToolbarItemsParentData nextButtonParentData =
1049
          _nextButton!.parentData! as ToolbarItemsParentData;
1050
      final ToolbarItemsParentData nextButtonDisabledParentData =
1051
          _nextButtonDisabled!.parentData! as ToolbarItemsParentData;
1052
      final ToolbarItemsParentData backButtonParentData =
1053
          _backButton!.parentData! as ToolbarItemsParentData;
1054 1055 1056 1057 1058
      // The forward button always shows if there is more than one page, even on
      // the last page (it's just disabled).
      if (page == currentPage) {
        nextButtonDisabledParentData.offset = Offset(toolbarWidth, 0.0);
        nextButtonDisabledParentData.shouldPaint = true;
1059
        toolbarWidth += nextButtonDisabled!.size.width;
1060 1061 1062
      } else {
        nextButtonParentData.offset = Offset(toolbarWidth, 0.0);
        nextButtonParentData.shouldPaint = true;
1063
        toolbarWidth += nextButton!.size.width;
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
      }
      if (page > 0) {
        backButtonParentData.offset = Offset.zero;
        backButtonParentData.shouldPaint = true;
        // No need to add the width of the back button to toolbarWidth here. It's
        // already been taken care of when laying out the children to
        // accommodate the back button.
      }
    } else {
      // No divider for the next button when there's only one page.
      toolbarWidth -= dividerWidth;
    }

    size = constraints.constrain(Size(toolbarWidth, _kToolbarHeight));
  }

  @override
  void paint(PaintingContext context, Offset offset) {
    visitChildren((RenderObject renderObjectChild) {
      final RenderBox child = renderObjectChild as RenderBox;
1084
      final ToolbarItemsParentData childParentData = child.parentData! as ToolbarItemsParentData;
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100

      if (childParentData.shouldPaint) {
        final Offset childOffset = childParentData.offset + offset;
        context.paintChild(child, childOffset);
      }
    });
  }

  @override
  void setupParentData(RenderBox child) {
    if (child.parentData is! ToolbarItemsParentData) {
      child.parentData = ToolbarItemsParentData();
    }
  }

  // Returns true iff the single child is hit by the given position.
1101
  static bool hitTestChild(RenderBox? child, BoxHitTestResult result, { required Offset position }) {
1102 1103 1104 1105
    if (child == null) {
      return false;
    }
    final ToolbarItemsParentData childParentData =
1106
        child.parentData! as ToolbarItemsParentData;
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
    return result.addWithPaintOffset(
      offset: childParentData.offset,
      position: position,
      hitTest: (BoxHitTestResult result, Offset transformed) {
        assert(transformed == position - childParentData.offset);
        return child.hitTest(result, position: transformed);
      },
    );
  }

  @override
1118
  bool hitTestChildren(BoxHitTestResult result, { required Offset position }) {
1119 1120
    // Hit test list children.
    // The x, y parameters have the top left of the node's box as the origin.
1121
    RenderBox? child = lastChild;
1122
    while (child != null) {
1123
      final ToolbarItemsParentData childParentData = child.parentData! as ToolbarItemsParentData;
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156

      // Don't hit test children that aren't shown.
      if (!childParentData.shouldPaint) {
        child = childParentData.previousSibling;
        continue;
      }

      if (hitTestChild(child, result, position: position)) {
        return true;
      }
      child = childParentData.previousSibling;
    }

    // Hit test slot children.
    if (hitTestChild(backButton, result, position: position)) {
      return true;
    }
    if (hitTestChild(nextButton, result, position: position)) {
      return true;
    }
    if (hitTestChild(nextButtonDisabled, result, position: position)) {
      return true;
    }

    return false;
  }

  @override
  void attach(PipelineOwner owner) {
    // Attach list children.
    super.attach(owner);

    // Attach slot children.
1157
    for (final RenderBox child in slottedChildren.values) {
1158
      child.attach(owner);
1159
    }
1160 1161 1162 1163 1164 1165 1166 1167
  }

  @override
  void detach() {
    // Detach list children.
    super.detach();

    // Detach slot children.
1168
    for (final RenderBox child in slottedChildren.values) {
1169
      child.detach();
1170
    }
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
  }

  @override
  void redepthChildren() {
    visitChildren((RenderObject renderObjectChild) {
      final RenderBox child = renderObjectChild as RenderBox;
      redepthChild(child);
    });
  }

  @override
  void visitChildren(RenderObjectVisitor visitor) {
    // Visit the slotted children.
    if (_backButton != null) {
1185
      visitor(_backButton!);
1186 1187
    }
    if (_nextButton != null) {
1188
      visitor(_nextButton!);
1189 1190
    }
    if (_nextButtonDisabled != null) {
1191
      visitor(_nextButtonDisabled!);
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
    }
    // Visit the list children.
    super.visitChildren(visitor);
  }

  // Visit only the children that should be painted.
  @override
  void visitChildrenForSemantics(RenderObjectVisitor visitor) {
    visitChildren((RenderObject renderObjectChild) {
      final RenderBox child = renderObjectChild as RenderBox;
1202
      final ToolbarItemsParentData childParentData = child.parentData! as ToolbarItemsParentData;
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
      if (childParentData.shouldPaint) {
        visitor(renderObjectChild);
      }
    });
  }

  @override
  List<DiagnosticsNode> debugDescribeChildren() {
    final List<DiagnosticsNode> value = <DiagnosticsNode>[];
    visitChildren((RenderObject renderObjectChild) {
      final RenderBox child = renderObjectChild as RenderBox;
      if (child == backButton) {
        value.add(child.toDiagnosticsNode(name: 'back button'));
      } else if (child == nextButton) {
        value.add(child.toDiagnosticsNode(name: 'next button'));
      } else if (child == nextButtonDisabled) {
        value.add(child.toDiagnosticsNode(name: 'next button disabled'));

      // List children.
      } else {
        value.add(child.toDiagnosticsNode(name: 'menu item'));
      }
    });
    return value;
  }
}

// The slots that can be occupied by widgets in
// _CupertinoTextSelectionToolbarItems, excluding the list of children.
enum _CupertinoTextSelectionToolbarItemsSlot {
  backButton,
  nextButton,
  nextButtonDisabled,
}

xster's avatar
xster committed
1238
/// Text selection controls that follows iOS design conventions.
1239
final TextSelectionControls cupertinoTextSelectionControls = _CupertinoTextSelectionControls();
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256

class _NullElement extends Element {
  _NullElement() : super(_NullWidget());

  static _NullElement instance = _NullElement();

  @override
  bool get debugDoingBuild => throw UnimplementedError();

  @override
  void performRebuild() { }
}

class _NullWidget extends Widget {
  @override
  Element createElement() => throw UnimplementedError();
}