颤振:计算 CustomSingleChildLayout 内的小部件大小

时间:2021-05-16 18:09:46

标签: flutter flutter-layout

我正在尝试像这样使用 CustomSingleChildLayout

 @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: widget.route.animation,
      builder: (context, child) {
        final double bottomPadding = MediaQuery.of(context).padding.bottom;

        return CustomSingleChildLayout(
          delegate: _BottomPickerLayout(widget.route.animation.value,
              bottomPadding: bottomPadding),
          child: Material(
            color: Colors.white,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.only(
                topLeft: const Radius.circular(20.0),
                topRight: const Radius.circular(20.0),
              ),
            ),
            clipBehavior: Clip.antiAlias,
            elevation: 1.0,
            child: AltMonth(),
          ),
        );
      },
    );

这是我的委托类:

class _BottomPickerLayout extends SingleChildLayoutDelegate {
  final double progress;
  final double bottomPadding;

  _BottomPickerLayout(
    this.progress, {
    this.bottomPadding = 0,
  });

  @override
  Size getSize(BoxConstraints constraints) {
    return super.getSize(constraints);
  }

  @override
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) {

    return BoxConstraints(
      minWidth: constraints.maxWidth,
      maxWidth: constraints.maxWidth,
      minHeight: 0.0,
      maxHeight: 310 + bottomPadding,
    );
  }

  @override
  Offset getPositionForChild(Size size, Size childSize) {
    final height = size.height - childSize.height * progress;
    return Offset(0.0, height);
  }

  @override
  bool shouldRelayout(covariant _BottomPickerLayout oldDelegate) {
    return progress != oldDelegate.progress;
  }
}

我想计算 AltMonth 小部件的大小并将其大小替换为 maxHeight: 310 + bottomPadding,。 AltMonth 小部件具有不同的大小,并且出现错误。我如何计算 AltMonth widget 大小?

0 个答案:

没有答案