button_style_button.dart 23.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math' as math;

import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

import 'button_style.dart';
import 'colors.dart';
import 'constants.dart';
14 15
import 'elevated_button.dart';
import 'filled_button.dart';
16 17 18
import 'ink_well.dart';
import 'material.dart';
import 'material_state.dart';
19 20
import 'outlined_button.dart';
import 'text_button.dart';
21 22
import 'theme_data.dart';

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/// {@template flutter.material.ButtonStyleButton.iconAlignment}
/// Determines the alignment of the icon within the widgets such as:
///   - [ElevatedButton.icon],
///   - [FilledButton.icon],
///   - [FilledButton.tonalIcon].
///   - [OutlinedButton.icon],
///   - [TextButton.icon],
///
/// The effect of `iconAlignment` depends on [TextDirection]. If textDirection is
/// [TextDirection.ltr] then [IconAlignment.start] and [IconAlignment.end] align the
/// icon on the left or right respectively.  If textDirection is [TextDirection.rtl] the
/// the alignments are reversed.
///
/// Defaults to [IconAlignment.start].
///
/// {@tool dartpad}
/// This sample demonstrates how to use `iconAlignment` to align the button icon to the start
/// or the end of the button.
///
/// ** See code in examples/api/lib/material/button_style_button/button_style_button.icon_alignment.0.dart **
/// {@end-tool}
///
/// {@endtemplate}
enum IconAlignment {
  /// The icon is placed at the start of the button.
  start,

  /// The icon is placed at the end of the button.
  end,
}

54 55 56 57 58
/// The base [StatefulWidget] class for buttons whose style is defined by a [ButtonStyle] object.
///
/// Concrete subclasses must override [defaultStyleOf] and [themeStyleOf].
///
/// See also:
59 60 61 62 63 64 65
///  * [ElevatedButton], a filled button whose material elevates when pressed.
///  * [FilledButton], a filled button that doesn't elevate when pressed.
///  * [FilledButton.tonal], a filled button variant that uses a secondary fill color.
///  * [OutlinedButton], a button with an outlined border and no fill color.
///  * [TextButton], a button with no outline or fill color.
///  * <https://m3.material.io/components/buttons/overview>, an overview of each of
///    the Material Design button types and how they should be used in designs.
66
abstract class ButtonStyleButton extends StatefulWidget {
67 68
  /// Abstract const constructor. This constructor enables subclasses to provide
  /// const constructors so that they can be used in const expressions.
69
  const ButtonStyleButton({
70
    super.key,
71 72
    required this.onPressed,
    required this.onLongPress,
73 74
    required this.onHover,
    required this.onFocusChange,
75 76 77 78
    required this.style,
    required this.focusNode,
    required this.autofocus,
    required this.clipBehavior,
79
    this.statesController,
80
    this.isSemanticButton = true,
81
    required this.child,
82
    this.iconAlignment = IconAlignment.start,
83
  });
84 85 86 87 88 89 90 91

  /// Called when the button is tapped or otherwise activated.
  ///
  /// If this callback and [onLongPress] are null, then the button will be disabled.
  ///
  /// See also:
  ///
  ///  * [enabled], which is true if the button is enabled.
92
  final VoidCallback? onPressed;
93 94 95 96 97 98 99 100

  /// Called when the button is long-pressed.
  ///
  /// If this callback and [onPressed] are null, then the button will be disabled.
  ///
  /// See also:
  ///
  ///  * [enabled], which is true if the button is enabled.
101
  final VoidCallback? onLongPress;
102

103 104 105 106 107 108 109 110 111 112 113 114 115
  /// Called when a pointer enters or exits the button response area.
  ///
  /// The value passed to the callback is true if a pointer has entered this
  /// part of the material and false if a pointer has exited this part of the
  /// material.
  final ValueChanged<bool>? onHover;

  /// Handler called when the focus changes.
  ///
  /// Called with true if this widget's node gains focus, and false if it loses
  /// focus.
  final ValueChanged<bool>? onFocusChange;

116 117 118 119 120 121 122 123
  /// Customizes this button's appearance.
  ///
  /// Non-null properties of this style override the corresponding
  /// properties in [themeStyleOf] and [defaultStyleOf]. [MaterialStateProperty]s
  /// that resolve to non-null values will similarly override the corresponding
  /// [MaterialStateProperty]s in [themeStyleOf] and [defaultStyleOf].
  ///
  /// Null by default.
124
  final ButtonStyle? style;
125

126
  /// {@macro flutter.material.Material.clipBehavior}
127
  ///
128 129 130 131
  /// Defaults to [Clip.none] unless [ButtonStyle.backgroundBuilder] or
  /// [ButtonStyle.foregroundBuilder] is specified. In those
  /// cases the default is [Clip.antiAlias].
  final Clip? clipBehavior;
132 133

