{"id":2132,"date":"2022-04-09T18:29:46","date_gmt":"2022-04-09T18:29:46","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=2132"},"modified":"2022-04-09T18:29:46","modified_gmt":"2022-04-09T18:29:46","slug":"flutter-table","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/04\/09\/flutter-table\/","title":{"rendered":"Flutter Table"},"content":{"rendered":"\n<p>A table allows the user to arrange the data in rows and columns. It is used to store and display our data in a structured format, which helps us to compare the pairs of related values easily.<\/p>\n\n\n\n<p>Flutter also allows the user to create a table layout in the mobile application. We can create a table in Flutter using the&nbsp;<strong>Table widget<\/strong>&nbsp;that uses the table layout algorithm for its children. This widget has several properties to enhance or modify the table layout. These properties are: border, children, columnWidths, textDirection, textBaseline, etc.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">When we use the Table widget?<\/h3>\n\n\n\n<p>A table widget can be used when we want to store multiple rows with the same column width, and each column(table) contains equal data.\u00a0Flutter\u00a0provides another approach for the same using the\u00a0<strong>GridView widget<\/strong>.<\/p>\n\n\n\n<p>To create a table, we must use the following things:<\/p>\n\n\n\n<ol><li>First, we need to add a&nbsp;<strong>Table widget<\/strong>&nbsp;in the body.<\/li><li>Next, we have to add&nbsp;<strong>TableRow(s)<\/strong>&nbsp;in&nbsp;<strong>children<\/strong>&nbsp;of the table widget. Since the table widget has multiple rows, so we use children, not child.<\/li><li>Finally, we need to add&nbsp;<strong>TableCell(s)<\/strong>&nbsp;in children of TableRow widget. Now, we can write any widget in this place like we are going to use a&nbsp;<strong>Text widget<\/strong>.<\/li><\/ol>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TableRow(children: &#91;  \n  TableCell(child: Text('javatpoint')),  \n  TableCell(  \n    child: Text('Flutter'),  \n  ),  \n  TableCell(child: Text('Android')),  \n  TableCell(child: Text('MySQL')),  \n]),  <\/code><\/pre>\n\n\n\n<p>While using this widget, we must know these rules:<\/p>\n\n\n\n<ol><li>This widget automatically decided the column width, which is equally divided between TableCells. If it is not equal, we will get an error that says every TableRow in a table must have the same number of children so that every cell is filled. Otherwise, the table will contain holes.<\/li><li>Each row has the same height, which will be equal to the highest height of a TableCell.<\/li><li>The children of a table can have only TableRow widgets.<\/li><\/ol>\n\n\n\n<p>Let us understand it with the help of an example given below where we try to cover each thing related with this widget:<a href=\"https:\/\/www.javatpoint.com\/flutter-table#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-table#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-table#\"><\/a><span style=\"font-family: var(--list--font-family); background-color: var(--global--color-background); color: var(--global--color-primary); font-size: var(--global--font-size-base);\">\u00a0<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'package:flutter\/material.dart';  \n  \nvoid main() {runApp(MyApp());}  \n  \nclass MyApp extends StatefulWidget {  \n  @override  \n  _TableExample createState() => _TableExample();  \n}  \n  \nclass _TableExample extends State&lt;MyApp> {  \n  @override  \n  Widget build(BuildContext context) {  \n    return MaterialApp(  \n      home: Scaffold(  \n          appBar: AppBar(  \n            title: Text('Flutter Table Example'),  \n          ),  \n          body: Center(  \n              child: Column(children: &lt;Widget>&#91;  \n                Container(  \n                  margin: EdgeInsets.all(20),  \n                  child: Table(  \n                    defaultColumnWidth: FixedColumnWidth(120.0),  \n                    border: TableBorder.all(  \n                        color: Colors.black,  \n                        style: BorderStyle.solid,  \n                        width: 2),  \n                    children: &#91;  \n                      TableRow( children: &#91;  \n                        Column(children:&#91;Text('Website', style: TextStyle(fontSize: 20.0))]),  \n                        Column(children:&#91;Text('Tutorial', style: TextStyle(fontSize: 20.0))]),  \n                        Column(children:&#91;Text('Review', style: TextStyle(fontSize: 20.0))]),  \n                      ]),  \n                      TableRow( children: &#91;  \n                        Column(children:&#91;Text('Javatpoint')]),  \n                        Column(children:&#91;Text('Flutter')]),  \n                        Column(children:&#91;Text('5*')]),  \n                      ]),  \n                      TableRow( children: &#91;  \n                        Column(children:&#91;Text('Javatpoint')]),  \n                        Column(children:&#91;Text('MySQL')]),  \n                        Column(children:&#91;Text('5*')]),  \n                      ]),  \n                      TableRow( children: &#91;  \n                        Column(children:&#91;Text('Javatpoint')]),  \n                        Column(children:&#91;Text('ReactJS')]),  \n                        Column(children:&#91;Text('5*')]),  \n                      ]),  \n                    ],  \n                  ),  \n                ),  \n              ])  \n          )),  \n    );  \n  }  \n} <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>When we run the application in emulator or device, we will see the screenshot below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/flutter\/images\/flutter-table.png\" alt=\"Flutter Table\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Flutter DataTable<\/h2>\n\n\n\n<p>Flutter also allows us another widget to create a table in our application named&nbsp;<strong>DataTable<\/strong>&nbsp;widget. It is a material design data table where we can display data with&nbsp;<strong>column labels and rows<\/strong>. This widget automatically adjusted the table&#8217;s column according to the cell data. It is expensive to display data using this widget because here, all the data must be measured twice. First, it measured the dimensions for each column, and second, it actually lay out the table. So, we have to ensure that this widget can only be used when we have fewer rows.<\/p>\n\n\n\n<p>DataTable widget stored information using&nbsp;<strong>columns and rows property<\/strong>. The columns property contains data using an array of&nbsp;<strong>DataColumn<\/strong>, and the rows property contains information using an array of&nbsp;<strong>DataRow<\/strong>. The DataRow has sub-property&nbsp;<strong>cells<\/strong>&nbsp;that take an array of&nbsp;<strong>DataCell<\/strong>. The DataColumn has a sub-property&nbsp;<strong>label<\/strong>&nbsp;that takes widgets as&nbsp;<strong>value<\/strong>. We can also provide Text, Image, Icon, or any other widget in the DataTable.<\/p>\n\n\n\n<p>The following is the syntax of DataTable:<a href=\"https:\/\/www.javatpoint.com\/flutter-table#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-table#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-table#\"><\/a><span style=\"font-family: var(--list--font-family); background-color: var(--global--color-background); color: var(--global--color-primary); font-size: var(--global--font-size-base);\">\u00a0\u00a0<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DataTable(  \n  columns: &#91;  \n    DataColumn(label: ),  \n    DataColumn(label: )),  \n  ],  \n  rows: &#91;  \n    DataRow(cells: &#91;  \n      DataCell( ),  \n      DataCell( ),  \n      ...  \n      DataCell( ),  \n    ]),  \n    DataRow(cells: &#91;  \n      DataCell( ),  \n      DataCell( ),  \n      ...  \n      DataCell( ),  \n    ]),  \n  ],  \n),<\/code><\/pre>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<p>Let us understand how to use DataTable in the Flutter app. Here, we will define a simple data table that has\u00a0<strong>three column labels and four rows<\/strong>:<a href=\"https:\/\/www.javatpoint.com\/flutter-table#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-table#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-table#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'package:flutter\/material.dart';  \n  \nvoid main() {runApp(MyApp());}  \n  \nclass MyApp extends StatefulWidget {  \n  @override  \n  _DataTableExample createState() => _DataTableExample();  \n}  \n  \nclass _DataTableExample extends State&lt;MyApp> {  \n  @override  \n  Widget build(BuildContext context) {  \n    return MaterialApp(  \n      home: Scaffold(  \n          appBar: AppBar(  \n            title: Text('Flutter DataTable Example'),  \n          ),  \n          body: ListView(children: &lt;Widget>&#91;  \n            Center(  \n                child: Text(  \n                  'People-Chart',  \n                  style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),  \n                )),  \n            DataTable(  \n              columns: &#91;  \n                DataColumn(label: Text(  \n                    'ID',  \n                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)  \n                )),  \n                DataColumn(label: Text(  \n                    'Name',  \n                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)  \n                )),  \n                DataColumn(label: Text(  \n                    'Profession',  \n                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)  \n                )),  \n              ],  \n              rows: &#91;  \n                DataRow(cells: &#91;  \n                  DataCell(Text('1')),  \n                  DataCell(Text('Stephen')),  \n                  DataCell(Text('Actor')),  \n                ]),  \n                DataRow(cells: &#91;  \n                  DataCell(Text('5')),  \n                  DataCell(Text('John')),  \n                  DataCell(Text('Student')),  \n                ]),  \n                DataRow(cells: &#91;  \n                  DataCell(Text('10')),  \n                  DataCell(Text('Harry')),  \n                  DataCell(Text('Leader')),  \n                ]),  \n                DataRow(cells: &#91;  \n                  DataCell(Text('15')),  \n                  DataCell(Text('Peter')),  \n                  DataCell(Text('Scientist')),  \n                ]),  \n              ],  \n            ),  \n          ])  \n      ),  \n    );  \n  }  \n}<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>When we run the application in emulator or device, we will see the screenshot below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/flutter\/images\/flutter-table2.png\" alt=\"Flutter Table\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>A table allows the user to arrange the data in rows and columns. It is used to store and display our data in a structured format, which helps us to compare the pairs of related values easily. Flutter also allows the user to create a table layout in the mobile application. We can create a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[585],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/2132"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=2132"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/2132\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=2132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=2132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=2132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}