Is there a way to create a spreadsheet like editable grid on flutter?

4,522

Solution 1

You can copy paste run full code below
You can use package https://pub.dev/packages/table_sticky_headers

code snippet

StickyHeadersTable(
          columnsLength: titleColumn.length,
          rowsLength: titleRow.length,
          columnsTitleBuilder: (i) => Text(titleColumn[i]),
          rowsTitleBuilder: (i) => Text(titleRow[i]),
          contentCellBuilder: (i, j) =>
              Container(height: 50, width: 50, child: TextField()),
          legendCell: Text('Sticky Legend'),
        ),

working demo

enter image description here

full code

import 'package:flutter/material.dart';
import 'package:table_sticky_headers/table_sticky_headers.dart';

void main() {
  final columns = 10;
  final rows = 20;

  List<List<String>> _makeData() {
    final List<List<String>> output = [];
    for (int i = 0; i < columns; i++) {
      final List<String> row = [];
      for (int j = 0; j < rows; j++) {
        row.add('T$i : L$j');
      }
      output.add(row);
    }
    return output;
  }

  /// Simple generator for column title
  List<String> _makeTitleColumn() => List.generate(columns, (i) => 'Top $i');

  /// Simple generator for row title
  List<String> _makeTitleRow() => List.generate(rows, (i) => 'Left $i');

  runApp(
    TableSimple(
      titleColumn: _makeTitleColumn(),
      titleRow: _makeTitleRow(),
      data: _makeData(),
    ),
  );
}

class TableSimple extends StatelessWidget {
  TableSimple(
      {@required this.data,
      @required this.titleColumn,
      @required this.titleRow});

  final List<List<String>> data;
  final List<String> titleColumn;
  final List<String> titleRow;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Sticky Headers Two-Dimension  Table'),
          backgroundColor: Colors.amber,
        ),
        body: StickyHeadersTable(
          columnsLength: titleColumn.length,
          rowsLength: titleRow.length,
          columnsTitleBuilder: (i) => Text(titleColumn[i]),
          rowsTitleBuilder: (i) => Text(titleRow[i]),
          contentCellBuilder: (i, j) =>
              Container(height: 50, width: 50, child: TextField()),
          legendCell: Text('Sticky Legend'),
        ),
      ),
    );
  }
}

Solution 2

You can do this perfectly using Editable Package!

List rows = [
    {
      "name": 'James Joe',
      "date": '23/09/2020',
      "month": 'June',
      "status": 'completed'
    },
    {
      "name": 'Daniel Paul',
      "month": 'March',
      "status": 'new',
      "date": '12/4/2020',
    },
    {
      "month": 'May',
      "name": 'Mark Zuckerberg',
      "date": '09/4/1993',
      "status": 'expert'
    },
    {
      "name": 'Jack',
      "status": 'legend',
      "date": '01/7/1820',
      "month": 'December',
    },
  ];
  List cols = [
    {"title": 'Name', 'index': 1, 'key': 'name'},
    {"title": 'Date', 'index': 2, 'key': 'date'},
    {"title": 'Month', 'index': 3, 'key': 'month'},
    {"title": 'Status', 'index': 4, 'key': 'status'},
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Editable(
        columns: cols,
        rows: rows,
        zebraStripe: true,
        stripeColor2: Colors.grey[200],
        onRowSaved: (value) {
          print(value);
        },
        onSubmitted: (value) {
          print(value);
        },
        borderColor: Colors.blueGrey,
        showSaveIcon: true,
        saveIconColor: Colors.black,
        showCreateButton: true,
      ),
    );
  }
Share:
4,522
Hugo Pablo
Author by

Hugo Pablo

Updated on December 17, 2022

Comments

  • Hugo Pablo
    Hugo Pablo over 1 year

    How can this be approached if there is no dart package available

  • Hugo Pablo
    Hugo Pablo over 4 years
    Would this enable focus change on TAB key press? What version of table_sticky_headers are you using there btw?
  • chunhunghan
    chunhunghan over 4 years
    table_sticky_headers version, I specify any
  • chunhunghan
    chunhunghan over 4 years
    tab key work fine. I have tested with Android Emulator
  • hfunes.com
    hfunes.com about 4 years
    Tranks, It worked for me, but the library is wrong... You said "You can use package pub.dev/packages/flutter_sticky_header", but the library that you have to import is not flutter_sticky_header, but table_sticky_headers. I've used table_sticky_headers: ^1.1.2 and worked very well :)
  • chunhunghan
    chunhunghan about 4 years
    @hfunes.com, thank you for your information. I have updated.
  • hfunes.com
    hfunes.com about 4 years
    You are welcome, thank you for reading and update... And for the solution :)
  • Colin
    Colin over 3 years
    Good, informative answer, thank you for using SO we hope to see you answer more questions