简体   繁体   中英

Incorrect use of ParentDataWidget when adding horizontal listview inside vertical listview

I want to add a vertical listView inside horizontal listView but it always shows me incorrect use of parent data widget I have tried adding expanded widget around Column widget but it does not work I also tried shrink wrap with column and set it to true but it did not work either

Main Page

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

Vertical ListView Item


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

Horizontal ListView Item

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

Error Message

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


    

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

Expanded or Flexible widget must be inside a column or row. for more info, you can refer this https://api.flutter.dev/flutter/widgets/Expanded-class.html and https://api.flutter.dev/flutter/widgets/Flexible-class.html you can not use outside of that and wrap your listview builder with expanded and give physics to NeverScrollableScrollPhysics() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM