首页 > 解决方案 > 在垂直列表视图中添加水平列表视图时不正确使用 ParentDataWidget

问题描述

我想在水平列表视图中添加一个垂直列表视图,但它总是显示我对父数据小部件的错误使用我尝试在列小部件周围添加扩展小部件,但它不起作用我还尝试使用列收缩包装并将其设置为 true,但确实如此也不行

主页

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,
    );
  }
}

垂直 ListView 项


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,
            ),
          ),
        ],
      ),
    );
  }
}

水平 ListView 项

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 ← ⋯


    

标签: flutterlistviewdart

解决方案


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


推荐阅读