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

Flutter ListView卡片侧滑功能报错:缺少required参数child

问题解决:Dismissible组件必填child参数报错

错误原因

你遇到的报错核心是Dismissible组件必须传入child参数,用来指定可触发滑动操作的内容容器。你的代码中把Dismissible和Card作为ListView的两个独立子项,没有将Card绑定为Dismissible的子组件,导致框架找不到必填的child参数。

修改后的完整代码

import 'package:flutter/material.dart';

class Page3 extends StatelessWidget { // 修正类名,符合Dart命名规范
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: const Color.fromARGB(255, 255, 106, 0),
        toolbarHeight: 51,
        title: const Text('my title here'),
      ),
      body: Container(
        alignment: Alignment.center,
        padding: const EdgeInsets.all(1),
        child: Column(
          children: [
            const TextField(
              textAlign: TextAlign.center,
              style: TextStyle(
                  color: Color.fromARGB(255, 255, 111, 0),
                  fontWeight: FontWeight.w600),
              decoration: InputDecoration(
                prefixIcon: Icon(Icons.search),
              ),
            ),
            Expanded(
              child: ListView(
                children: <Widget>[
                  const Padding(padding: EdgeInsets.symmetric(horizontal: 9)),
                  // 核心修改:用Dismissible包裹Card,将Card作为child传入
                  Dismissible(
                    key: UniqueKey(),
                    background: Container(
                      color: Colors.red,
                      alignment: Alignment.centerRight,
                      child: const Icon(Icons.delete, color: Colors.white),
                    ),
                    secondaryBackground: Container(
                      color: Colors.green,
                      alignment: Alignment.centerLeft,
                      child: const Icon(Icons.archive, color: Colors.white),
                    ),
                    onDismissed: (direction) {
                      if (direction == DismissDirection.endToStart) {
                        ScaffoldMessenger.of(context).showSnackBar(
                          const SnackBar(content: Text('删除操作!')),
                        );
                      } else if (direction == DismissDirection.startToEnd) {
                        ScaffoldMessenger.of(context).showSnackBar(
                          const SnackBar(content: Text('归档操作')),
                        );
                      }
                    },
                    child: Card(
                      margin: const EdgeInsets.all(3),
                      elevation: 1,
                      color: const Color.fromARGB(255, 250, 195, 195),
                      child: ListTile(
                        onTap: () {
                          ScaffoldMessenger.of(context).showSnackBar(
                              const SnackBar(content: Text('点击了列表项!')));
                        },
                        title: const Text("示例标题"),
                        subtitle: const Text("示例副标题"),
                        trailing: const Icon(Icons.arrow_forward),
                        leading: const CircleAvatar(
                          backgroundImage: AssetImage("images/defphoto.png"),
                        ),
                      ),
                    ),
                  ),
                  // 其他需要滑动操作的卡片,重复上述结构即可
                  // Dismissible(
                  //   key: UniqueKey(),
                  //   ...
                  //   child: Card(...),
                  // )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}

额外说明

如果后续需要实现真正的学生数据删除/归档逻辑,建议将StatelessWidget改为StatefulWidget:

  • 用List存储学生信息列表
  • 在onDismissed回调中移除对应列表项
  • 调用setState()刷新页面,完成UI和数据的同步

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 22:23:14