Updated 31 January 2024
Using Data Table in Flutter class is a widget to display data in a table is expensive, because to lay out the table all the data must be measured twice, once to negotiate the dimensions to use for each column, and a Row is used to set the values of the columns.
You can find out more about the Flutter app development services page.
1.) Create a Scaffold.
2.) Use the Data Table widget.
Inside Scaffold widgets, it Implements the basic material design visual layout structure. First, initialize the main app as a stateless widget.
This class provides APIs for showing drawers and bottom sheets. We can add the background color inside the scaffold widget.
It also supports special Material Design components, such as Drawers, AppBars, and SnackBars.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'Selectable Widget Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: _DataTableDemo(), ); } } |
Data Table is suggested that you use a PaginatedDataTable which automatically splits the data into multiple pages.
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 |
bool sort = true; List<int> sno = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; List<String> itemList = [ 'Student', 'Employee', 'Tester', 'Developer', 'UI Designer', 'Manager', 'Software', 'Flutter', 'Data', 'C', 'Swift', 'C++', 'Python', 'Magento', 'CS-Cart' ]; onSort(int columnIndex, bool ascending) { if (columnIndex == 0) { if (ascending) { sno.sort((a, b) => a.compareTo(b)); itemList = itemList.reversed.toList(); } else { sno.sort((b, a) => a.compareTo(b)); itemList = itemList.reversed.toList(); } } } |
However, For this reason, if you have a lot of data (say, more than a dozen rows with a dozen columns, though the precise limits depend on the target device), it is suggested that you use a PaginatedDataTable which automatically splits the data into multiple pages.
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 224 225 226 227 |
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'Data Table Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: _DataTableDemo(), ); } } class _DataTableDemo extends StatelessWidget { bool sort = true; List<int> sno = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; List<String> itemList = [ 'Student', 'Employee', 'Tester', 'Developer', 'UI Designer', 'Manager', 'Software', 'Flutter', 'Data', 'C', 'Swift', 'C++', 'Python', 'Magento', 'CS-Cart' ]; onSort(int columnIndex, bool ascending) { if (columnIndex == 0) { if (ascending) { sno.sort((a, b) => a.compareTo(b)); itemList = itemList.reversed.toList(); } else { sno.sort((b, a) => a.compareTo(b)); itemList = itemList.reversed.toList(); } } } @override Widget build(BuildContext context) { return Stack( children: [ Scaffold( backgroundColor: Colors.white, appBar: commonAppBar( Utils.getStringValue(context, "Data Table Demo"), context, isLeadingEnable: false, onPressed: () { Navigator.pop(context); }), body: SingleChildScrollView( scrollDirection: Axis.horizontal, child: SingleChildScrollView( scrollDirection: Axis.vertical, child: Padding( padding: const EdgeInsets.all(8.0), child: DataTable( sortColumnIndex: 0, sortAscending: sort, columns: [ DataColumn( label: const Text('No.'), numeric: true, onSort: (int columnIndex, bool ascending) { setState(() { sort = !sort; }); onSort(columnIndex, ascending); }), const DataColumn(label: Text('Data Table-1')), const DataColumn(label: Text('Data Table-2')), const DataColumn(label: Text('Data Table-3')), ], rows: [ DataRow( cells: [ DataCell(Text('${sno[0]}')), DataCell(Text(itemList[0])), DataCell(Text(itemList[0])), DataCell(Text(itemList[0])), ], ), DataRow( cells: [ DataCell(Text('${sno[1]}')), DataCell(Text(itemList[1])), DataCell(Text(itemList[1])), DataCell(Text(itemList[1])), ], ), DataRow( cells: [ DataCell(Text('${sno[2]}')), DataCell(Text(itemList[2])), DataCell(Text(itemList[2])), DataCell(Text(itemList[2])), ], ), DataRow( cells: [ DataCell(Text('${sno[3]}')), DataCell(Text(itemList[3])), DataCell(Text(itemList[3])), DataCell(Text('${itemList[3]}')), ], ), DataRow( cells: [ DataCell(Text('${sno[4]}')), DataCell(Text(itemList[4])), DataCell(Text(itemList[4])), DataCell(Text(itemList[4])), ], ), DataRow( cells: [ DataCell(Text('${sno[5]}')), DataCell(Text(itemList[5])), DataCell(Text(itemList[5])), DataCell(Text(itemList[5])), ], ), DataRow( cells: [ DataCell(Text('${sno[6]}')), DataCell(Text(itemList[6])), DataCell(Text(itemList[6])), DataCell(Text(itemList[6])), ], ), DataRow( cells: [ DataCell(Text('${sno[7]}')), DataCell(Text(itemList[7])), DataCell(Text(itemList[7])), DataCell(Text(itemList[7])), ], ), DataRow( cells: [ DataCell(Text('${sno[8]}')), DataCell(Text(itemList[8])), DataCell(Text(itemList[8])), DataCell(Text(itemList[8])), ], ), DataRow( cells: [ DataCell(Text('${sno[9]}')), DataCell(Text(itemList[9])), DataCell(Text(itemList[9])), DataCell(Text(itemList[9])), ], ), DataRow( cells: [ DataCell(Text('${sno[10]}')), DataCell(Text(itemList[10])), DataCell(Text(itemList[10])), DataCell(Text(itemList[10])), ], ), DataRow( cells: [ DataCell(Text('${sno[11]}')), DataCell(Text(itemList[11])), DataCell(Text(itemList[11])), DataCell(Text(itemList[11])), ], ), DataRow( cells: [ DataCell(Text('${sno[12]}')), DataCell(Text(itemList[12])), DataCell(Text(itemList[12])), DataCell(Text(itemList[12])), ], ), DataRow( cells: [ DataCell(Text('${sno[13]}')), DataCell(Text(itemList[13])), DataCell(Text(itemList[13])), DataCell(Text(itemList[13])), ], ), DataRow( cells: [ DataCell(Text('${sno[14]}')), DataCell(Text(itemList[14])), DataCell(Text(itemList[14])), DataCell(Text(itemList[14])), ], ), ], ), ), ), ) ), Visibility(visible: _loading, child: const Loader()) ], ); } } |
We can now run the app on how to create Data Table in a flutter.
Hope this blog helps you to create DataTable in a flutter.
So, I hope it will help you understand and get a brief idea about it.
For more understanding please go through this Link :
That’s all.
Thank you very much.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.