Renderflex在底部溢出

问题描述

我试图做动画,每当我滚动列表视图时,HomePagetop小部件都会缩小 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滚动控制器控制。 每当我向下滚动时都会出现此错误

错误

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

我的HomePagetop小部件

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),child: searchField())),]),);
  }

用扩展的包装动画容器可以消除错误,但是不会发生尺寸减小的动画。 searchField()是一个文本字段,将其包装在合适的框中也会产生错误。 非常感谢您的帮助。

解决方法

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