【问题标题】:Flutter about Dismissible classFlutter 关于 Dismissible 类
【发布时间】:2021-09-23 17:25:26
【问题描述】:

首先,我有 CheckList 类,其中我有列表 checkLists 和许多其他变量,如名称、位置等,在这个列表中我有 CheckList 对象。

所以在 Dismissible 键中我通过了 CheckList.checkLists[index] 但我得到了这个错误

不能将参数类型“CheckList”分配给参数类型“Key”

Scaffold(
      body: Container(
        padding: EdgeInsets.all(14.0),
        child: ListView.separated(
          itemCount: CheckList.checkLists.length,
          itemBuilder: (BuildContext Context, int index){
            return Dismissible(
              key: CheckList.checkLists[index],
              child: InkWell(
                
              ),
            );
          },
          separatorBuilder: (BuildContext context, int index) {
            return Container(
              height: 14,
            );
          }),    
      ),
  );

我需要将什么传递给密钥?

第二个问题

ListView 中的项目必须如下所示。这是我应用程序中其他 ListView 的项目,但我需要与新 ListView 项目中的其他数据相同

我的 Dismissble 项目会用这种类型的代码正常关闭吗?

Scaffold(           
                  body:
                  Container(
                   padding: EdgeInsets.all(14.0),
                  child: ListView.separated(
                    itemCount: CheckList.checkListtemplateXmlList.length,
                    itemBuilder: (BuildContext context, int index) {
                    return InkWell(
                      onTap: (){
                              CheckList checkList = CheckList();
                                CheckList.addCheckList(index,checkList);
                                showDialog(context: context, builder: (BuildContext  context){
                                  return AlertDialog(
                                    title: Text('Добавить описание'),
                                    content: TextField(
                                      onChanged: (String value) async{
                                        checkList.description = value;
                                      },
                                    ),
                                    actions: [
                                      ElevatedButton(onPressed: () async{
                                        await checkList.writeToFile();
                                        setState(() {
                                          CheckList.checkLists.add(checkList);
                                        });
                                        Navigator.pushNamedAndRemoveUntil(context, '/',(route) => false );
                                      },
                                          child: Text('Добавить'))
                                    ],
                                  );
                                });
                      },
                      child: Container(
                      decoration: BoxDecoration(
                        border: Border.all(
                        color: Color.fromARGB(255, 141, 166, 255), width: 1),
                        borderRadius: BorderRadius.circular(10)),
                      width: 50,
                      height: 80,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                          "${CheckList.checkListTempaltexmlNameList[index]}",
                          style: TextStyle(fontSize: 40),
                  )
                ],
              ),
            ),
                    );
          },
          separatorBuilder: (BuildContext context, int index) {
            return Container(
              height: 14,
            );
          }),
    ),
            );

【问题讨论】:

  • 我回答了你的第一个问题。为清楚起见,您应该编辑第二个问题并将其移至新问题。

标签: flutter dart


【解决方案1】:

key 参数需要一个 Key 对象。 你可以通过

  • UniqueKey() 会自动为您生成唯一密钥
  • ValueKey(dynamic value) 如果您想从某个变量生成一个。在你的情况下,那将是 ValueKey(CheckList.checkLists[index])

【讨论】:

    猜你喜欢
    • 2018-05-23
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 2019-09-10
    • 2018-07-07
    • 2020-09-10
    • 2019-12-22
    相关资源
    最近更新 更多