Fixed column and row header for DataTable on Flutter Dart. Basically it’s an horizontal scroll for the first row, a vertical scroll for the first column and a mixed horizontal and vertical scroll for the subtable. Then when you move the subtable, its controllers move the column and the row.
Here is a custom widget with an example of how to use it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
final _rowsCells = [ [7, 8, 10, 8, 7], [10, 10, 9, 6, 6], [5, 4, 5, 7, 5], [9, 4, 1, 7, 8], [7, 8, 10, 8, 7], [10, 10, 9, 6, 6], [5, 4, 5, 7, 5], [9, 4, 1, 7, 8], [7, 8, 10, 8, 7], [10, 10, 9, 6, 6], [5, 4, 5, 7, 5], [9, 4, 1, 7, 8], [7, 8, 10, 8, 7], [10, 10, 9, 6, 6], [5, 4, 5, 7, 5], [9, 4, 1, 7, 8] ]; final _fixedColCells = [ "Pablo", "Gustavo", "John", "Jack", "Pablo", "Gustavo", "John", "Jack", "Pablo", "Gustavo", "John", "Jack", "Pablo", "Gustavo", "John", "Jack", ]; final _fixedRowCells = [ "Math", "Informatics", "Geography", "Physics", "Biology" ]; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(), body: CustomDataTable( rowsCells: _rowsCells, fixedColCells: _fixedColCells, fixedRowCells: _fixedRowCells, cellBuilder: (data) { return Text('$data', style: TextStyle(color: Colors.red)); }, ), ); } class CustomDataTable<T> extends StatefulWidget { final T fixedCornerCell; final List<T> fixedColCells; final List<T> fixedRowCells; final List<List<T>> rowsCells; final Widget Function(T data) cellBuilder; final double fixedColWidth; final double cellWidth; final double cellHeight; final double cellMargin; final double cellSpacing; CustomDataTable({ this.fixedCornerCell, this.fixedColCells, this.fixedRowCells, @required this.rowsCells, this.cellBuilder, this.fixedColWidth = 60.0, this.cellHeight = 56.0, this.cellWidth = 120.0, this.cellMargin = 10.0, this.cellSpacing = 10.0, }); @override State<StatefulWidget> createState() => CustomDataTableState(); } class CustomDataTableState<T> extends State<CustomDataTable<T>> { final _columnController = ScrollController(); final _rowController = ScrollController(); final _subTableYController = ScrollController(); final _subTableXController = ScrollController(); Widget _buildChild(double width, T data) => SizedBox( width: width, child: widget.cellBuilder?.call(data) ?? Text('$data')); Widget _buildFixedCol() => widget.fixedColCells == null ? SizedBox.shrink() : Material( color: Colors.lightBlueAccent, child: DataTable( horizontalMargin: widget.cellMargin, columnSpacing: widget.cellSpacing, headingRowHeight: widget.cellHeight, dataRowHeight: widget.cellHeight, columns: [ DataColumn( label: _buildChild( widget.fixedColWidth, widget.fixedColCells.first)) ], rows: widget.fixedColCells .sublist(widget.fixedRowCells == null ? 1 : 0) .map((c) => DataRow( cells: [DataCell(_buildChild(widget.fixedColWidth, c))])) .toList()), ); Widget _buildFixedRow() => widget.fixedRowCells == null ? SizedBox.shrink() : Material( color: Colors.greenAccent, child: DataTable( horizontalMargin: widget.cellMargin, columnSpacing: widget.cellSpacing, headingRowHeight: widget.cellHeight, dataRowHeight: widget.cellHeight, columns: widget.fixedRowCells .map((c) => DataColumn(label: _buildChild(widget.cellWidth, c))) .toList(), rows: []), ); Widget _buildSubTable() => Material( color: Colors.lightGreenAccent, child: DataTable( horizontalMargin: widget.cellMargin, columnSpacing: widget.cellSpacing, headingRowHeight: widget.cellHeight, dataRowHeight: widget.cellHeight, columns: widget.rowsCells.first .map((c) => DataColumn(label: _buildChild(widget.cellWidth, c))) .toList(), rows: widget.rowsCells .sublist(widget.fixedRowCells == null ? 1 : 0) .map((row) => DataRow( cells: row .map((c) => DataCell(_buildChild(widget.cellWidth, c))) .toList())) .toList())); Widget _buildCornerCell() => widget.fixedColCells == null || widget.fixedRowCells == null ? SizedBox.shrink() : Material( color: Colors.amberAccent, child: DataTable( horizontalMargin: widget.cellMargin, columnSpacing: widget.cellSpacing, headingRowHeight: widget.cellHeight, dataRowHeight: widget.cellHeight, columns: [ DataColumn( label: _buildChild( widget.fixedColWidth, widget.fixedCornerCell)) ], rows: []), ); @override void initState() { super.initState(); _subTableXController.addListener(() { _rowController.jumpTo(_subTableXController.position.pixels); }); _subTableYController.addListener(() { _columnController.jumpTo(_subTableYController.position.pixels); }); } @override Widget build(BuildContext context) { return Stack( children: <Widget>[ Row( children: <Widget>[ SingleChildScrollView( controller: _columnController, scrollDirection: Axis.vertical, physics: NeverScrollableScrollPhysics(), child: _buildFixedCol(), ), Flexible( child: SingleChildScrollView( controller: _subTableXController, scrollDirection: Axis.horizontal, child: SingleChildScrollView( controller: _subTableYController, scrollDirection: Axis.vertical, child: _buildSubTable(), ), ), ), ], ), Row( children: <Widget>[ _buildCornerCell(), Flexible( child: SingleChildScrollView( controller: _rowController, scrollDirection: Axis.horizontal, physics: NeverScrollableScrollPhysics(), child: _buildFixedRow(), ), ), ], ), ], ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.