  /// {@macro flutter.widgets.Focus.focusNode}
134
  final FocusNode? focusNode;
135 136 137 138

  /// {@macro flutter.widgets.Focus.autofocus}
  final bool autofocus;

139 140 141
  /// {@macro flutter.material.inkwell.statesController}
  final MaterialStatesController? statesController;

142 143 144 145 146 147 148 149 150
  /// Determine whether this subtree represents a button.
  ///
  /// If this is null, the screen reader will not announce "button" when this
  /// is focused. This is useful for [MenuItemButton] and [SubmenuButton] when we
  /// traverse the menu system.
  ///
  /// Defaults to true.
  final bool? isSemanticButton;

151
  /// Typically the button's label.
152 153
  ///
  /// {@macro flutter.widgets.ProxyWidget.child}
154
  final Widget? child;
155

156 157 158
  /// {@macro flutter.material.ButtonStyleButton.iconAlignment}
  final IconAlignment iconAlignment;

159 160 161
  /// Returns a non-null [ButtonStyle] that's based primarily on the [Theme]'s
  /// [ThemeData.textTheme] and [ThemeData.colorScheme].
  ///
162
  /// The returned style can be overridden by the [style] parameter and
163
  /// by the style returned by [themeStyleOf]. For example the default
164
  /// style of the [TextButton] subclass can be overridden with its
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  /// [TextButton.style] constructor parameter, or with a
  /// [TextButtonTheme].
  ///
  /// Concrete button subclasses should return a ButtonStyle that
  /// has no null properties, and where all of the [MaterialStateProperty]
  /// properties resolve to non-null values.
  ///
  /// See also:
  ///
  ///  * [themeStyleOf], Returns the ButtonStyle of this button's component theme.
  @protected
  ButtonStyle defaultStyleOf(BuildContext context);

  /// Returns the ButtonStyle that belongs to the button's component theme.
  ///
180
  /// The returned style can be overridden by the [style] parameter.
181 182 183 184 185 186 187 188 189
  ///
  /// Concrete button subclasses should return the ButtonStyle for the
  /// nearest subclass-specific inherited theme, and if no such theme
  /// exists, then the same value from the overall [Theme].
  ///
  /// See also:
  ///
  ///  * [defaultStyleOf], Returns the default [ButtonStyle] for this button.
  @protected
190
  ButtonStyle? themeStyleOf(BuildContext context);
191 192 193 194 195 196 197 198

  /// Whether the button is enabled or disabled.
  ///
  /// Buttons are disabled by default. To enable a button, set its [onPressed]
  /// or [onLongPress] properties to a non-null value.
  bool get enabled => onPressed != null || onLongPress != null;

  @override
199
  State<ButtonStyleButton> createState() => _ButtonStyleState();
200 201 202 203 204 205 206 207 208

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(FlagProperty('enabled', value: enabled, ifFalse: 'disabled'));
    properties.add(DiagnosticsProperty<ButtonStyle>('style', style, defaultValue: null));
    properties.add(DiagnosticsProperty<FocusNode>('focusNode', focusNode, defaultValue: null));
  }

209
  /// Returns null if [value] is null, otherwise `MaterialStatePropertyAll<T>(value)`.
210 211
  ///
  /// A convenience method for subclasses.
212
  static MaterialStateProperty<T>? allOrNull<T>(T? value) => value == null ? null : MaterialStatePropertyAll<T>(value);
213

214 215
  /// A convenience method used by subclasses in the framework, that returns an
  /// interpolated value based on the [fontSizeMultiplier] parameter:
216 217
  ///
  ///  * 0 - 1 [geometry1x]
218 219
  ///  * 1 - 2 lerp([geometry1x], [geometry2x], [fontSizeMultiplier] - 1)
  ///  * 2 - 3 lerp([geometry2x], [geometry3x], [fontSizeMultiplier] - 2)
220 221
  ///  * otherwise [geometry3x]
  ///
