如何使用动画颤动将小部件 by/relative 动态扩展到子维度

How to dynamically expand a widget by/relative to child dimensions with animation flutter

我正在尝试 expand/animate 相对于包含的小部件动态尺寸的容器。

我尝试通过获取渲染框尺寸来获取提取的内部小部件尺寸。 我有这个来改变尺寸

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

typedef void OnWidgetSizeChange(Size size);

class MeasureSizeRenderObject extends RenderProxyBox {
  Size? oldSize;
  final OnWidgetSizeChange onChange;

  MeasureSizeRenderObject(this.onChange);

  @override
  void performLayout() {
    super.performLayout();

    Size newSize = child!.size;
    if (oldSize == newSize) return;

    oldSize = newSize;
    WidgetsBinding.instance.addPostFrameCallback((_) {
      onChange(newSize);
    });
  }
}

class MeasureSize extends SingleChildRenderObjectWidget {
  final OnWidgetSizeChange onChange;

  const MeasureSize({
    Key? key,
    required this.onChange,
    required Widget child,
  }) : super(key: key, child: child);

  @override
  RenderObject createRenderObject(BuildContext context) {
    print('=============bro we here=======');
    return MeasureSizeRenderObject(onChange);
  }
}

像这样使用它

...
double _width = 0;
double _height = 0;
...
AnimatedContainerApp(
            width: _width,
            height: _height,
            child: MeasureSize(
                onChange: (size) {
                  print(size.height);
                  setState(() {
                    _height = size.height;
                  });
                },
                child: !isExpanded
                    ? Container(
                        height: 200,
                        width: double.Infinity
                        )
                    : Container(
                        height: 400,
                        width: double.Infinity
                        )),
          ),

其中 AnimatedContainerApp 是一个简单的 AnimatedContainer 小部件。

我无法触发和更新动画的尺寸。如果这是一种更好的实现方式,我愿意接受。

您可以在此处使用 AnimatedSize 小部件 https://api.flutter.dev/flutter/widgets/AnimatedSize-class.html but make sure that it will animate only one side. or else you can check 解决方案。