【发布时间】: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 ← ⋯
【问题讨论】: