【问题标题】:Flutter (Dart): Exceptions caused by rendering / A RenderFlex overflowedFlutter (Dart):渲染导致的异常/A RenderFlex 溢出
【发布时间】:2018-09-03 22:11:08
【问题描述】:

我遇到了 Flutter (Dart) RenderFlex 溢出像素的问题。渲染库异常。

我如何管理或应用滚动功能到我的应用页面视图,并避免 Flutter 出现以下消息的呈现异常:

一个 RenderFlex 在底部溢出了 28 个像素。

如果您有任何机会需要完整的日志来帮助我,请点击此处:

在热重载时,它会根据消息在底部显示黄色/黑色条纹。

这是我可以使用可滚动小部件来管理的吗?或者我可以声明我的小部件以控制它?

如果需要完整代码(我更改了文本数据,但假设出现的文本比屏幕尺寸长,因此出现错误):

   @override
  Widget build(BuildContext context) {
    return new DefaultTabController(
        length: 3,
        child: new Scaffold(
          appBar: new AppBar(
            bottom: new TabBar(
              tabs: [
                new Tab(text: "xxx",),
                new Tab(text: "xxx",),
                new Tab(text: "xxx",),
              ],
            ),
            title: new Text(data["xxx"]),
          ),
          body: new TabBarView(
            children: [
              new Column(
                children: <Widget>[
                  new Text(data["xxx"],
                        style: new TextStyle(
                              fontStyle: FontStyle.italic,
                              color: Colors.blue,
                              fontSize: 16.0
                        ),),
                  new Text(data["xxx"],
                        style: new TextStyle(
                              fontStyle: FontStyle.italic,
                              color: Colors.blue,
                              fontSize: 10.0
                        ),),
                  new Text(data["xxx"],
                        style: new TextStyle(
                              fontStyle: FontStyle.italic,
                              color: Colors.blue,
                              fontSize: 16.0
                        ),),
                  new Text(data["xxx"],
                        style: new TextStyle(
                              fontStyle: FontStyle.italic,
                              color: Colors.blue,
                              fontSize: 8.0
                        ),
                      ),
                  new Text(data["xxx"],
                        style: new TextStyle(
                              fontStyle: FontStyle.italic,
                              color: Colors.blue,
                              fontSize: 8.0
                        ),),

                  new Row(
                    children: <Widget>[
                      new Expanded(
                        child: new Text("xxx"),
                      ),
                      new Expanded(
                        child: new Icon(Icons.file_download, color: Colors.green, size: 30.0,),
                      ),
                    ],
                  ),

                  new Divider(),
                  new Text("xxx", 
                            style: new TextStyle(
                              fontStyle: FontStyle.italic,
                              color: Colors.red,
                              fontSize: 16.0
                      ),
                  ),
                ],
              ),

              new ListView.builder(
                itemBuilder: (BuildContext context, int index) => new EntryItem(_lstTiles[index]),
                itemCount: _lstTiles.length,
              ),

              new Column(
                children: <Widget>[
                  new Text(data["xxx"], 
                            style: new TextStyle(
                              fontStyle: FontStyle.italic,
                              color: Colors.green[900],
                              fontSize: 16.0
                      ),
                  ),
                  new Text(data["xxx"], 
                            style: new TextStyle(
                              fontStyle: FontStyle.italic,
                              color: Colors.green[900],
                              fontSize: 16.0
                      ),),
                  new Text(data["xxx"]),
                  new ListTile(title: new Text("xxx")),
                  new Text(data["xxx"]),
                  new ListTile(title: new Text("xxx")),
                  new Divider(),
                  new Text("xxx", 
                            style: new TextStyle(
                              fontStyle: FontStyle.italic,
                              color: Colors.red,
                              fontSize: 16.0
                      ),
                  ),
                ],
              ),
            ],
          ),
        ),
      );
  }

【问题讨论】:

  • 您可以在您的内容周围放置一个可滚动的小部件,以使其滚动并防止错误。
  • 嗨,Herohtar,确实是的......今天我通过docs.flutter.io/flutter/widgets/ListView-class.html 解决了这个问题,以便提供所需的滚动功能

标签: mobile dart rendering overflow flutter


【解决方案1】:

这是一个很常见的问题,尤其是当您开始在多个设备和方向上测试您的应用时。 Flutter 的 Widget 库有一个部分涵盖了各种滚动小部件:

https://flutter.io/widgets/scrolling/

我建议将您的全部内容封装在 SingleChildScrollView 中,或者使用滚动的 ListView。

编辑:这个问题和答案已经引起了一些注意,所以我想为那些登陆这里的人提供更多帮助。

Flutter SDK 团队在 SDK 代码本身的良好文档方面投入了大量精力。理解 Flex 小部件(Row 和 Column 都是 Flex 的子类)用来布置子类的算法的最佳资源之一是类本身附带的 DartDoc:

