在Flutter中使DataTable双向滚动

kir*_*oud 5 flutter flutter-layout

如何使DataTable双向滚动。我使数据表水平滚动,但是列表很大,无法向下滚动。

@override 
Widget build(BuildContext context) { 
    return Scaffold(
      appBar: AppBar(title: Text("Bills Receivable"),),
      body:SingleChildScrollView( 
        scrollDirection: Axis.horizontal, 
        child:
           DataTable(
          columns: <DataColumn>[
            DataColumn(label:Text("BNm",style: TextStyle(fontWeight: FontWeight.bold),)),
            DataColumn(label:Text("BDt",style: TextStyle(fontWeight: FontWeight.bold),)),
            DataColumn(label:Text("BPrty",style: TextStyle(fontWeight: FontWeight.bold),)),
            DataColumn(label:Text("BdueDt",style: TextStyle(fontWeight: FontWeight.bold),)),
            DataColumn(label:Text("Dys",style: TextStyle(fontWeight: FontWeight.bold),)),
            DataColumn(label:Text("BAmt",style: TextStyle(fontWeight: FontWeight.bold),)),
            DataColumn(label:Text("BPAmt",style: TextStyle(fontWeight: FontWeight.bold),)),
            DataColumn(label:Text("BBAmt",style: TextStyle(fontWeight: FontWeight.bold),))
          ], rows:  widget.rdata.bRecDtl.map((e)=>
            DataRow(
              cells:<DataCell>[
                DataCell(Text(e.bNm.toString())),
                DataCell(Text(e.bDt.toString())),
                DataCell(Text(e.bPrty.toString())),
                DataCell(Text(e.bdueDt.toString())),
                DataCell(Text(e.dys.toString())), 
                DataCell(Text(e.bAmt.toString())),
                DataCell(Text(e.bPAmt.toString())),
                DataCell(Text(e.bBAmt.toString())),
          ])).toList()
        ),
      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)

Luc*_*ach 51

Just add two SingleChildScrollViews:

child: SingleChildScrollView(
         scrollDirection: Axis.vertical,
         child: SingleChildScrollView(
           scrollDirection: Axis.horizontal,
           child: DataTable()
Run Code Online (Sandbox Code Playgroud)

  • 水平滚动的滚动条未显示在 futter web 中,如何使其可见? (3认同)

Exo*_*uid 16

更新更简单的方法。

从最近的 Flutter 更新开始,您还可以将您DataTableInteractiveViewer小部件包装在constrained属性设置为的小部件中false。与早期的解决方案不同,这将允许您同时水平和垂直滚动。它也是更少的代码,并允许您放大/缩小您的数据表。

祝你好运!

  • 由于某种原因,与“SingleChildScrollView”相比,“InteractiveViewer”会影响大数据表中的性能。然而 InteractiveViewer 具有更多更好的可用性。希望几个版本之后情况会有所改变 (4认同)
  • 在 Flutter Web 上测试。主要问题是滚动绑定到“InteractiveViewer”的缩放/缩放。选项中包含“scaleEnable = false”的事件,您将无法在表格上垂直滚动。您必须进行平移,这在网络上并不理想。 (2认同)

小智 5

如果这里有人使用 PaginatoredDataTable 或 PaginatoredDataTable2,则需要设置 minWidth 属性才能使水平滚动正常工作。