Renderflex在底部溢出

4

我正在尝试制作动画,当我滚动listview时,如果listview的偏移量大于100,则HomePageTop小部件会缩小。 动画可以正常工作,但在动画结束时会显示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),
          ],
        ),
      ),
    );
  }
}

在这里,动画容器的大小由列表视图滚动控制器控制。 每当我向下滚动时,会出现以下错误

错误

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

用"expanded"包装动画容器可以解决错误,但是大小减小的动画不会发生。 "searchField()"是一个文本字段。把它包装在一个"fittebox"中也会出错。 欢迎任何帮助。提前感谢。

1个回答

2

原因是HomePageTop widget内的SizedBox有一个固定的高度。如果您将其删除,则错误不会出现,反正您也不需要它。


我需要一个大小合适的框,这样按钮就不会进入通知区域,使用安全区域并不能给我想要的UI输出。但是删除它确实起作用了,谢谢。 - Mr.Despicable

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接