https://github.com/flutter/flutter/blob/e3005e6962cfefbc12e7aac56576597177cc966f/packages/flutter/lib/src/widgets/basic.dart#L3724

Flutter 网站还包含一个关于建筑布局的 tutorial 和一个关于如何使用 Row 和 Column 小部件的 interactive codelab。

【讨论】:

  • 如果您不希望它滚动怎么办?就我而言,我有一个 Card > ScopedModelDescendant > Column > Container。我不希望列滚动,但容器溢出它。有什么想法吗?
  • 首先,我会尝试将 Column 的 crossAxisAlignment 值设置为 stretch 或将 Container 包装在 Expanded 小部件中。这将尝试强制它成为正确的大小。如果这不起作用,您需要查看容器的子级并确定是哪一个阻止了它的大小正确。您还可以尝试使用 FittedBox,它可以缩放/剪辑内容以使其适合。
  • @forresthopkinsa 防止滚动在SingleChildScrollView 中使用physics: NeverScrollableScrollPhysics()
  • @Curly 谢谢,但我的问题已经得到解答。根本不需要 ScrollView。
  • SingleChildScrollView 有效。顺便说一句,你在 YouTube 上摇滚!
【解决方案2】:

假设您有一个 List 的 100 个 Text 小部件,如下所示:

final children = List<Widget>.generate(100, (i) => Text('Item $i')).toList();

根据设备屏幕,这些小部件可能会溢出,处理它的解决方案很少。

  1. 使用Column包裹在SingleChildScrollView中

    SingleChildScrollView(
      child: Column(children: children),
    )
    
  2. 使用ListView

    ListView(
      children: children
    )
    
  3. 同时使用Column 和ListView(你应该使用Expanded/Flexible,或者在这样做的时候给ListView 一个固定的高度)。

    Column(
      children: [
        ...children.take(2).toList(), // show first 2 children in Column
        Expanded(
          child: ListView(
            children: children.getRange(3, children.length).toList(),
          ), // And rest of them in ListView
        ),
      ],
    )
    

【讨论】:

  • body: SingleChildScrollView( child: Column(children: children), ) 解决了 A RenderFlex 底部溢出 161 像素的问题。
【解决方案3】:

尝试将Container 或任何其他Widget 包装在Flexible 或Expanded 中,它会完成。

这样做

           Flexible(
              child: Container(
                padding: EdgeInsets.all(5),
                height: 400,
                width: double.infinity,
                child: ListView.builder(
                  scrollDirection: Axis.vertical,
                  shrinkWrap: true,
                  itemCount: 4,
                  itemBuilder: (context, index) {
                    return ListTile(
                      title: Text("Tony Stark"),
                      subtitle: Text("A Iron Man"),
                    );
                  },
                ),
              ),
            )

【讨论】:

    【解决方案4】:

    我也遇到了这个问题,试试这个....

    resizeToAvoidBottomPadding: false,
    

    在身体部位之前

    【讨论】:

    • 这仅适用于您不想滚动的情况。另外下次请提供更多信息,将这行代码放入脚手架小部件中。
    • 更新:resizeToAvoidBottomPadding 已弃用。使用 resizeToAvoidBottomInset : false
    【解决方案5】:

    您可以为列内的每个容器使用小部件Expanded。然后使用flex进行正确的调整。

    【讨论】:

      【解决方案6】:

      您可以将文本包装在 PreferredSize 小部件中:

       bottom: PreferredSize(
                          preferredSize: Size.fromHeight(120.0),
                          child: Column(
                            children: <Widget>[
                              Padding(
                                padding: EdgeInsets.only(
                                    right: 5.0, left: 5.0, bottom: 2.0, top: 2.0),
                                child: Text(
                                  hospitalName,
                                  textAlign: TextAlign.left,
                                  style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      color: Colors.white,
                                      fontSize: 14.0),
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.symmetric(horizontal: 10.0),
                                child: Container(
                                  height: 1.0,
                                  width: MediaQuery.of(context).size.width,
                                  color: Colors.white,
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.only(
                                    top: 2.0, right: 5.0, left: 5.0, bottom: 2.0),
                                child: Text(
                                  doctorName,
                                  textAlign: TextAlign.left,
                                  style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      color: Colors.white,
                                      fontSize: 14.0),
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.symmetric(horizontal: 10.0),
                                child: Container(
                                  height: 1.0,
                                  width: MediaQuery.of(context).size.width,
                                  color: Colors.white,
                                ),
                              ),
                  
                              TabBar(
                                isScrollable: true,
                                tabs: [
                                  Tab(
                                    text: "Tab1",
                                  ),
                                  Tab(text: "Tab2"),
                                  Tab(text: "Tab3"),
                                ],
                              ),
                            ],
                          )),
      

      【讨论】:

      • 在某些情况下调整 Size.fromHeight(120.0) 可以修复渲染问题
      猜你喜欢
      • 2019-06-07
      • 2020-09-05
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 2021-06-21
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多