在垂直列表视图中添加水平列表视图时不正确使用 ParentDataWidget

Posted

技术标签:

【中文标题】在垂直列表视图中添加水平列表视图时不正确使用 ParentDataWidget【英文标题】:Incorrect use of ParentDataWidget when adding horizontal listview inside vertical listview 【发布时间】:2021-01-13 08:14:48 【问题描述】:

我想在水平 listView 中添加一个垂直 listView,但它总是显示我对父数据小部件的使用不正确 我尝试在 Column 小部件周围添加扩展小部件,但它不起作用但它也不起作用

主页

import 'package:provider/provider.dart';

class TeamSearchResultScreen extends StatelessWidget 
  static const routeName = 'team-search-result-screen';
  @override
  Widget build(BuildContext context) 
    final loadedTeams = Provider.of<Teams>(context);
    final loadedCandidates = Provider.of<Candidates>(context);
    return ListView.builder(
      itemBuilder: (ctx, index) => TeamsItemCard(
        teamName: loadedTeams.teams[index].teamName,
        district: loadedTeams.teams[index].teamDistrict,
      ),
      itemCount: loadedTeams.teamsCount,
    );
  

垂直列表视图项


import 'package:election/provider/candidates.dart';
import 'package:election/widgets/candidate_item_card.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class TeamsItemCard extends StatelessWidget 
  final String teamName;
  final String district;
  TeamsItemCard(this.teamName, this.district);
  @override
  Widget build(BuildContext context) 
    final loadedCandidates = Provider.of<Candidates>(context);
    return Expanded(
      child: Column(
        children: [
          Row(
            children: [
              Text(
                teamName,
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
              ),
              SizedBox(
                width: 10,
              ),
              Text(
                '-',
                style: TextStyle(fontSize: 18),
              ),
              SizedBox(
                width: 10,
              ),
              Text(
                district,
                style: TextStyle(fontSize: 16),
              ),
            ],
          ),
          ListView.builder(
            shrinkWrap: true,
            scrollDirection: Axis.horizontal,
            itemCount: loadedCandidates.candidatesCount,
            itemBuilder: (ctx, index) => CandidateItemCard(
              name: loadedCandidates.candidates[index].firstName,
              profile: loadedCandidates.candidates[index].image,
            ),
          ),
        ],
      ),
    );
  


水平列表视图项

import 'package:flutter/material.dart';

class CandidateItemCard extends StatelessWidget 
  final IconData profile;
  final String name;

  CandidateItemCard(this.profile, this.name);

  @override
  Widget build(BuildContext context) 
    return Column(
      children: [
        Icon(
          profile,
          size: 16,
        ),
        Text(
          'name',
          style: TextStyle(fontSize: 16),
        ),
      ],
    );
  

错误信息

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 ParentData.
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 RepaintBoundary widget.
The ownership chain for the RenderObject that received the incompatible parent data was:
  Column ← Expanded ← TeamsItemCard ← RepaintBoundary ← IndexedSemantics ←
NotificationListener<KeepAliveNotification> ← KeepAlive ← AutomaticKeepAlive ← KeyedSubtree ←
SliverList ← ⋯


    

【问题讨论】:

【参考方案1】:

ExpandedFlexible 小部件必须位于列或行内。 更多信息,你可以参考这个https://api.flutter.dev/flutter/widgets/Expanded-class.html和https://api.flutter.dev/flutter/widgets/Flexible-class.html 您不能在此之外使用并使用扩展包装您的列表视图构建器并将物理信息提供给NeverScrollableScrollPhysics()

【讨论】:

【参考方案2】:

我通过用容器包装列表视图并赋予它高度来解决它

【讨论】:

以上是关于在垂直列表视图中添加水平列表视图时不正确使用 ParentDataWidget的主要内容,如果未能解决你的问题,请参考以下文章

我们如何在不使用 recyclerview 的情况下创建水平列表视图?

如何使用下面的类从web服务中动态读取下面给出的JSON数据,并为收到的数据创建水平列表视图。

自定义滚动谷歌颤动中的水平列表视图

全屏视图在 HStack 和水平滚动视图中不起作用

在垂直堆栈视图中具有自动项目大小的水平分页 UICollectionView?

使用 HTML 助手时不正确的列表模型绑定索引