SingleChildScrollView 内的 Flutter Column 不滚动

问题描述 投票:0回答:4

当我尝试在 SingleScrollChildView 中使用 Card 时,出现此错误:

The following assertion was thrown during layout:
A RenderFlex overflowed by 178 pixels on the bottom.

The relevant error-causing widget was: 
  Column file:///C:/Users/emirs/AndroidStudioProjects/view_met/lib/home.dart:197:16
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.
This is considered an error condition because it indicates that there is content that cannot be seen. If the content is legitimately bigger than the available space, consider clipping it with a ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex, like a ListView.

The specific RenderFlex in question is: RenderFlex#d861f relayoutBoundary=up2 OVERFLOWING
...  needs compositing
...  parentData: offset=Offset(0.0, 0.0) (can use size)
...  constraints: BoxConstraints(0.0<=w<=411.4, 0.0<=h<=820.6)
...  size: Size(411.4, 820.6)
...  direction: vertical
...  mainAxisAlignment: start
...  mainAxisSize: max
...  crossAxisAlignment: center
...  verticalDirection: down
◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤

这是我的代码:

Padding(
                padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
                child: Column(
                  children: <Widget>[
                    Text("Random Items You Could Like", style: GoogleFonts.merriweather(fontSize: 18, color: Colors.black)),
                    SingleChildScrollView(
                      scrollDirection: Axis.vertical,
                      child: Column(
                       children: <Widget>[
                         builder(randint1.toString()),
                         builder(randint2.toString()),
                         builder(randint3.toString()),
                         builder(randint4.toString()),
                         builder(randint5.toString()),
                       ],
                      )
                    ),
                  ],
                ),
              )

这是

builder()
功能:

builder(String id) {
      return FutureBuilder(
        future: fetchData(id),
        builder: (BuildContext context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Padding(
              padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
              child: CircularProgressIndicator(),
            );
          }
          var data = jsonDecode(snapshot.data.toString());

          var leading;
          var artist;


          if (data["primaryImageSmall"] == "") {
            leading = Icon(Icons.dangerous);
          }
          else {
            leading = Image.network(data["primaryImageSmall"]);
          }

          if (data["artistDisplayName"]== "") {
            artist = "Unknown";
          }
          else {
            artist = data["artistDisplayName"];
          }

          return Card(
            clipBehavior: Clip.antiAlias,
            child: Column(
              children: [
                ListTile(
                  leading: leading,
                  title: Text(data["title"]),
                  subtitle: Text(
                    "by $artist",
                    style: TextStyle(color: Colors.black.withOpacity(0.6)),
                  ),
                ),
                ButtonBar(
                  alignment: MainAxisAlignment.start,
                  children: [
                    TextButton(
                      onPressed: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(builder: (context) => DetailsPage()),
                        );
                      },
                      child: Text("Details", style: TextStyle(color: Color(0xFF6200EE))),
                    ),
                    TextButton(
                      onPressed: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(builder: (context) => DetailsPage()),
                        );
                      },
                      child: Text("Add to Favorites", style: TextStyle(color: Color(0xFF6200EE))),
                    ),
                  ],
                ),
              ],
            ),
          );
        },
      );
    }

这是结果:

我也查看了其他问题,但没有一个真正有帮助,而且他们没有我正在寻找的答案。我现在真的不知道该怎么办。我将不胜感激任何类型的帮助。如果需要的话我可以提供更多信息。

flutter dart material-design
4个回答
11
投票

SingleChildScrollView
包裹
Expanded

Column(
  children: [
    // ...
    Expanded(
      child: SingleChildScrollView(),
    ),
  ],
),

1
投票

我认为您错误地使用了

SingleChildScrollView
,删除
SingleChildScrollView
和scrollDirection;用它来包裹这部分:

Column(
                          children: <Widget>[
                            builder(randint1.toString()),
                            builder(randint2.toString()),
                            builder(randint3.toString()),
                            builder(randint4.toString()),
                            builder(randint5.toString()),
                          ],

它看起来像这样:

SingleChildScrollView(
                    scrollDirection: Axis.vertical,
Column(
                          children: <Widget>[
                            builder(randint1.toString()),
                            builder(randint2.toString()),
                            builder(randint3.toString()),
                            builder(randint4.toString()),
                            builder(randint5.toString()),
                          ],
),

0
投票

尝试以下代码

 Padding(
              padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
              child: Stack(
                children: [
                  Align(
                    alignment:Alignment.topCenter,
                      child: Text("Random Items You Could Like", style: GoogleFonts.merriweather(fontSize: 18, color: Colors.black))),
                  SingleChildScrollView(
                    scrollDirection: Axis.vertical,
                    child: Column(
                      children: <Widget>[
                        Column(
                          children: <Widget>[
                            builder(randint1.toString()),
                            builder(randint2.toString()),
                            builder(randint3.toString()),
                            builder(randint4.toString()),
                            builder(randint5.toString()),
                          ],
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            )

0
投票

以下代码对我有用。使用以下代码,您可以在列内使用 SingleChildScrollView 。

Column(
children :[
    Expanded(
                child: SingleChildScrollView(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text("Monat".toUpperCase(),
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 20,
                          )),
              ],
    ),
    ),
],
);
© www.soinside.com 2019 - 2024. All rights reserved.