222 223 224 225 226 227 228 229 230 231
  /// This method is used by the framework for estimating the default paddings to
  /// use on a button with a text label, when the system text scaling setting
  /// changes. It's usually supplied with empirical [geometry1x], [geometry2x],
  /// [geometry3x] values adjusted for different system text scaling values, when
  /// the unscaled font size is set to 14.0 (the default [TextTheme.labelLarge]
  /// value).
  ///
  /// The `fontSizeMultiplier` argument, for historical reasons, is the default
  /// font size specified in the [ButtonStyle], scaled by the ambient font
  /// scaler, then divided by 14.0 (the default font size used in buttons).
232 233 234 235
  static EdgeInsetsGeometry scaledPadding(
    EdgeInsetsGeometry geometry1x,
    EdgeInsetsGeometry geometry2x,
    EdgeInsetsGeometry geometry3x,
236
    double fontSizeMultiplier,
237
  ) {
238
    return switch (fontSizeMultiplier) {
239
      <= 1 => geometry1x,
240 241
      < 2  => EdgeInsetsGeometry.lerp(geometry1x, geometry2x, fontSizeMultiplier - 1)!,
      < 3  => EdgeInsetsGeometry.lerp(geometry2x, geometry3x, fontSizeMultiplier - 2)!,
242 243
      _    => geometry3x,
    };
244 245 246 247 248 249 250 251
  }
}

/// The base [State] class for buttons whose style is defined by a [ButtonStyle] object.
///
/// See also:
///
///  * [ButtonStyleButton], the [StatefulWidget] subclass for which this class is the [State].
252
///  * [ElevatedButton], a filled button whose material elevates when pressed.
253
///  * [FilledButton], a filled ButtonStyleButton that doesn't elevate when pressed.
254
///  * [OutlinedButton], similar to [TextButton], but with an outline.
255
///  * [TextButton], a simple button without a shadow.
256 257 258 259 260 261 262 263 264 265
class _ButtonStyleState extends State<ButtonStyleButton> with TickerProviderStateMixin {
  AnimationController? controller;
  double? elevation;
  Color? backgroundColor;
  MaterialStatesController? internalStatesController;

  void handleStatesControllerChange() {
    // Force a rebuild to resolve MaterialStateProperty properties
    setState(() { });
  }
266

267 268 269 270 271 272 273 274
  MaterialStatesController get statesController => widget.statesController ?? internalStatesController!;

