Design Converter
Education
Software Development Executive - II
Last updated on Apr 30, 2024
Last updated on Apr 29, 2024
Flutter, as we all know, is a comprehensive open-source framework that lets developers create aesthetically pleasing, high-performance applications for Android, iOS, Web, and Desktop from a single codebase.
In any Flutter app, managing and displaying data in a structured format is of utmost importance. This is where the Flutter Table Widget exhibits its significance.
The Flutter Table class, a core part of Flutter's data table widget, is a powerful tool that allows developers to make the most out of displaying tabular data. It uses the table layout algorithm for its children, making it an appropriate choice when you have multiple rows and columns to display.
However, if you’re dealing with only one row, the Row widget becomes more appropriate, and for one column, SliverList or column widget will be more suitable.
This blog post aims to guide you in understanding the ins and outs of the Flutter table, along with a Flutter Table example for practical illustration.
So, let's dive into it!
Similar to tables used in web development, Flutter tables augment data representation, making it more readable and structured. Tables make complex data understandable – even non-technical people can easily make sense of the data. The Flutter Table Widget is a vital part of this data organization. In essence, a table widget in Flutter is a widget that employs a table layout algorithm to shape its children.
With Flutter's data table, you can exemplify column-oriented data. The table widget allows for the easy addition of rows and columns, facilitating a user-friendly way to organize UI elements on the screen. It provides a few extra features that set it apart. The key benefit is the control it offers over the alignment of widgets for each cell, the widget build buildcontext context and the ability to define column width, and the way the table's child widgets are partitioned among themselves.
Let's now move ahead and unravel the core elements of a table in Flutter.
When constructing a table in Flutter, there are several key elements that you should familiarize yourself with.
Table: The main Table Flutter Widget, which instantly generates a table.
Children: These are the rows in the table, each defined by the TableRow class.
TableRow: The individual row in a Flutter Table. TableRow children property allows for specifying the items in the row.
Column Widths: Flutter Table allows controlling the columns' widths using the columnWidths property, which specifies a TableColumnWidth for each column. If columnWidths is null, or if there is a null entry for a given column in columnWidths, the table defaults to using defaultColumnWidth.
defaultColumnWidth: A TableColumnWidth that is used when the table's columnWidths map does not have an entry for a given column.
With a firm grasp of these elements, let’s proceed to observe how to display data using a Flutter data table.
Displaying data using a Flutter table revolves around the creation of rows and columns.
The actual table creation takes place with the Table class. The Table class has several properties, such as columns, border, defaultColumnWidth (with FlexColumnWidth as default), columnWidths, textDirection, and children. The rows of the table can be managed with the children's property of the Table class.
Here’s an example of how to make a simple table using a Flutter table widget:
1class MyApp extends StatelessWidget { 2 3 Widget build(BuildContext context) { 4 return MaterialApp( 5 home: Scaffold( 6 appBar: AppBar(title: Text('Flutter Table Example')), 7 body: Table( 8 children: [ 9 TableRow(children: [ 10 Text('Cell 1'), 11 Text('Cell 2'), 12 Text('Cell 3'), 13 ]), 14 TableRow(children: [ 15 Text('Cell 4'), 16 Text('Cell 5'), 17 Text('Cell 6'), 18 ]), 19 ], 20 ), 21 ), 22 ); 23 } 24} 25void main() { 26 runApp(MyApp()); 27}
This code will create the table in your Flutter app with two rows and three columns, displaying six cells in total. In the next section, we'll explore the working of a Flutter data table, drawing from the Flutter table example provided.
Following each of the subsequent sections, we will explore these concepts in more detail, including a walkthrough of a Flutter Table example, advanced techniques for displaying data, and useful tips for getting the most out of the Flutter Table widget.
The Flutter data table serves as an efficient way to exhibit tabular data. This table view is built with various elements like cells, columns, rows, and clickable actions.
A DataTable widget takes two properties: columns and rows, which are both required. The columns are defined by the DataColumn widget, which takes a label as a required property. This label is a widget that appears as the column header.
Data rows are created with the help of DataRow. Each DataRow contains multiple DataCells. A DataCell can contain any widget, but generally, it holds Text or DropdownButton or Checkbox or ElevatedButton.
Here is an example of how a Flutter data table in which we display three data columns of a user- id, name, and profession.
1import 'package:flutter/material.dart'; 2 3void main() { 4 runApp(const MaterialApp(home: MyApp())); 5} 6 7class MyApp extends StatelessWidget { 8 const MyApp({Key? key}) : super(key: key); 9 10 11 Widget build(BuildContext context) { 12 return Scaffold( 13 appBar: AppBar(title: const Text('Flutter Data Table Example')), 14 body: DataTable( 15 columns: const <DataColumn>[ 16 DataColumn( 17 label: Text( 18 'Name', 19 style: TextStyle(fontWeight: FontWeight.bold), 20 ), 21 ), 22 DataColumn( 23 label: Text( 24 'Age', 25 style: TextStyle(fontWeight: FontWeight.bold), 26 ), 27 ), 28 DataColumn( 29 label: Text( 30 'Role', 31 style: TextStyle(fontWeight: FontWeight.bold), 32 ), 33 ), 34 ], 35 rows: const <DataRow>[ 36 DataRow( 37 cells: <DataCell>[ 38 DataCell(Text('Rama')), 39 DataCell(Text('19')), 40 DataCell(Text('Student')), 41 ], 42 ), 43 DataRow( 44 cells: <DataCell>[ 45 DataCell(Text('Shyama')), 46 DataCell(Text('25')), 47 DataCell(Text('Software Engineer')), 48 ], 49 ), 50 DataRow( 51 cells: <DataCell>[ 52 DataCell(Text('Shiva')), 53 DataCell(Text('32')), 54 DataCell(Text('Product Manager')), 55 ], 56 ), 57 ], 58 ), 59 ); 60 } 61}
In the above code, DataTable has two properties, columns, and rows. The DataColumn represents the column of the data table, and DataRow is for creating rows dynamically. And for each DataRow, we are defining DataCell which is the cell data.
So, this way we can easily create our data table. Let's look at how we can deal with it in the forthcoming sections of our blog, along with the practical example and other features.
As a developer, you learn better by doing. Let's look at a hands-on example where we will create a Flutter table consisting of two rows and three columns.
This would look something like this in the code:
1// Importing Flutter Material package 2import 'package:flutter/material.dart'; 3 4void main() { 5 runApp(const MyApp()); 6} 7 8class MyApp extends StatelessWidget { 9 const MyApp({Key? key}) : super(key: key); 10 11 12 Widget build(BuildContext context) { 13 return MaterialApp( 14 title: 'Flutter Demo', 15 theme: ThemeData( 16 primarySwatch: Colors.blue, 17 ), 18 home: const MyHomePage(title: 'Flutter Table Example'), 19 ); 20 } 21} 22 23class MyHomePage extends StatelessWidget { 24 const MyHomePage({Key? key, required this.title}) : super(key: key); 25 26 final String title; 27 28 29 Widget build(BuildContext context) { 30 31 return Scaffold( 32 appBar: AppBar( 33 title: Text(title), 34 ), 35 body: Table( 36 border: TableBorder.all(), 37 children: const <TableRow>[ 38 TableRow( 39 children: <Widget>[ 40 Text('Row 1, Value 1'), 41 Text('Row 1, Value 2'), 42 Text('Row 1, Value 3'), 43 ], 44 ), 45 TableRow( 46 children: <Widget>[ 47 Text('Row 2, Value 1'), 48 Text('Row 2, Value 2'), 49 Text('Row 2, Value 3'), 50 ], 51 ), 52 TableRow( 53 children: <Widget>[ 54 Text('Row 3, Value 1'), 55 Text('Row 3, Value 2'), 56 Text('Row 3, Value 3'), 57 ], 58 ), 59 ], 60 ), 61 ); 62 } 63}
With this example, a simple table with borders, row headers of 'Name', 'Age', and 'ID', and one row of data is created. The entire table is wrapped in a container, but it could be any other widget like a Column or Row based on your layout. Padding is used in cells to provide white space around the cell data for a cleaner look.
When dealing with large datasets, a Flutter data table might not be efficient due to the available real estate, particularly on a smaller screens of mobile devices. Though Flutter data tables support horizontal scrolling, it is not always a good user experience.
In such situations, the Flutter table widget comes to the rescue. A Table widget allows you to create a table layout in Flutter. It works with rows and columns, allowing for flexibility with the layout size and structure. The Table widget uses a TableLayout algorithm for its working, whereby the Table widget takes an array of rows and each row consists of an array of widgets.
This way makes it a lot easier to manipulate the table layout while maintaining the essence of a data table, making it a valuable asset in the toolkit of a Flutter developer. In the next section, we look at how to combine the power of Flutter tables with Material Design for a well-rounded user interface.
For a Flutter table view to be more attractive, Flutter comes with a Material Design version of DataTable. This DataTable widget adheres to the Material Design specifications. It offers features like sorting data in ascending or descending order, adjusting the cell width to fit the screen size, and even embedding other Material widgets such as DropDownButton, PopupButton, and IconMenuButton within the cell to enhance its capabilities.
This Flutter data table essentially has rows and columns, where rows represent the data and columns are the data types.
1return MaterialApp( 2 home: Scaffold( 3 body: DataTable( 4 columns: const <DataColumn>[ 5 DataColumn( 6 label: Text( 7 'Name', 8 style: TextStyle(fontStyle: FontStyle.italic), 9 ), 10 ), 11 DataColumn( 12 label: Text( 13 'Age', 14 style: TextStyle(fontStyle: FontStyle.italic), 15 ), 16 ), 17 DataColumn( 18 label: Text( 19 'Role', 20 style: TextStyle(fontStyle: FontStyle.italic), 21 ), 22 ), 23 ], 24 rows: const <DataRow>[ 25 DataRow( 26 cells: <DataCell>[ 27 DataCell(Text('Aakash')), 28 DataCell(Text('25')), 29 DataCell(Text('Team Lead')), 30 ], 31 ), 32 DataRow( 33 cells: <DataCell>[ 34 DataCell(Text('Rahul')), 35 DataCell(Text('23')), 36 DataCell(Text('Developer')), 37 ], 38 ), 39 DataRow( 40 cells: <DataCell>[ 41 DataCell(Text('Anita')), 42 DataCell(Text('22')), 43 DataCell(Text('System Analyst')), 44 ], 45 ), 46 ], 47 ), 48 ), 49);
In the example above, we use the Material Design Flutter data table and create a data table comprising three columns and rows.
As discussed in the official material design data table guide , a material data table can be manipulated to change the look and functionality at the end-user level. This is extremely useful when you want to create a custom Flutter table layout that follows material design standards.
In the further sections, we're going to deep-dive into advanced concepts of Flutter table data.
In addition to the basics of creating rows and columns, Flutter Table also introduces advanced concepts for optimizing your table and working with different types of data. A few of these concepts include:
Cell Alignment: You can control the alignment of children within the cells of your table, with advanced adjustment of vertical alignment and baseline for text cells.
Column Width Sorting: The Flutter Table class also provides the option to control column widths. You can even specify a different TableColumnWidth to optimize your Table widget for any layout needs.
Table Borders: You can customize table borders according to your preferences with border property included in Flutter Table Class.
These additions to your table can improve readability, provide a better user experience, and add visual intrigue to your Flutter apps.
Here’s how to modify the cell's content alignment and add column widths:
1void main() { 2 runApp(const MaterialApp(home: BasicTable())); 3} 4 5class BasicTable extends StatelessWidget { 6 const BasicTable({Key? key}) : super(key: key); 7 8 9 Widget build(BuildContext context) { 10 return Scaffold( 11 body: Table( 12 border: TableBorder.all(), 13 columnWidths: const <int, TableColumnWidth>{ 14 0: IntrinsicColumnWidth(), 15 1: FlexColumnWidth(), 16 2: FixedColumnWidth(64), 17 }, 18 defaultVerticalAlignment: TableCellVerticalAlignment.middle, 19 children: <TableRow>[ 20 TableRow( 21 children: <Widget>[ 22 Text('Website'), 23 Text('Tutorial'), 24 Text('Review'), 25 ], 26 ), 27 TableRow( 28 children: <Widget>[ 29 Text('Jenstine'), 30 Text('Creating Flutter Project'), 31 Text('10/10'), 32 ], 33 ), 34 // More rows go here 35 ], 36 ), 37 ); 38 } 39}
In this Flutter Table example, we've added 3 columns with different widths. The first column has an intrinsic width, and its width depends on the content of the cell. The second column has a flexible width, and it occupies the remaining space. The third column has a fixed width of 64 logical pixels. Moreover, children in each cell are aligned in the middle vertically.
Tables are an important part of the UI where data presentation is a prerequisite. As we’ve gone through, the Flutter Table widget is a great option among others for handling and managing this data.
Remember to consider the following points when working with Flutter Tables:
• Make the most use of the columnWidths property for a custom layout.
• When large amounts of data are involved, always opt for a scrollable parent for your table.
• Use DataRow and DataCell for better control over the widgets in the table.
• Incorporate other Flutter widgets like Checkbox, DropdownButton, PopupMenuButton, etc. in DataCell to create an interactive datatable.
• Leverage the power of defaultVerticalAlignment and textBaseline for custom alignment of cell data.
• Use the TableBorder class to create custom borders around your table.
Here is a simple yet impactful example to illustrate all these best practices together:
1void main() => runApp(MyApp()); 2 3class MyApp extends StatelessWidget { 4 // Root widget for the application. 5 Widget build(BuildContext context) { 6 return MaterialApp( 7 title: 'Flutter DataTable', 8 theme: ThemeData( 9 primarySwatch: Colors.blue, 10 ), 11 home: DataTableDemo(), 12 ); 13 } 14} 15 16class DataTableDemo extends StatefulWidget { 17 DataTableDemo() : super(); 18 19 final String title = "Data Table Flutter"; 20 21 DataTableDemoState createState() => DataTableDemoState(); 22} 23 24class DataTableDemoState extends State<DataTableDemo> { 25 List<User> users; 26 User selectedUser; 27 bool sort; 28 29 void initState() { 30 sort = false; 31 selectedUser = User('','',''); 32 users = User.getUsers(); 33 } 34 35 Widget build(BuildContext context) { 36 return Scaffold( 37 // Add rest widget tree here 38 body: SingleChildScrollView( 39 scrollDirection: Axis.horizontal, 40 child: DataTable( 41 sortColumnIndex: 0, 42 sortAscending: sort, 43 // Define rest datatable structure here 44 ), 45 ), 46 ); 47 } 48}
This is an advanced example where you can see how to create a Flutter data table with scrolling, sorting, and selecting capabilities.
Remember, the capabilities of Flutter Table are not just restricted to these features. With its compatibility with Material Design and other widgets, a Flutter data table application is endless.
So, start designing your perfect Flutter Table today!
And with this, we conclude our comprehensive guide and walkthrough on understanding the Flutter Table class, as well as how it can be employed in your Flutter projects. Keep exploring to make the most of your Flutter journey!
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.