You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Polls最后一项缺失Icon按钮,报ParentDataWidget使用错误

问题分析与解决方案

错误根源

  1. 「Incorrect use of ParentDataWidget」错误:代码里把Expanded直接嵌套在Container内部,而Expanded仅能作为Row/Column/Flex的直接子组件,这种错误的层级结构会触发ParentDataWidget异常。
  2. 最后一个投票项底部元素不显示:外层Container设置了固定高度MediaQuery.of(context).size.height,加上SingleChildScrollView和ListView的嵌套导致布局溢出,最后一个投票项的底部区域被遮挡;同时错误的Expanded也打乱了布局结构,影响了metaWidget的正常渲染。

修复步骤

1. 移除错误的Expanded组件

删除Container内部的Expanded,ListView.builder不需要被Expanded包裹,直接作为Container的子组件即可。

2. 调整布局结构,解决滚动冲突与遮挡问题

  • 删掉Container的height: MediaQuery.of(context).size.height设置,让容器根据内容自适应高度。
  • 给ListView.builder添加shrinkWrap: true,让列表根据子项总高度自适应,同时移除外层多余的SingleChildScrollView(因为ListView本身支持滚动,嵌套会导致滚动冲突)。

修改后的核心代码片段

Widget build(BuildContext context) {
  return Stack(children: [
    Provider.of<PollsProvider>(context).isLoading
        ? const Center(child: CircularProgressIndicator())
        : Container(
            padding: const EdgeInsets.all(20),
            child: ListView.builder(
              shrinkWrap: true,
              itemCount: pollsList.length,
              itemBuilder: (BuildContext context, int index) {
                var polaI = pollsList[index];
                bool voted = hasVotedFunc(polaI);
                votedTo = getHasVotedTo(polaI);

                return Provider.of<PollsProvider>(context).isLoading
                    ? const Center(child: CircularProgressIndicator())
                    : Container(
                        margin: const EdgeInsets.only(bottom: 20),
                        child: Stack(
                          children: [
                            FlutterPolls(
                              pollId: polaI.id.toString(),
                              hasVoted: voted,
                              userVotedOptionId: votedTo,
                              onVoted: (PollOption pollOption, int newTotalVotes) async {
                                polaI.options[pollOption.id].votes += 1;
                                polaI.options[pollOption.id].voters.add(
                                  Provider.of<AuthService>(context, listen: false)
                                      .currentUser?.email ?? ''
                                );
                                await Provider.of<PollsProvider>(context, listen: false)
                                    .updatePoll(polaI);
                                return true;
                              },
                              pollTitle: Align(
                                alignment: Alignment.centerLeft,
                                child: Text(
                                  polaI.question,
                                  style: const TextStyle(
                                    fontSize: 14,
                                    fontWeight: FontWeight.w600,
                                  ),
                                ),
                              ),
                              pollOptions: List<PollOption>.from(
                                polaI.options.map((option) => PollOption(
                                  id: option.id,
                                  title: Text(
                                    option.title,
                                    style: const TextStyle(
                                      fontSize: 14,
                                      fontWeight: FontWeight.w600,
                                    ),
                                  ),
                                  votes: option.votes,
                                ))
                              ),
                              votedPercentageTextStyle: const TextStyle(
                                fontSize: 14,
                                fontWeight: FontWeight.w600,
                              ),
                              metaWidget: Row(
                                children: [
                                  const SizedBox(width: 6),
                                  const Text('•'),
                                  const SizedBox(width: 6),
                                  IconButton(
                                    icon: const Icon(Icons.replay_outlined),
                                    onPressed: () => DeleteVote(polaI),
                                  ),
                                  const SizedBox(width: 6),
                                ],
                              ),
                            ),
                            Align(
                              alignment: Alignment.topRight,
                              child: PopupMenuButton(
                                itemBuilder: (context) => [
                                  const PopupMenuItem(value: 'Update', child: Text('Update')),
                                  const PopupMenuItem(value: 'Delete', child: Text('Delete')),
                                ],
                                onSelected: (String value) => actionPopUpItemSelected(value, polaI),
                                child: const Icon(Icons.more_vert),
                              ),
                            ),
                          ],
                        ),
                      );
              },
            ),
          ),
    Positioned(
      bottom: 20,
      right: 20,
      child: FloatingActionButton(
        onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (_) => AddPollPage())),
        child: const Icon(Icons.add),
      ),
    ),
  ]);
}

额外优化点

  • 移除了外层多余的SingleChildScrollView,避免滚动冲突与性能损耗。
  • 给voters.add的内容添加?? '',处理currentUser?.email为空的情况,避免报错。

内容的提问来源于stack exchange,提问作者Eya Rais

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 03:34:57