How to solve: Best Way to Create Table In Flutter

Question

Asked by Eko M on January 12, 2022 (source).

Here i create table using using container and based on expanded using flex number to make header and the row relatable. I think this is not best way, but i don't know the other way how to create table like this, anyone can give me a suggestion?

Here is the code:

_tableSection

Widget _bookingListSection(OnlineBookingListController controller) {
    return Column(
      children: [
        _buildHeaderTable(),
        Expanded(
          child: ListView.builder(
            padding: EdgeInsets.zero,
            itemCount: controller.listBooking.length,
            itemBuilder: (context, index) {
              int lastIndex = controller.listBooking.length - 1;

              return _buildContentTable(
                index,
                lastIndex,
                context,
                controller,
              );
            },
          ),
        ),
      ],
    );
  }

_buildHeaderTable(),

Widget _buildHeaderTable() {
    return Container(
      width: double.maxFinite,
      height: AppSize.DIMEN_64.h,
      padding: EdgeInsets.fromLTRB(
        AppSize.DIMEN_22.h,
        AppSize.DIMEN_16.h,
        AppSize.DIMEN_22.h,
        AppSize.DIMEN_16.h,
      ),
      decoration: BoxDecoration(
        color: AppColors.GREY_BLACK_BACKGROUND,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(AppSize.RADIUS_8.h),
          topRight: Radius.circular(AppSize.RADIUS_8.h),
        ),
      ),
      child: Row(children: [
        _titleHeaderTable('Time', 3),
        _titleHeaderTable('Phone Number', 3),
        _titleHeaderTable('Customer Name', 3),
        _titleHeaderTable('Room', 3),
        _titleHeaderTable('Waitress', 3),
        _titleHeaderTable('Status', 2),
        _titleHeaderTable('Action', 4),
      ]),
    );
  }

Widget _titleHeaderTable(String title, int flexNum) {
    return Expanded(
      flex: flexNum,
      child: Container(
        child: Text(
          title,
          textAlign: TextAlign.left,
          maxLines: 2,
          style: textStyleW700S14.copyWith(
            color: AppColors.WHITE,
          ),
        ),
      ),
    );
  }

Then for the content i using flex inside row. Did you have any suggestion about this one?

Answer

Question answered by Ravindra S (source).

You can create Table in Flutter using Table and Data Table

  1. Using Table Class:

Refer Table Class here

Container(
  margin: EdgeInsets.all(20),
  child: Table(
    defaultColumnWidth: FixedColumnWidth(120.0),
    border: TableBorder.all(
        color: Colors.black, style: BorderStyle.solid, width: 2),
    children: [
      TableRow(children: [
        Column(
          children: [
            Text(
              'Website',
              style: TextStyle(fontSize: 20.0),
            ),
          ],
        ),
        Column(
          children: [
            Text(
              'Tutorial',
              style: TextStyle(fontSize: 20.0),
            ),
          ],
        ),
        Column(
          children: [
            Text(
              'Review',
              style: TextStyle(fontSize: 20.0),
            ),
          ],
        ),
      ]),
      TableRow(children: [
        Column(
          children: [
            Text('https://flutter.dev/'),
          ],
        ),
        Column(
          children: [
            Text('Flutter'),
          ],
        ),
        Column(
          children: [
            Text('5*'),
          ],
        ),
      ]),
      TableRow(children: [
        Column(
          children: [
            Text('https://dart.dev/'),
          ],
        ),
        Column(
          children: [
            Text('Dart'),
          ],
        ),
        Column(
          children: [
            Text('5*'),
          ],
        ),
      ]),
      TableRow(children: [
        Column(
          children: [
            Text('https://pub.dev/'),
          ],
        ),
        Column(
          children: [
            Text('Flutter Packages'),
          ],
        ),
        Column(
          children: [
            Text('5*'),
          ],
        ),
      ]),
    ],
  ),
),

Result Screen Using Table Class-> enter image description here

  1. Using DataTable Class:

Refer DataTable Class here

DataTable(
  columns: const <DataColumn>[
    DataColumn(
      label: Text(
        'Sr.No',
        style: TextStyle(
          fontWeight: FontWeight.bold,
        ),
      ),
    ),
    DataColumn(
      label: Text(
        'Website',
        style: TextStyle(
          fontWeight: FontWeight.bold,
        ),
      ),
    ),
    DataColumn(
      label: Text(
        'Tutorial',
        style: TextStyle(
          fontWeight: FontWeight.bold,
        ),
      ),
    ),
    DataColumn(
      label: Text(
        'Review',
        style: TextStyle(
          fontWeight: FontWeight.bold,
        ),
      ),
    ),
  ],
  rows: const <DataRow>[
    DataRow(
      cells: <DataCell>[
        DataCell(
          Text('1'),
        ),
        DataCell(
          Text('https://flutter.dev/'),
        ),
        DataCell(
          Text('Flutter'),
        ),
        DataCell(
          Text('5*'),
        ),
      ],
    ),
    DataRow(
      cells: <DataCell>[
        DataCell(
          Text('2'),
        ),
        DataCell(
          Text('https://dart.dev/'),
        ),
        DataCell(
          Text('Dart'),
        ),
        DataCell(
          Text('5*'),
        ),
      ],
    ),
    DataRow(
      cells: <DataCell>[
        DataCell(
          Text('3'),
        ),
        DataCell(
          Text('https://pub.dev/'),
        ),
        DataCell(
          Text('Flutter Packages'),
        ),
        DataCell(
          Text('5*'),
        ),
      ],
    ),
  ],
),

Result Screen using DataTable Class->enter image description here

You can refer this package also for Table

Video Answers on YouTube

DART FLUTTER FLUTTER-LAYOUT FLUTTER-TABLE
SHARE: