Skip to content

DataTable - make scrollable, set background colour and fix/freeze header row and first column #30770

Closed
flutter/packages
#4536

Description

@lukaszbojarski

I've started developing in Flutter with webview, charts, tables, but I met some problems with tables.

  1. I use DataTable to represent data in a table.
    There is a first problem. By default, it isn't scrollable if data is out of a screen. So I embedded in some Widgets (i.e. SingleChildScrollView, ListView), but it was scrollable only in one direction (vertical or horizontal). I searched something about it and I found a plugin for both directions (bidirectional_scroll_view 0.0.6). After compilation I received an error

Because app depends on bidirectional_scroll_view >=0.0.2 which
requires SDK version <2.1.0, version solving failed.

I'm using Dart SDK version: >=2.1.0 <3.0.0 in configuration.

Next, I found PaginatedDataTable which allows scrollable in both directions, but it is paginated table.
So anybody know is it possible to somehow scroll DataTable in both directions (vertical and horizontal)?

  1. The second question (more important): is it possible to set background colour in header (first) row and first column in a table? Or even in any cell in a table?

For DataColumn/DataCell I tried embed Text in Container and then set Container colour, but the background colour was only set for text, the rest of a cell was with default colour.

i.e.

DataColumn(label: Container(child: Text('2011'), color: Colors.grey,)),

Screen Shot 2019-04-04 at 13 57 38

  1. And is it possible to fixed/freeze header (first) row and first column. I mean the table is scrolled and first row/column is always visible on a screen. I couldn't find any solution ..

Activity

  1. added
    p: material_uimaterial_ui package in flutter/packages
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Apr 9, 2019
  2. Richard-Choooou commented on Aug 13, 2019

    @Richard-Choooou

    Can not agree anymore, the third problem is most important for me.
    you can use Table, TableRow, TableCell widget custom you table for the third problem, but their widgets look like can not define events.

  3. harishshet222 commented on Sep 20, 2019

    @harishshet222

    is there any one who found solution for the 3rd problem, even I am facing the same. I want to freeze the first row and first column of row, so once scroll down those should be visible.
    image , or any other way to do so,

    here what I am doing is, but here once get scrolled the first row also goes up

     Widget getTable(BuildContext context, var data) {
        Widget _widget;
        _widget = SingleChildScrollView(
          child: SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: DataTable(
              rows: createDataRows(context, data),
              columns: createDataColumns(context, data),
            ),
          ),
        );
        return _widget;
      }
    
      List<DataRow> createDataRows(BuildContext context, var jsonData) {
        List<DataRow> _dataRows = [];
        List<DataCell> _cells = [];
        String _dataCellTitle;
    
        Map<String, Object> headers = jsonData['headers'];
        Map<int, String> headerMapping;
        headerMapping = headers.keys
            .toList()
            .asMap()
            .map((index, value) => MapEntry(index + 1, value));
    
        if (jsonData["data"] == null) {
          _dataRows = [];
        } else {
          for (var j = 0; j < jsonData["data"].length; j++) {
            _cells.add(DataCell(Text('${j + 1}')));
            for (int i = 1; i <= headerMapping.length; i++) {
              _dataCellTitle = "${jsonData["data"][j][headerMapping[i]]}" ?? '-';
              _cells.add(DataCell(Text('$_dataCellTitle')));
            }
            _dataRows.add(DataRow(cells: _cells));
            _cells = [];
          }
        }
        return _dataRows;
      }
    
      List<DataColumn> createDataColumns(BuildContext context, var jsonData) {
        String columnTitle;
        List<DataColumn> _dataColumns = [];
        Map<String, Object> headers = jsonData['headers'];
        Map<int, String> headerMapping;
        headerMapping = headers.keys
            .toList()
            .asMap()
            .map((index, value) => MapEntry(index + 1, value));
        _dataColumns.add(DataColumn(label: Text('S. No.')));
        for (int i = 1; i <= headerMapping.length; i++) {
          columnTitle = headers[headerMapping[i]];
          _dataColumns.add(
            DataColumn(label: Text('$columnTitle')),
          );
        }
        return _dataColumns;
      }
    
  4. didavid61202 commented on Sep 23, 2019

    @didavid61202

    same here, the third problem is most important for me too.
    really hope someone know if there's any way to implement it.

  5. Goostavo commented on Oct 24, 2019

    @Goostavo

    There is an specific thread only about the third item (fix/freeze columns). #21165

    Also. We are currently making an workaround on stackOverflow: StackOverflow Issue on fixing header/row

  6. nikesh03 commented on Feb 2, 2020

    @nikesh03

    Can not agree anymore, the third problem is most important for me.
    you can use Table, TableRow, TableCell widget custom you table for the third problem, but their widgets look like can not define events.

    this might help https://pub.dev/packages/table_sticky_headers

  7. pulstar commented on Feb 9, 2020

    @pulstar

    You can try something like this, but the table_sticky_headers above is the best option, I think.

    ListView(
            shrinkWrap: true,
            children: <Widget>[
              SingleChildScrollView(
                scrollDirection: Axis.horizontal,
                child: DataTable(
                  columns: widget.columns,
                  rows: widget.rows,
                  columnSpacing: widget.columnSpacing,
                  horizontalMargin: widget.horizontalMargin,
                  headingRowHeight: 50.0,
                  dataRowHeight: 40.0,
                ),
              ),
            ],
          )
    
  8. Viacheslav-Romanov commented on Mar 17, 2020

    @Viacheslav-Romanov

    Good points!

  9. normidar commented on May 5, 2020

    @normidar

    This problem has been bothering me for a long time, why is it still unsolved after so long?

  10. nikesh03 commented on May 5, 2020

    @nikesh03
  11. added
    a: qualityA truly polished experience
    customer: crowdAffects or could affect many people, though not necessarily a specific customer.
    P3Issues that are less important to the Flutter project
    c: new featureNothing broken; request for a new capability
    on Jun 5, 2020
  12. 17 remaining items

  13. removed their assignment
    on Dec 2, 2022
  14. moved this from Planning to In Progress in 2D Scrollingon Mar 10, 2023
  15. joo commented on Jul 20, 2023

    @joo
  16. moved this from In Progress to Done in 2D Scrollingon Aug 17, 2023
  17. github-actions commented on Aug 31, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  18. locked as resolved and limited conversation to collaborators on Aug 31, 2023
  19. self-assigned this
    on Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: qualityA truly polished experiencec: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Fluttercustomer: crowdAffects or could affect many people, though not necessarily a specific customer.frameworkflutter/packages/flutter repository. See also f: labels.p: material_uimaterial_ui package in flutter/packagesteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions