flutter 在列中展开,但引发ParentDataWidget的错误使用

3pmvbmvn  于 2023-02-13  发布在  Flutter
关注(0)|答案(1)|浏览(125)

我直接在一个列中使用FutureBuilder中的Expanded小部件,一切都很好,但是添加一个RefreshIndicator小部件似乎破坏了一些东西,我不明白为什么。

return Scaffold(
      body: Column(
        children: [
          Row(...),
          FutureBuilder(
            future: ...,
            builder: (context, snapshot) {
              return RefreshIndicator(
                onRefresh: ...,
                child: getList(snapshot.connectionState, user),
              );
            },
          ),
        ],
      ),
    );

Widget getList(ConnectionState connectionState, UserRepository user) {
    switch (connectionState) {
      case ConnectionState.done:
        return Expanded(
          child: ListView.builder(
            shrinkWrap: true,
            itemCount: number,
            itemBuilder: (BuildContext context, int index) {
              return GestureDetector(
                onTap: ...,
                child: Card(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      ...,
                      Expanded(
                        child: Container(
                          padding: ...,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                "",
                                softWrap: false,
                                overflow: TextOverflow.ellipsis,
                                style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
                              ),
                              Text(
                                "",
                                softWrap: false,
                                overflow: TextOverflow.ellipsis,
                                style: const TextStyle(fontSize: 14),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              );
            },
          ),
        );
      default:
        return const Text("other");
    }
  }

错误:

The following assertion was thrown while applying parent data.:
Incorrect use of ParentDataWidget.

The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type StackParentData.

Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically, Expanded widgets are placed directly inside Flex widgets.
The offending Expanded is currently placed inside a Stack widget.

The ownership chain for the RenderObject that received the incompatible parent data was:
  _ScrollSemantics-[GlobalKey#85a8f] ← NotificationListener<ScrollMetricsNotification> ← Scrollable ← PrimaryScrollController ← ListView ← Expanded ← NotificationListener<OverscrollIndicatorNotification> ← NotificationListener<ScrollNotification> ← Stack ← RefreshIndicator ← ⋯

我看过其他有类似错误的帖子,他们只是谈论使用列内展开,我已经在做了,所以我迷路了。我试图让我的列表视图是屏幕的剩余高度,这是工作之前添加RefreshIndicator

bd1hkmkf

bd1hkmkf1#

我找到了一个解决办法,尽管我确信这是最好的办法。
我删除了直接围绕ListViewExpanded,并在FutureBuilder内部添加了一个Column和两个Expanded

return Expanded(
    child: RefreshIndicator(
        onRefresh: () {
            return user.fetchUserEvents(client, Environment().config.baseApiUrl, user.userId);
        },
        child: Column(
            children: [
                Expanded(
                    child: getList(snapshot.connectionState, user),
                ),
            ],
        ),
    ),
);

相关问题