  void initStatesController() {
    if (widget.statesController == null) {
      internalStatesController = MaterialStatesController();
    }
    statesController.update(MaterialState.disabled, !widget.enabled);
    statesController.addListener(handleStatesControllerChange);
275 276
  }

277
  @override
278 279 280
  void initState() {
    super.initState();
    initStatesController();
281 282
  }

283
  @override
284 285
  void didUpdateWidget(ButtonStyleButton oldWidget) {
    super.didUpdateWidget(oldWidget);
286 287 288 289 290 291 292 293 294 295 296 297 298 299
    if (widget.statesController != oldWidget.statesController) {
      oldWidget.statesController?.removeListener(handleStatesControllerChange);
      if (widget.statesController != null) {
        internalStatesController?.dispose();
        internalStatesController = null;
      }
      initStatesController();
    }
    if (widget.enabled != oldWidget.enabled) {
      statesController.update(MaterialState.disabled, !widget.enabled);
      if (!widget.enabled) {
        // The button may have been disabled while a press gesture is currently underway.
        statesController.update(MaterialState.pressed, false);
      }
300
    }
301 302
  }

303 304 305 306 307 308 309 310
  @override
  void dispose() {
    statesController.removeListener(handleStatesControllerChange);
    internalStatesController?.dispose();
    controller?.dispose();
    super.dispose();
  }

311 312
  @override
  Widget build(BuildContext context) {
313 314
    final ButtonStyle? widgetStyle = widget.style;
    final ButtonStyle? themeStyle = widget.themeStyleOf(context);
315 316
    final ButtonStyle defaultStyle = widget.defaultStyleOf(context);

317 318 319 320
    T? effectiveValue<T>(T? Function(ButtonStyle? style) getProperty) {
      final T? widgetValue  = getProperty(widgetStyle);
      final T? themeValue   = getProperty(themeStyle);
      final T? defaultValue = getProperty(defaultStyle);
321 322 323
      return widgetValue ?? themeValue ?? defaultValue;
    }

324
    T? resolve<T>(MaterialStateProperty<T>? Function(ButtonStyle? style) getProperty) {
325
      return effectiveValue(
326 327 328
        (ButtonStyle? style) {
          return getProperty(style)?.resolve(statesController.value);
        },
329 330 331
      );
    }

332 333 334 335 336
    final double? resolvedElevation = resolve<double?>((ButtonStyle? style) => style?.elevation);
    final TextStyle? resolvedTextStyle = resolve<TextStyle?>((ButtonStyle? style) => style?.textStyle);
    Color? resolvedBackgroundColor = resolve<Color?>((ButtonStyle? style) => style?.backgroundColor);
    final Color? resolvedForegroundColor = resolve<Color?>((ButtonStyle? style) => style?.foregroundColor);
    final Color? resolvedShadowColor = resolve<Color?>((ButtonStyle? style) => style?.shadowColor);
337
    final Color? resolvedSurfaceTintColor = resolve<Color?>((ButtonStyle? style) => style?.surfaceTintColor);
338 339
    final EdgeInsetsGeometry? resolvedPadding = resolve<EdgeInsetsGeometry?>((ButtonStyle? style) => style?.padding);
    final Size? resolvedMinimumSize = resolve<Size?>((ButtonStyle? style) => style?.minimumSize);
340
    final Size? resolvedFixedSize = resolve<Size?>((ButtonStyle? style) => style?.fixedSize);
341
    final Size? resolvedMaximumSize = resolve<Size?>((ButtonStyle? style) => style?.maximumSize);
342
    final Color? resolvedIconColor = resolve<Color?>((ButtonStyle? style) => style?.iconColor);
343
    final double? resolvedIconSize = resolve<double?>((ButtonStyle? style) => style?.iconSize);
344 345
    final BorderSide? resolvedSide = resolve<BorderSide?>((ButtonStyle? style) => style?.side);
    final OutlinedBorder? resolvedShape = resolve<OutlinedBorder?>((ButtonStyle? style) => style?.shape);
346

347
    final MaterialStateMouseCursor mouseCursor = _MouseCursor(
348
      (Set<MaterialState> states) => effectiveValue((ButtonStyle? style) => style?.mouseCursor?.resolve(states)),
349 350
    );

351
    final MaterialStateProperty<Color?> overlayColor = MaterialStateProperty.resolveWith<Color?>(
352
      (Set<MaterialState> states) => effectiveValue((ButtonStyle? style) => style?.overlayColor?.resolve(states)),
353 354
    );

355 356 357 358
    final VisualDensity? resolvedVisualDensity = effectiveValue((ButtonStyle? style) => style?.visualDensity);
    final MaterialTapTargetSize? resolvedTapTargetSize = effectiveValue((ButtonStyle? style) => style?.tapTargetSize);
    final Duration? resolvedAnimationDuration = effectiveValue((ButtonStyle? style) => style?.animationDuration);
    final bool? resolvedEnableFeedback = effectiveValue((ButtonStyle? style) => style?.enableFeedback);
359
    final AlignmentGeometry? resolvedAlignment = effectiveValue((ButtonStyle? style) => style?.alignment);
360
    final Offset densityAdjustment = resolvedVisualDensity!.baseSizeAdjustment;
361
    final InteractiveInkFeatureFactory? resolvedSplashFactory = effectiveValue((ButtonStyle? style) => style?.splashFactory);
362 363 364 365 366
    final ButtonLayerBuilder? resolvedBackgroundBuilder = effectiveValue((ButtonStyle? style) => style?.backgroundBuilder);
    final ButtonLayerBuilder? resolvedForegroundBuilder = effectiveValue((ButtonStyle? style) => style?.foregroundBuilder);

    final Clip effectiveClipBehavior = widget.clipBehavior
      ?? ((resolvedBackgroundBuilder ?? resolvedForegroundBuilder) != null ? Clip.antiAlias : Clip.none);
367 368

    BoxConstraints effectiveConstraints = resolvedVisualDensity.effectiveConstraints(
369
      BoxConstraints(
370
        minWidth: resolvedMinimumSize!.width,
371
        minHeight: resolvedMinimumSize.height,
372 373
        maxWidth: resolvedMaximumSize!.width,
        maxHeight: resolvedMaximumSize.height,
374 375
      ),
    );
376 377 378 379 380 381 382 383 384 385 386
    if (resolvedFixedSize != null) {
      final Size size = effectiveConstraints.constrain(resolvedFixedSize);
      if (size.width.isFinite) {
        effectiveConstraints = effectiveConstraints.copyWith(
          minWidth: size.width,
          maxWidth: size.width,
        );
      }
      if (size.height.isFinite) {
        effectiveConstraints = effectiveConstraints.copyWith(
          minHeight: size.height,
387
          maxHeight: size.height,
388 389 390 391
        );
      }
    }

392 393 394 395 396 397 398 399
    // Per the Material Design team: don't allow the VisualDensity
    // adjustment to reduce the width of the left/right padding. If we
    // did, VisualDensity.compact, the default for desktop/web, would
    // reduce the horizontal padding to zero.
    final double dy = densityAdjustment.dy;
    final double dx = math.max(0, densityAdjustment.dx);
    final EdgeInsetsGeometry padding = resolvedPadding!
      .add(EdgeInsets.fromLTRB(dx, dy, dx, dy))
400
      .clamp(EdgeInsets.zero, EdgeInsetsGeometry.infinity);
401

402 403 404 405
    // If an opaque button's background is becoming translucent while its
    // elevation is changing, change the elevation first. Material implicitly
    // animates its elevation but not its color. SKIA renders non-zero
    // elevations as a shadow colored fill behind the Material's background.
406
    if (resolvedAnimationDuration! > Duration.zero
407 408 409 410 411
        && elevation != null
        && backgroundColor != null
        && elevation != resolvedElevation
        && backgroundColor!.value != resolvedBackgroundColor!.value
        && backgroundColor!.opacity == 1
412 413
        && resolvedBackgroundColor.opacity < 1
        && resolvedElevation == 0) {
414 415 416
      if (controller?.duration != resolvedAnimationDuration) {
        controller?.dispose();
        controller = AnimationController(
417 418 419 420 421 422 423 424 425
          duration: resolvedAnimationDuration,
          vsync: this,
        )
        ..addStatusListener((AnimationStatus status) {
          if (status == AnimationStatus.completed) {
            setState(() { }); // Rebuild with the final background color.
          }
        });
      }
426 427 428
      resolvedBackgroundColor = backgroundColor; // Defer changing the background color.
      controller!.value = 0;
      controller!.forward();
429
    }
430 431
    elevation = resolvedElevation;
    backgroundColor = resolvedBackgroundColor;
432

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
    Widget effectiveChild = Padding(
      padding: padding,
      child: Align(
        alignment: resolvedAlignment!,
        widthFactor: 1.0,
        heightFactor: 1.0,
        child: resolvedForegroundBuilder != null
          ? resolvedForegroundBuilder(context, statesController.value, widget.child)
          : widget.child,
      ),
    );
    if (resolvedBackgroundBuilder != null) {
      effectiveChild = resolvedBackgroundBuilder(context, statesController.value, effectiveChild);
    }

448 449 450
    final Widget result = ConstrainedBox(
      constraints: effectiveConstraints,
      child: Material(
451
        elevation: resolvedElevation!,
452
        textStyle: resolvedTextStyle?.copyWith(color: resolvedForegroundColor),
453
        shape: resolvedShape!.copyWith(side: resolvedSide),
454 455
        color: resolvedBackgroundColor,
        shadowColor: resolvedShadowColor,
456
        surfaceTintColor: resolvedSurfaceTintColor,
457 458
        type: resolvedBackgroundColor == null ? MaterialType.transparency : MaterialType.button,
        animationDuration: resolvedAnimationDuration,
459
        clipBehavior: effectiveClipBehavior,
460 461 462
        child: InkWell(
          onTap: widget.onPressed,
          onLongPress: widget.onLongPress,
463 464
          onHover: widget.onHover,
          mouseCursor: mouseCursor,
465 466 467
          enableFeedback: resolvedEnableFeedback,
          focusNode: widget.focusNode,
          canRequestFocus: widget.enabled,
468
          onFocusChange: widget.onFocusChange,
469
          autofocus: widget.autofocus,
470
          splashFactory: resolvedSplashFactory,
471 472
          overlayColor: overlayColor,
          highlightColor: Colors.transparent,
473
          customBorder: resolvedShape.copyWith(side: resolvedSide),
474
          statesController: statesController,
475
          child: IconTheme.merge(
476
            data: IconThemeData(color: resolvedIconColor ?? resolvedForegroundColor, size: resolvedIconSize),
477
            child: effectiveChild,
478 479 480 481 482
          ),
        ),
      ),
    );

483
    final Size minSize;
484
    switch (resolvedTapTargetSize!) {
485 486 487 488 489 490 491 492 493 494 495 496 497
      case MaterialTapTargetSize.padded:
        minSize = Size(
          kMinInteractiveDimension + densityAdjustment.dx,
          kMinInteractiveDimension + densityAdjustment.dy,
        );
        assert(minSize.width >= 0.0);
        assert(minSize.height >= 0.0);
      case MaterialTapTargetSize.shrinkWrap:
        minSize = Size.zero;
    }

    return Semantics(
      container: true,
498
      button: widget.isSemanticButton,
499 500 501 502 503 504 505 506 507 508 509 510
      enabled: widget.enabled,
      child: _InputPadding(
        minSize: minSize,
        child: result,
      ),
    );
  }
}

class _MouseCursor extends MaterialStateMouseCursor {
  const _MouseCursor(this.resolveCallback);

511
  final MaterialPropertyResolver<MouseCursor?> resolveCallback;
512 513

  @override
514
  MouseCursor resolve(Set<MaterialState> states) => resolveCallback(states)!;
515 516 517 518 519

  @override
  String get debugDescription => 'ButtonStyleButton_MouseCursor';
}

520
/// A widget to pad the area around a [ButtonStyleButton]'s inner [Material].
521 522 523 524 525 526
///
/// Redirect taps that occur in the padded area around the child to the center
/// of the child. This increases the size of the button and the button's
/// "tap target", but not its material or its ink splashes.
class _InputPadding extends SingleChildRenderObjectWidget {
  const _InputPadding({
527
    super.child,
528
    required this.minSize,
529
  });
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

  final Size minSize;

  @override
  RenderObject createRenderObject(BuildContext context) {
    return _RenderInputPadding(minSize);
  }

  @override
  void updateRenderObject(BuildContext context, covariant _RenderInputPadding renderObject) {
    renderObject.minSize = minSize;
  }
}

class _RenderInputPadding extends RenderShiftedBox {
545
  _RenderInputPadding(this._minSize, [RenderBox? child]) : super(child);
546 547 548 549

  Size get minSize => _minSize;
  Size _minSize;
  set minSize(Size value) {
550
    if (_minSize == value) {
551
      return;
552
    }
553 554 555 556 557 558
    _minSize = value;
    markNeedsLayout();
  }

  @override
  double computeMinIntrinsicWidth(double height) {
559
    if (child != null) {
560
      return math.max(child!.getMinIntrinsicWidth(height), minSize.width);
561
    }
562 563 564 565 566
    return 0.0;
  }

  @override
  double computeMinIntrinsicHeight(double width) {
567
    if (child != null) {
568
      return math.max(child!.getMinIntrinsicHeight(width), minSize.height);
569
    }
570 571 572 573 574
    return 0.0;
  }

  @override
  double computeMaxIntrinsicWidth(double height) {
575
    if (child != null) {
576
      return math.max(child!.getMaxIntrinsicWidth(height), minSize.width);
577
    }
578 579 580 581 582
    return 0.0;
  }

  @override
  double computeMaxIntrinsicHeight(double width) {
583
    if (child != null) {
584
      return math.max(child!.getMaxIntrinsicHeight(width), minSize.height);
585
    }
586 587 588
    return 0.0;
  }

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
  Size _computeSize({required BoxConstraints constraints, required ChildLayouter layoutChild}) {
    if (child != null) {
      final Size childSize = layoutChild(child!, constraints);
      final double height = math.max(childSize.width, minSize.width);
      final double width = math.max(childSize.height, minSize.height);
      return constraints.constrain(Size(height, width));
    }
    return Size.zero;
  }

  @override
  Size computeDryLayout(BoxConstraints constraints) {
    return _computeSize(
      constraints: constraints,
      layoutChild: ChildLayoutHelper.dryLayoutChild,
    );
  }

607 608
  @override
  void performLayout() {
609 610 611 612
    size = _computeSize(
      constraints: constraints,
      layoutChild: ChildLayoutHelper.layoutChild,
    );
613
    if (child != null) {
614
      final BoxParentData childParentData = child!.parentData! as BoxParentData;
615
      childParentData.offset = Alignment.center.alongOffset(size - child!.size as Offset);
616 617 618 619
    }
  }

  @override
620
  bool hitTest(BoxHitTestResult result, { required Offset position }) {
621 622 623
    if (super.hitTest(result, position: position)) {
      return true;
    }
624
    final Offset center = child!.size.center(Offset.zero);
625 626 627
    return result.addWithRawTransform(
      transform: MatrixUtils.forceToPoint(center),
      position: center,
628
      hitTest: (BoxHitTestResult result, Offset position) {
629
        assert(position == center);
630
        return child!.hitTest(result, position: center);
631 632 633 634
      },
    );
  }
}