How to Create Dynamic Table Widget in Flutter?

In this blog post, we’ll explore how to create a dynamic Table Widget in Flutter using a practical example. Tables are a crucial part of many applications, aiding in the organization and presentation of data.

In this guide, we’ll explore the capabilities of the Table widget in Flutter, including its usage, properties, and examples of creating dynamic tables to suit different application requirements.

Whether you’re building a data-driven dashboard, a product catalog, or a financial report, understanding how to leverage the Table widget will empower you to create rich and interactive user interfaces in your Flutter applications.

Let’s dive in and discover the versatility of the Table widget in Flutter development.

Dynamic Table Widget in Flutter

Introduction to the Table Widget in Flutter

Tables are fundamental components in user interface design, providing a structured way to organize and present data in rows and columns.

In Flutter, a versatile UI toolkit for building natively compiled applications, the Table widget serves as a powerful tool for creating tables with dynamic content and layout flexibility.

The Table widget allows developers to create tables that adapt to various screen sizes and orientations, making it suitable for a wide range of applications across mobile, web, and desktop platforms.

With the ability to customize cell content, styling, and alignment, Flutter’s Table widget offers developers a robust solution for displaying tabular data in a visually appealing and user-friendly manner.

Implementing Dynamic Table Widget in Flutter

Now, let’s implement dynamic tables in Flutter using the provided code example.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Dynamic Table',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<List<String>> tableData = [
    ["Education", "Institution name", "University"],
    ["B.Tech", "KEC", "PU"],
    ["12th", "Shankhar dev College", "HSEB"],
    ["High School", "Public High School", "SEE"],
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Table example"),
        backgroundColor: Colors.green,
      ),
      body: Column(
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Text(
              "Table",
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Table(
              border: TableBorder.all(color: Colors.black),
              defaultVerticalAlignment: TableCellVerticalAlignment.middle,
              children: tableData.map((row) {
                return TableRow(
                  children: row.map((cell) {
                    return TableCell(
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          cell,
                        ),
                      ),
                    );
                  }).toList(),
                );
              }).toList(),
            ),
          ),
        ],
      ),
    );
  }
}

Explanation of Code

  • The provided Flutter code demonstrates how to create a dynamic table using the Table widget.
  • We begin by defining a Flutter app in the MyApp class. The MaterialApp widget provides basic material design and visual layout structure.
  • In the MyHomePage class, we define a list of lists (tableData), where each inner list represents a row in the table. This data structure makes the table dynamic and adaptable to changes.
  • The build method of _MyHomePageState returns a Scaffold widget, which provides the basic structure of the app’s visual layout. Inside the Scaffold, we have an AppBar and a Column containing the table.
  • Within the Column, we display a title (“Table”) and a Table widget. The Table widget contains a list of TableRow widgets, each representing a row in the table.
  • For each row, we map over the list of cells (row) and create a TableCell for each cell value. The TableCell contains a Text widget displaying the cell value.
  • The border property of the Table widget is set to TableBorder.all(color: Colors.black) to add borders around the table cells.
  • Finally, the defaultVerticalAlignment property is set to TableCellVerticalAlignment.middle to align the content vertically within each cell.

Output:

Output of the Dynamic Table Widget in Flutter

Also Read

Conclusion

In this blog post, we’ve explored how to create dynamic tables in Flutter using a practical example. By following the provided code and explanation, you can easily implement dynamic tables in your Flutter applications to display structured data effectively.

Tables are versatile widgets that can be customized further to suit your specific design and data presentation needs. You can follow the official documentation to set up Flutter: Flutter Installation Guide.

Share:
Ambika Dulal

Ambika Dulal is a Flutter Developer from Nepal who is passionate about building beautiful and user-friendly apps. She is always looking for new challenges and is eager to learn new things. She is also a strong believer in giving back to the community and is always willing to help others.

Leave a Comment

AO Logo

App Override is a leading mobile app development company based in Kathmandu, Nepal. Specializing in both Android and iOS app development, the company has established a strong reputation for delivering high-quality and innovative mobile solutions to clients across a range of industries.

Services

UI/UX Design

Custom App Development

Mobile Strategy Consulting

App Testing and Quality Assurance

App Maintenance and Support

App Marketing and Promotion

Contact

App Override

New Plaza, Kathmandu