我有一个数据表,它的行数据是从rest api获得的.我希望能够 Select 一个特定的行,但所有行都被 Select 了.如何解决这个问题?

这是我的代码:

DataTable(
  columns: const <DataColumn>[
    DataColumn(label: Text('ID')),
    DataColumn(label: Text('Visitor\'s Name')),
    DataColumn(label: Text('Visit Date')),
    DataColumn(label: Text('Time In')),
    DataColumn(label: Text('Time Out')),
    DataColumn(label: Text('Department')),
    DataColumn(label: Text('Visitee')),
    DataColumn(label: Text('Address')),
    DataColumn(label: Text('Phone')),
    DataColumn(label: Text('Purpose of Visit')),
  ],
  rows: List.generate(snapshot.data!.length, (index) {
    var data = snapshot.data![index];
    return DataRow(
        selected: rowSelected,
        onSelectChanged: (index) {
// Handle row selection here
// print('Row ${rowData.id} selected: $isSelected');
          setState(() {
            rowSelected = index!;
          });
        },
        cells: [
          DataCell(Text(data.id.toString())),
          DataCell(Text(data.visitor_name.toString())),
          DataCell(Text(data.visit_date.toString())),
          DataCell(Text(data.Time_in.toString())),
          DataCell(TextFormField(initialValue: data.Time_out.toString())),
          DataCell(Text(data.Department.toString())),
          DataCell(Text(data.Visitee_name.toString())),
          DataCell(Text(data.Address.toString())),
          DataCell(Text(data.Phone.toString())),
          DataCell(Text(data.Purpose_of_visit.toString())),
        ]);
  }).toList(),
);

我try 添加一个onSelectedChanged属性,当我 Select 一行时,它会 Select 数据表中的每一行,

推荐答案

我认为你没有注意到selectedonSelectChanged类型.所以,你代码的这一部分

selected: rowSelected,
onSelectChanged: (index) {
  setState(() {
    rowSelected = index!;
  });
},

是错误的,因为selected的值是boolean,但我猜你分配的是int.

您使用的是onSelectChanged: (index) {...},您之前定义的index,但onSelectChanged上的新indexboolean.

  • 仅选定一行

现在,如果你只想得到one selected row,我想你应该得到int rowSelected = -1;.'—1'是因为一开始没有 Select 任何行,您必须执行以下操作:

selected: rowSelected == index,
onSelectChanged: (selected) {
  setState(() {
    rowSelected = selected ?? false ? index : -1;
  });
},

在这个代码rowSelected = selected ?? false ? index : -1;中,我们判断,如果selected的值可以为空,我们设置为false,然后我们判断它是否被选中.

  • 多个选定行

否则,如果你想拥有multiple selected rows,你必须将你的int rowSelected更改为List<bool> selectedRows = [],以保存所选索引的列表,代码应该是这样的:

onSelectChanged: (selected) {
  setState(() {
    selected = selected ?? false;
    if (selected! && !selectedRows.contains(index)) {
      // add to the list if selected but is not in the list
      selectedRows.add(index);
    } else if (!selected! && selectedRows.contains(index)) {
      // remove from list if not selected but is in the list
      selectedRows.remove(index);
    }
  });
},

Flutter相关问答推荐

Cupertino Buttino SheetAction on按下可触发加载指示器,然后勾选并延迟

在Flutter中为不受约束的小部件制作动画

在Flutter中创建具有固定行的压缩水平列表

使用自定义控制器将项插入到ListView中时行为不正确

将记录/元组用作SplayTreeMap中的键

如何从文本比例因子迁移到文本比例因子

Riverpod Provider.family 和 HookConsumerWidget 不刷新 UI

Flutter中有没有办法区分鼠标左右平移?

Flutter:在 pubspec.yaml 中找不到assets资源

忽略异步函数上的 context.mounted 访问 linting 错误是否安全?

Flutter 错误:OutletListModel类型的值不能分配给List类型的变量?

为什么 ref.watch(otherProvider.stream) 在 Riverpod 2.x 中被弃用并且将在 3.x 中被删除?

当项目较少时收缩列表视图

java.lang.IncompatibleClassChangeError:找到接口 com.google.android.gms.location.SettingsClient,

flutter - 无法解析 JSON 消息:文档为空

如何在 Flutter 中使用简写 IF 禁用单选按钮?

在 Android 12+ 中,REST API 调用在 flutter 中非常慢,在 WiFi 中需要 10 秒,而在移动数据中不到一秒

避免长单词中断

在 Flutter 应用程序中全局使用 assets 文件夹

单击列表视图导航到 Flutter 中的另一个页面