flutter - Renderflex 在底部溢出

标签 flutter dart flutter-layout flutter-animation

我正在尝试制作动画,每当我滚动 ListView 和
listview 的偏移量大于 > 100。
动画有效,但就在动画结束时显示了 renderflex 溢出错误。

class HomePageView extends StatefulWidget {
  @override
  _HomePageViewState createState() => _HomePageViewState();
}

class _HomePageViewState extends State<HomePageView> {
  ScrollController scrollController = ScrollController();
  bool closeTopContainer = false;

  @override
  void initState() {
    super.initState();
    scrollController.addListener(() {
      setState(() {
        closeTopContainer = scrollController.offset > 100;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    final Size size = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: Color.fromRGBO(235, 236, 240, 1),
      body: Container(
        height: size.height,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            AnimatedContainer(
              color: Colors.redAccent,
                width: size.width,
                duration: const Duration(milliseconds: 200),
                height: closeTopContainer ? 0 : size.width * .33,
                child: HomePageTop(
                  size: size,
                )),
            Expanded(
              child: ListView(
                      controller: scrollcontroller),
          ],
        ),
      ),
    );
  }
}
这里动画容器的大小由 ListView 滚动 Controller 控制。
每当我向下滚动时,都会出现此错误
错误
The following assertion was thrown during layout:
A RenderFlex overflowed by 5.4 pixels on the bottom.

The relevant error-causing widget was:
  Column file:///D:/flutter/app/app/lib/views/Widgets/widgets.dart:94:12

The overflowing RenderFlex has an orientation of Axis.vertical.
The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and
black striped pattern. This is usually caused by the contents being too big for the RenderFlex.
Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the
RenderFlex to fit within the available space instead of being sized to their natural size.
The specific RenderFlex in question is: RenderFlex#a37e0 OVERFLOWING:
  needs compositing
  creator: Column ← HomePageTop ← DecoratedBox ← ConstrainedBox ← Container ← AnimatedContainer ← Flex
    ← ConstrainedBox ← Container ← _BodyBuilder ← MediaQuery ← LayoutId-[<_ScaffoldSlot.body>] ← ⋯
  parentData: <none> (can use size)
  constraints: BoxConstraints(w=470.2, h=49.6)
  size: Size(470.2, 49.6)
  direction: vertical
  mainAxisAlignment: start
  mainAxisSize: min
  crossAxisAlignment: center
  verticalDirection: down
我的主页顶部小部件
Widget build(BuildContext context) {
    return Column(
      children: [
        SizedBox(
          height: 55,
        ),
        Expanded(
          child: Stack(children: [
            Positioned(right: 20, child: FittedBox(fit:BoxFit.fill,child: LogoutButton())),
            Positioned(
                top: 50,
                child: Container(
                  alignment: Alignment.topCenter,
                    padding: EdgeInsets.all(20),
                    width: size.width,
                    child: searchField())),
          ]),
        ),
      ],
    );
  }
用扩展包装动画容器会消除错误,但不会发生尺寸减小的动画。
searchField() 是一个文本字段。将其包装在合适的框中也会出错。
任何帮助表示赞赏。提前致谢

最佳答案

这是因为 HomePageTop 小部件中的 SizedBox 具有固定的高度。如果您删除它,则不会出现错误,无论如何您都不需要它。

关于flutter - Renderflex 在底部溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63800426/

相关文章:

flutter - getter 'bloc' 在 null 上调用

flutter - 如何在 Flutter 中设置工作日多天的本地通知?

dart - 多次打印相同的字符而不循环

flutter - 查看容器内的网页

flutter 。将一个 Draggable 项拖到另一个 Draggable 项上

flutter - 从数组中删除特定项目会删除UI中的错误项目

flutter - 只处理特定区域的鼠标事件

flutter - Flutter:admob构建成功,但随后崩溃

dart - CustomPainter Path Canvas 上的多余行

flutter - 始终保持选中至少一个复选框