Creating Editable Tables With Flutter

In this article, we will be building an editable table with flutter.

Godwin Asuquo
The Startup
4 min readOct 4, 2020

--

Editable tables are very useful especially in building for Desktops and Larger screen devices. Flutter already has a DataTable widget for building tables, this widget does not allow for editing though it comes with an icon for editing, developers are expected to handle the edit method themselves, however, that is a daunting and undocumented process with no ready to use callbacks to implement.

Hence there is a need for such a package to allow a quick and ready spreadsheet or editable table. I’ve created a package to do just that which we will be using for this project.

Prerequisite

In other to fully grasp and follow through with this article, the following requirements are necessary:

  • Install and setup flutter
  • Create a flutter project

For this article, I already have a flutter desktop setup for windows, you don’t necessarily need to set up for desktop, thanks to flutter, it’s cross-platform compatible.

Step 1: Install Dependencies

We begin by installing the needed dependencies.

Editable
1. Depend on it,
Add this to your package’s `pubspec.yaml` file:

2. Install it
You can install packages from the command line:
with Flutter:

Step 2: Build the table

In your project directory, open the `main.dart` file,
at the top of the file, import the editable package

You can delete everything else in this file and replace it with

All we actually did there is to call a class `TablePage()`, lets create that class just below the above code, like so:

Now, we will replace the Container with `Editable()` widget class from our package like so:

Notice that we referenced two variables `headers` and `rows`, there are arrays of objects we will provide to the columns and rows properties to build our table.

Just above the build method, we define our arrays like so:

now run the app
You have an editable table. There are methods you could use to get changes `onSubmitted` and `onRowChanged`.

Step 3: Get Edited Data

To get edited data from a data cell, add the `onSubmitted` callback. It is an anonymous function of type, `ValueChanged`. This can be added like so:

Edit a cell value and hit enter(for desktop users), the value is printed on the console

Step 4: Save Edited Rows

To save edited row, set the `showSaveIcon` property to `true`.
this will show a save Icon at the right end of each row, you can customize this icon data as you wish.

Add the `onRowSaved` callback method like so:

This method only prints the edited values of the row as an object containing, column `key`, `row id`, and `value`. if not data was edited, it prints `no edits`.

You can store this data as you wish, perhaps, update your database.

Feel free to ask questions, raise issues, and contribute to the package on git.

Don't forget to like the Editable package on pub.dev

https://www.youtube.com/watch?v=OJuAKpWVWeM

Thank You!

--

--