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

Flutter中如何为DataRow处理双击事件?

为DataTable每行实现双击事件的方案

要实现DataTable每行的双击事件,直接用InkWell包裹整个表格无法获取行索引和内容,你可以通过给每行的每个单元格添加GestureDetector并绑定双击回调来解决,这样点击行内任意位置都能触发事件,同时拿到对应行的索引和数据。

以下是修改后的完整代码:

DataTable(
  onSelectAll: (val) {
    setState(() {
      selectedIndex = -1;
    });
  },      
  border: TableBorder.all(color: textColor),
  columnSpacing: 20,
  showCheckboxColumn: false,
  headingRowHeight: 32,
  headingRowColor: MaterialStateColor.resolveWith((states) => mainColor),
  dataRowMinHeight: 40,
  dataRowMaxHeight: 40,
  columns: const <DataColumn>[
    DataColumn(label: Text("Cognome", style: TextStyle(color: Colors.white),)),
    DataColumn(label: Text("Nome", style: TextStyle(color: Colors.white),)),
    DataColumn(label: Text("Email", style: TextStyle(color: Colors.white),)),
    DataColumn(label: Text("Telefono", style: TextStyle(color: Colors.white),)),
    DataColumn(label: Text("Ore/Sett", style: TextStyle(color: Colors.white))),
    DataColumn(label: Text("Disp./Sett", style: TextStyle(color: Colors.white))),
  ],
  rows: List<DataRow>.generate(
    snapshot.data!.length, (index) {
      // 获取当前行的数据
      final docente = snapshot.data![index];
      // 定义双击回调逻辑
      void _onRowDoubleTap() {
        setState(() {
          selectedIndex = index;
        });
        // 这里可以添加双击后的业务逻辑,比如跳转详情页、弹出编辑框等
        context.read<GestioneDocentiProvider>().setSelectedRow(index);
        context.read<GestioneDocentiProvider>().setDocenteSelezionato(docente);
        context.read<GestioneOrarioCompletoProvider>().setDocenteSelezionato(docente);
        context.read<GestioneDocentiProvider>().impostaSelezioneRiga();
        context.read<GestioneOrarioCompletoProvider>().impostaOrario();
      }

      return DataRow(
        color: MaterialStateProperty.resolveWith((states) {
          if(states.contains(MaterialState.selected)) {
            return accentColor50;
          }
          if(index % 2 == 0) {
            return mainColor200;
          }
          return null;
        }),
        selected: index == selectedIndex,
        onSelectChanged: (value) {
          setState(() {
            selectedIndex = index;
          });
          context.read<GestioneDocentiProvider>().setSelectedRow(index);
          context.read<GestioneDocentiProvider>().setDocenteSelezionato(docente);
          context.read<GestioneOrarioCompletoProvider>().setDocenteSelezionato(docente);
          context.read<GestioneDocentiProvider>().impostaSelezioneRiga();
          context.read<GestioneOrarioCompletoProvider>().impostaOrario();
        },
        cells: <DataCell>[
          // 给每个单元格添加GestureDetector,绑定同一双击回调
          DataCell(GestureDetector(
            onDoubleTap: _onRowDoubleTap,
            child: Text(docente.cognome),
          )),
          DataCell(GestureDetector(
            onDoubleTap: _onRowDoubleTap,
            child: Text(docente.nome),
          )),
          DataCell(GestureDetector(
            onDoubleTap: _onRowDoubleTap,
            child: Text(docente.email),
          )),
          DataCell(GestureDetector(
            onDoubleTap: _onRowDoubleTap,
            child: Text(docente.telefono),
          )),                                  
          DataCell(GestureDetector(
            onDoubleTap: _onRowDoubleTap,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                docente.oreSettimanali, 
                style: docente.oreSettimanali == "0"
                ? TextStyle(color: Colors.red[400], fontWeight: FontWeight.bold)
                : const TextStyle(color: textColor)
              ),
            ),
          )),
          DataCell(GestureDetector(
            onDoubleTap: _onRowDoubleTap,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                docente.oreDoc, 
                style: docente.oreDoc == "0"
                ? TextStyle(color: Colors.red[400], fontWeight: FontWeight.bold)
                : const TextStyle(color: textColor)
              ),
            ),
          ))
        ],
      );
    }
  ),
),

关键修改点:

  • 提前获取当前行的docente数据,避免重复调用snapshot.data![index]
  • 定义统一的_onRowDoubleTap回调函数,集中处理双击后的逻辑
  • 给每个DataCell的子组件包裹GestureDetector并绑定onDoubleTap到统一回调,确保点击行内任意位置都能触发双击事件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 12:23:22