【问题标题】:How to pass multiple widgets as children in Flutter? [duplicate]如何在 Flutter 中将多个小部件作为子级传递? [复制]
【发布时间】:2018-11-14 19:35:47
【问题描述】:

我最近开始学习 Flutter,并且一直在阅读文档。我正在开发这个小应用程序,屏幕顶部有一个按钮,下面有一个列表。

每当我将带有ListView 小部件的RaisedButton 传递到另一个ListViewColumn 小部件时,它都会抛出错误。

I/flutter ( 4734): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 4734): The following assertion was thrown during performResize():
I/flutter ( 4734): Vertical viewport was given unbounded height.
I/flutter ( 4734): Viewports expand in the scrolling direction to fill their container.
////MORE LINES OF ERRORS/////

这是我一直在处理的代码:

import 'package:flutter/material.dart';

void main() {
  runApp(ListDemo(
    items: new List<ListItem>.generate(
      100,
          (i) => i % 6 == 0
          ? new HeadingItem("Heading $i")
          : new MessageItem("Sender $i", "Message body $i"),
    ),
  ));
}

// The base class for the different types of items the List can contain
abstract class ListItem {}

// A ListItem that contains data to display a heading
class HeadingItem implements ListItem {
  final String heading;

  HeadingItem(this.heading);
}

// A ListItem that contains data to display a message
class MessageItem implements ListItem {
  final String sender;
  final String body;

  MessageItem(this.sender, this.body);
}

class ListDemo extends StatelessWidget {
  final List<ListItem> items;
  ListDemo({Key key, @required this.items}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    final ListView listView = ListView.builder(
      itemCount: items.length,
      itemBuilder: (context, index) {
        final item = items[index];

        if (item is HeadingItem) {
          return new ListTile(
            title: new Text(
              item.heading,
              style: Theme.of(context).textTheme.headline,
            ),
          );
        } else if (item is MessageItem) {
          return new ListTile(
            title: new Text(item.sender),
            subtitle: new Text(item.body),
          );
        }
      },
    );

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Lists'),
        ),
        body: ListView( //Tried using ListView, Column.. None of them help solving the issue
          children: <Widget>[
            RaisedButton(
              onPressed: null,
              child: Text('Sample Button'),
            ),
            Container(
              child: listView,
            )
        ]
      )
      )
    );
  }
}

请帮我解决这个让知道的问题,如何传递多个孩子,也请理解这个概念。

已编辑

其中一个可能的解决方案建议使用 Expanded 类包装 ListView。当我这样做时,它给我一个错误,如下所示:

I/flutter ( 4190): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 4190): The following assertion was thrown building NotificationListener<KeepAliveNotification>:
I/flutter ( 4190): Incorrect use of ParentDataWidget.
I/flutter ( 4190): Expanded widgets must be placed inside Flex widgets.
I/flutter ( 4190): Expanded(no depth, flex: 1, dirty) has no Flex ancestor at all.

所以我将整个 Widget 代码封装在 Flex 中,如下所示:

      Flex(
        direction: Axis.vertical,
        children: <Widget>[
          ListView(
            children: <Widget>[
              RaisedButton(
               onPressed: null,
               child: Text('Snackbar'),
              ),
              Expanded(
               child: listView
              )
             ],
            )
          ],
        )

但后来它给了我这个错误:

I/flutter ( 4388): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 4388): The following assertion was thrown during performResize():
I/flutter ( 4388): Vertical viewport was given unbounded height.
I/flutter ( 4388): Viewports expand in the scrolling direction to fill their container.In this case, a vertical
I/flutter ( 4388): viewport was given an unlimited amount of vertical space in which to expand. This situation
I/flutter ( 4388): typically happens when a scrollable widget is nested inside another scrollable widget.

【问题讨论】:

  • @RémiRousselet 建议的解决方案似乎没有多大帮助。
  • 星光,你是我的英雄。虽然Flex( direction: Axis.vertical, children: &lt;Widget&gt;[... 对您不起作用,但该修复对我完全有效。非常感谢。

标签: dart flutter flutter-layout


【解决方案1】:

这里已经回答了这个问题

Can't add a ListView in Flutter

如果您使用 scrollable view(Listview) inside another scrollable viewinner scrollable view 不知道 how much height it should occupy。您可以使用 Expanded 小部件告诉内部可滚动视图应该占用多少高度。

【讨论】:

  • 如果该问题已被回答,则标记为重复而不是再次回答
  • 他要求解释为什么会发生这种情况。那里我没有给出任何解释。这就是为什么我将其添加为答案
  • 但是您可以使用更多信息编辑原始答案,然后将其标记为重复。一般来说,如果问题的答案相同,则会被视为重复问题。
  • @VinothKumar 该解决方案似乎无法解决完整的问题,它引发了一个新错误。我把实现和导致的错误放在上面Edited部分。
  • 为什么不能用 Column 而不是 ListView?有什么具体目的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 2018-08-19
  • 1970-01-01
相关资源
最近更新 更多