flutter_data_grid 0.0.23
flutter_data_grid: ^0.0.23 copied to clipboard
High-performance, reactive data grid for Flutter with virtualization, sorting, filtering, cell editing, row selection, column pinning, and keyboard navigation.
Flutter Data Grid #
A high-performance, reactive data grid for Flutter with comprehensive features including virtualization, sorting, filtering, cell editing, row selection, column pinning, and keyboard navigation.

๐ Live Demo #
Try it in your browser: https://archie934.github.io/data_grid/ โ 100k rows, WebAssembly build.
Early Development - Beta: This package is in active development. While core features are stable and well-tested, some advanced features are still being refined. APIs may evolve based on community feedback. See Known Issues & Roadmap below for current limitations.
โจ Features #
Core Functionality #
- โ Virtualized Rendering - Only visible rows/columns rendered for smooth 60fps scrolling
- โ Column Management - Resize, pin, hide/show, reorder columns
- โ Column Sorting - Single-column sorting with ascending/descending/clear cycle
- โ Column Filtering - Customizable column filtering
- โ Row Selection - None/Single/Multiple modes with checkbox column
- โ Cell Editing - Inline editing with validation and callbacks
- โ Pagination - Client-side and server-side pagination with customizable page sizes
- โ Theming - Fully customizable appearance
- โ Interceptors - Logging, validation, custom event handling
Performance #
- Isolate Processing - Sorting and filtering run on background isolates for large datasets
- Debouncing - Configurable debounce for sort/filter operations
- Efficient Updates - Reactive state management with RxDart
- O(1) Viewport - Constant-time visibility calculations
๐ฆ Installation #
Add this to your package's pubspec.yaml file:
dependencies:
flutter_data_grid: ^0.0.23
Then run:
flutter pub get
Import the package in your Dart code:
import 'package:flutter_data_grid/data_grid.dart';
โ ๏ธ Known Issues & Roadmap #
This package is in active development. Core features work well, but some advanced features are still being refined:
What Works Well โ #
- Row selection - Fully implemented with none/multiple modes
- Cell selection - Individual cell focus, range selection (Shift+click / Shift+Arrow), right-click drag, Ctrl+C copy as CSV
- Virtualized rendering - Smooth scrolling with 100k+ rows
- Sorting & filtering - Single-column sorting with background processing for large datasets
- Column management - Resize, pin, hide/show columns
- Cell editing - Inline editing with validation (some edge cases remain)
- Pagination - Client-side and server-side pagination with loading states
- Theming - Comprehensive customization options
In Progress / Known Issues โ ๏ธ #
- Keyboard navigation - Arrow keys work but may have issues in some scenarios
- Tab navigation - Between cells needs improvement
- Cell editing - Edge cases with rapid interactions being addressed
Planned Features ๐ #
- Column selection - Select entire columns
- Accessibility - Improved screen reader support and ARIA labels
- Column reordering - Drag-and-drop reordering
- Context menus - Right-click menus for rows/cells
Production Use #
This package is suitable for:
- โ Internal tools and dashboards
- โ Admin panels and data management UIs
- โ Projects willing to work around known limitations
- โ Applications that can test thoroughly for their use case
Consider waiting for v1.0 if you need:
- โ Full accessibility compliance
- โ Rock-solid keyboard navigation
- โ Complete feature parity with enterprise data grids
Contributions welcome! If you'd like to help improve any of these areas, please see the Contributing section.
๐๏ธ Architecture #
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ DataGrid Widget โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โ โ Header โ โ Scrollbars โ โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Virtualized Body โ โ
โ โ CustomMultiChildLayout โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโผโโโโโโโโโ
โ Controller โ
โ (RxDart) โ
โโโโโโโโโโฌโโโโโโโโโ
โ
โโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Event-Driven Architecture โ
โ โข Data Events (CRUD) โ
โ โข Sort Events โ
โ โข Filter Events โ
โ โข Selection Events โ
โ โข Edit Events โ
โ โข Pagination Events โ
โ โข Keyboard Events โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ Project Structure #
lib/
โโโ controllers/
โ โโโ data_grid_controller.dart # Main state controller
โ โโโ grid_scroll_controller.dart # Scroll synchronization
โ
โโโ models/
โ โโโ data/
โ โ โโโ column.dart # Column configuration
โ โ โโโ row.dart # Row interface
โ โโโ state/
โ โ โโโ grid_state.dart # Freezed state models
โ โโโ enums/
โ โ โโโ filter_operator.dart # Filter operators
โ โ โโโ selection_mode.dart # Selection modes
โ โ โโโ sort_direction.dart # Sort directions
โ โโโ events/
โ โโโ data_events.dart # CRUD operations
โ โโโ sort_events.dart # Sorting
โ โโโ filter_events.dart # Filtering
โ โโโ selection_events.dart # Row/cell selection
โ โโโ edit_events.dart # Cell editing
โ โโโ keyboard_events.dart # Keyboard navigation
โ โโโ pagination_events.dart # Pagination
โ
โโโ delegates/
โ โโโ sort_delegate.dart # Pluggable sorting
โ โโโ filter_delegate.dart # Pluggable filtering
โ
โโโ interceptors/
โ โโโ data_grid_interceptor.dart # Base interceptor
โ โโโ logging_interceptor.dart # Event logging
โ โโโ validation_interceptor.dart # Data validation
โ
โโโ theme/
โ โโโ data_grid_theme.dart # Theme provider
โ โโโ data_grid_theme_data.dart # Theme configuration
โ
โโโ utils/
โ โโโ data_indexer.dart # Sort/filter operations
โ โโโ isolate_sort.dart # Background sorting
โ โโโ isolate_filter.dart # Background filtering
โ
โโโ widgets/
โ โโโ data_grid.dart # Main widget
โ โโโ data_grid_header.dart # Header row
โ โโโ data_grid_pagination.dart # Pagination controls
โ โโโ cells/
โ โ โโโ data_grid_cell.dart # Standard cell
โ โ โโโ data_grid_header_cell.dart # Header cell
โ โ โโโ data_grid_checkbox_cell.dart # Selection checkbox
โ โ โโโ cell_scope.dart # Per-cell InheritedWidget (CellScope)
โ โโโ custom_layout/ # CustomMultiChildLayout renderer
โ โ โโโ custom_layout_grid_body.dart # Body widget + scroll handling
โ โ โโโ grid_unpinned_quadrant.dart # Scrollable column region
โ โ โโโ grid_pinned_quadrant.dart # Fixed column region
โ โ โโโ grid_layout_delegate.dart # Layout delegate
โ โ โโโ layout_grid_cell.dart # LayoutId wrapper
โ โ โโโ external_scroll_position.dart # ScrollPosition adapter
โ โโโ filters/
โ โ โโโ filter_scope.dart # Per-column filter InheritedWidget
โ โ โโโ default_filter_widget.dart # Default text-based filter
โ โ โโโ filter_text_field.dart # Reusable filter text field
โ โโโ overlays/
โ โ โโโ loading_overlay.dart # Loading indicator + scoped overlay
โ โโโ scroll/
โ โ โโโ horizontal_scrollbar.dart
โ โ โโโ vertical_scrollbar.dart
โ โโโ viewport/
โ โโโ data_grid_header_viewport.dart # Header/filter render object
โ
โโโ data_grid.dart # Public API exports
โก Migration Guide #
v0.0.16 โ v0.0.17: Removed TwoDimensional renderer #
Breaking changes:
-
DataGridRendererTypeenum removed โ Delete any import or usage ofDataGridRendererType. -
rendererparameter removed fromDataGridโ Remove therenderer:argument from yourDataGrid(...)call. The grid now exclusively uses theCustomMultiChildLayoutrenderer. -
DataGridBody,DataGridScrollView, and all viewport classes removed โ If you were importingDataGridBody,DataGridScrollView,DataGridViewport,RenderDataGridViewport,DataGridChildDelegate, orDataGridVicinitydirectly, remove those imports.
Migration checklist:
- โ Remove
renderer: DataGridRendererType.customLayout(ortwoDimensional) from allDataGrid(...)calls - โ Remove any
importofDataGridRendererType - โ Remove direct usage of
DataGridBodyor the viewport classes if any
v0.0.11 โ v0.0.12: Cell rebuild optimization & removed viewport/scroll state #
Breaking changes:
-
ViewportStateremoved fromDataGridStateโ Theviewportfield onDataGridStateand the entireViewportStatefreezed class have been removed. Flutter'sTwoDimensionalScrollViewalready manages scroll offsets and visible range internally, so tracking them in grid state was redundant and caused unnecessary rebuilds. -
ViewportDelegateremoved โViewportDelegate,DefaultViewportDelegate, andViewportCalculatorhave been deleted. Remove theviewportDelegate:parameter from yourDataGridControllerconstructor if you were passing one. -
ScrollEvent/ViewportResizeEventremoved โ These events are no longer dispatched or handled. Remove any code that dispatches or listens for them. -
FilterRendererโfilterWidget+FilterScopeโ TheFilterRendererabstract class andDataGrid.filterRendererparameter have been replaced byDataGrid.filterWidget(a plainWidget) paired withFilterScope(anInheritedWidget). See the v0.0.9 โ v0.0.10 migration below for the pattern โ the same approach now applies globally. -
RowRenderer/RenderContextremoved โ These unused renderer abstractions have been deleted.
Cell rebuild performance:
Cells no longer subscribe to global state changes via InheritedModel. Instead, each cell subscribes to a stream scoped to its own row/column, so only the affected cells rebuild when selection or edit state changes. This dramatically reduces unnecessary rebuilds in grids with many visible cells.
Loading overlay:
The loading overlay is now wrapped in DataGridLoadingScope, which subscribes only to the loading aspect of the grid state. This prevents loading state changes from triggering rebuilds in unrelated widgets.
v0.0.8 โ v0.0.9+: cellRenderer โ cellWidget + CellScope #
The CellRenderer interface and the DataGrid.cellRenderer parameter have been removed. Custom cell rendering now uses a plain Widget declared on each DataGridColumn and a CellScope inherited widget for per-cell data access.
Before (v0.0.8):
class MyRenderer implements CellRenderer<MyRow> {
@override
Widget buildCell(RenderContext<MyRow> context) {
return Text(context.value.toString());
}
}
DataGrid<MyRow>(
controller: controller,
cellRenderer: MyRenderer(),
)
After (v0.0.9+):
class MyCell extends StatelessWidget {
const MyCell({super.key});
@override
Widget build(BuildContext context) {
final scope = CellScope.of<MyRow>(context);
return Text(scope.value.toString());
}
}
// Set per-column โ no global cellRenderer on DataGrid:
DataGridColumn<MyRow>(
id: 1,
title: 'Name',
width: 150,
valueAccessor: (row) => row.name,
cellWidget: const MyCell(),
)
Migration checklist:
- โ Delete any class that implements
CellRenderer<T> - โ Remove
cellRenderer:from yourDataGrid(...)constructor call - โ Create a
StatelessWidgetwith aconstconstructor for each custom cell - โ Inside the widget's
build, callCellScope.of<MyRow>(context)to access row data - โ Set
cellWidget: const MyCell()on eachDataGridColumnthat needs custom rendering
Why this change?
The old approach built a new renderer widget every frame, preventing Flutter from reusing element subtrees. The new const cellWidget + CellScope pattern lets Flutter preserve element identity across scrolls and rebuilds โ only the InheritedWidget data changes, so only CellScope.of call sites are rebuilt.
v0.0.9 โ v0.0.10+: filterRenderer โ filterWidget + FilterScope #
The FilterRenderer abstract class and the DataGrid.filterRenderer / DataGridColumn.filterRenderer parameters have been removed. Custom filter rendering now uses a plain Widget on each column and a FilterScope inherited widget for accessing filter context.
Before (v0.0.9):
class MyFilter implements FilterRenderer {
@override
Widget buildFilter(
BuildContext context,
DataGridColumn column,
ColumnFilter? currentFilter,
void Function(FilterOperator, dynamic) onChange,
void Function() onClear,
) {
return TextField(onChanged: (v) => onChange(FilterOperator.contains, v));
}
}
DataGridColumn<MyRow>(
id: 1,
title: 'Name',
width: 150,
filterRenderer: MyFilter(),
)
After (v0.0.10+):
class MyFilter extends StatelessWidget {
const MyFilter({super.key});
@override
Widget build(BuildContext context) {
final scope = FilterScope.of(context);
return TextField(
onChanged: (v) => scope.onChange(FilterOperator.contains, v),
);
}
}
DataGridColumn<MyRow>(
id: 1,
title: 'Name',
width: 150,
filterWidget: const MyFilter(),
)
Migration checklist:
- โ Delete any class that implements
FilterRenderer - โ Remove
filterRenderer:from yourDataGrid(...)andDataGridColumn(...)calls - โ Create a
StatelessWidgetwith aconstconstructor for each custom filter - โ Inside the widget's
build, callFilterScope.of(context)to access column, currentFilter, onChange, onClear - โ Set
filterWidget: const MyFilter()on eachDataGridColumnthat needs a custom filter
FilterScope provides:
scope.columnโ the column definitionscope.currentFilterโ the activeColumnFilter(operator + value), or nullscope.onChange(operator, value)โ dispatch a filter changescope.onClear()โ dispatch a filter clear
Why this change?
Same reason as the CellScope migration: FilterRenderer.buildFilter() was called every frame, creating a new widget subtree each rebuild. The const filterWidget + FilterScope pattern preserves element identity โ only widgets that call FilterScope.of are rebuilt when filter state changes.
๐ Quick Start #
1. Define Your Row Model #
class Person extends DataGridRow {
final String name;
final int age;
final String email;
Person({required double id, required this.name, required this.age, required this.email}) {
this.id = id;
}
}
2. Create Columns #
final columns = <DataGridColumn<Person>>[
DataGridColumn<Person>(
id: 1,
title: 'Name',
width: 200,
valueAccessor: (row) => row.name,
cellValueSetter: (row, value) => row.name = value,
sortable: true,
filterable: true,
editable: true,
pinned: false,
),
DataGridColumn<Person>(
id: 2,
title: 'Age',
width: 100,
valueAccessor: (row) => row.age,
cellValueSetter: (row, value) => row.age = value,
),
DataGridColumn<Person>(
id: 3,
title: 'Email',
width: 250,
valueAccessor: (row) => row.email,
validator: (oldValue, newValue) => newValue.contains('@'),
),
];
3. Create Controller #
final controller = DataGridController<Person>(
initialColumns: columns,
initialRows: rows,
rowHeight: 48.0,
sortDebounce: Duration(milliseconds: 300),
filterDebounce: Duration(milliseconds: 500), // Default: 500ms
sortIsolateThreshold: 10000, // Use isolate for >10k rows
filterIsolateThreshold: 10000,
);
4. Use the Widget #
@override
Widget build(BuildContext context) {
return Scaffold(
body: DataGrid<Person>(
controller: controller,
headerHeight: 48.0,
rowHeight: 48.0,
),
);
}
๐ Detailed Usage #
Column Configuration #
DataGridColumn<MyRow>(
id: 1, // Unique column identifier
title: 'Column Title', // Header text
width: 150, // Initial width
pinned: false, // Pin to left
visible: true, // Show/hide column
resizable: true, // Allow resize
sortable: true, // Enable sorting
filterable: true, // Enable filtering
editable: true, // Allow inline editing
valueAccessor: (row) => row.value, // Get cell value
cellValueSetter: (row, val) => row.value = val, // Set cell value
validator: (old, new) => new != null, // Validate edits
cellWidget: const MyCell(), // Custom cell widget (see CellScope)
cellEditorBuilder: customEditor, // Custom editor
cellFormatter: (row, col) => 'text', // Format display
)
Data Operations #
// Load data
controller.setRows(myRows);
// Insert rows
controller.insertRow(newRow);
controller.insertRow(newRow, position: 0); // Insert at index
controller.insertRows([row1, row2, row3]);
// Update data
controller.updateRow(rowId, updatedRow);
controller.updateCell(rowId, columnId, newValue);
// Delete data
controller.deleteRow(rowId);
controller.deleteRows({id1, id2, id3});
Sorting #
// Sort by column (ascending)
controller.addEvent(SortEvent(
columnId: 1,
direction: SortDirection.ascending,
));
// Sort by column (descending)
controller.addEvent(SortEvent(
columnId: 1,
direction: SortDirection.descending,
));
// Clear sort
controller.addEvent(SortEvent(
columnId: 1,
direction: null, // Clears the sort
));
// Note: Sorting a different column replaces the current sort
// Only single-column sorting is supported
Filtering #
// Apply filter
controller.addEvent(FilterEvent(
columnId: 1,
operator: FilterOperator.contains,
value: 'search term',
));
// Available operators:
// - equals, notEquals
// - contains, startsWith, endsWith (case-insensitive, trims whitespace)
// - greaterThan, lessThan
// - greaterThanOrEqual, lessThanOrEqual
// - isEmpty, isNotEmpty
// String filters are sanitized:
// - Trimmed (leading/trailing whitespace removed)
// - Case-insensitive matching
// - Multiple spaces normalized to single space
// Clear filter
controller.addEvent(ClearFilterEvent(columnId: 1)); // Clear one
controller.addEvent(ClearFilterEvent()); // Clear all
Selection #
// Configure selection mode
controller.setSelectionMode(SelectionMode.none); // Disabled
controller.setSelectionMode(SelectionMode.multiple); // Multiple rows (checkbox column)
// Or use helpers
controller.disableSelection();
controller.enableMultiSelect(true);
// Select rows
controller.addEvent(SelectRowEvent(rowId: 1));
controller.addEvent(SelectRowEvent(rowId: 2, multiSelect: true));
// Select range
controller.addEvent(SelectRowsRangeEvent(
startRowId: 1,
endRowId: 5,
));
// Select all visible
controller.addEvent(SelectAllVisibleEvent());
// Clear selection
controller.addEvent(ClearSelectionEvent());
// Listen to selection
controller.selection$.listen((selection) {
print('Selected: ${selection.selectedRowIds}');
});
Pagination #
// Enable pagination
controller.enablePagination(true);
// Navigate pages
controller.setPage(5); // Go to specific page
controller.nextPage(); // Next page
controller.previousPage(); // Previous page
controller.firstPage(); // First page
controller.lastPage(); // Last page
// Change page size
controller.setPageSize(25); // 25 rows per page
// Disable pagination
controller.enablePagination(false);
Server-Side Pagination
For server-side pagination, provide callbacks when creating the controller:
final controller = DataGridController<MyRow>(
initialColumns: columns,
initialRows: [], // Start empty for server-side
onLoadPage: (page, pageSize) async {
// Fetch data from your API
final response = await api.fetchPage(page: page, limit: pageSize);
return response.rows;
},
onGetTotalCount: () async {
// Return total row count from server
return await api.getTotalCount();
},
);
// Enable server-side pagination
controller.enablePagination(true);
controller.setServerSidePagination(true);
// For server-side, you must set total items after loading
controller.setTotalItems(totalCount);
Cell Editing #
// Start editing
controller.startEditCell(rowId, columnId);
// Update value
controller.updateCellEditValue(newValue);
// Commit changes
controller.commitCellEdit();
// Cancel editing
controller.cancelCellEdit();
// With validation
final controller = DataGridController<MyRow>(
columns: columns,
rows: rows,
canEditCell: (rowId, columnId) => rowId > 0, // Custom permission
onCellCommit: (rowId, columnId, oldValue, newValue) async {
// Return true to allow, false to reject
return await saveToDatabase(rowId, columnId, newValue);
},
);
Cell Selection #
// Plain click โ focus a single cell
controller.addEvent(FocusCellEvent(rowId: 1.0, columnId: 2));
// Ctrl+click โ toggle a cell in/out of the selection
controller.addEvent(ToggleCellInSelectionEvent(rowId: 1.0, columnId: 2));
// Shift+click โ extend the selection to this cell
controller.addEvent(ShiftSelectCellEvent(rowId: 3.0, columnId: 2));
// Programmatically set a rect of cells
controller.addEvent(SetFocusedCellsEvent(['1.0_2', '1.0_3', '2.0_2', '2.0_3']));
// Clear cell focus
controller.addEvent(ClearCellSelectionEvent());
// Copy focused cells to clipboard as CSV (Ctrl+C also triggers this)
controller.addEvent(CopyCellsEvent());
Right-click drag draws a selection rectangle over the grid body; releasing commits the covered cells as focused.
Keyboard Navigation #
Built-in keyboard shortcuts:
- Arrow Up/Down: Navigate rows (or cells when a cell is focused)
- Arrow Left/Right: Navigate cells
- Shift+Arrow: Extend cell selection
- Escape: Clear selection
- Ctrl+A / Cmd+A: Select all visible rows
- Ctrl+C / Cmd+C: Copy focused cells to clipboard as CSV
Custom Rendering #
Custom cell widgets use CellScope to access per-cell data. Declare a const widget on the column โ Flutter preserves element identity across rebuilds, so only widgets that call CellScope.of are rebuilt when data changes.
// 1. Define a const cell widget
class ValueCell extends StatelessWidget {
const ValueCell({super.key});
@override
Widget build(BuildContext context) {
final scope = CellScope.of<MyRow>(context);
return Container(
color: scope.row.value > 100 ? Colors.green : Colors.red,
padding: const EdgeInsets.symmetric(horizontal: 8),
alignment: Alignment.centerLeft,
child: Text(scope.value.toString()),
);
}
}
// 2. Assign it to the column
DataGridColumn<MyRow>(
id: 1,
title: 'Value',
width: 150,
valueAccessor: (row) => row.value,
cellWidget: const ValueCell(), // const for maximum performance
)
CellScope<T> provides:
scope.rowโ the typed row objectscope.columnโ the column configurationscope.valueโ pre-computed value fromvalueAccessorscope.rowIndexโ zero-based visible row indexscope.isSelectedโ whether the row is currently selectedscope.isPinnedโ whether the column is pinnedscope.controllerโ grid controller for dispatching events
Theming #
DataGrid<MyRow>(
controller: controller,
theme: DataGridThemeData(
colors: DataGridColors(
headerBackground: Colors.blue,
cellBackground: Colors.white,
selectedRow: Colors.blue.shade100,
gridLines: Colors.grey.shade300,
),
dimensions: DataGridDimensions(
headerHeight: 48,
rowHeight: 40,
columnMinWidth: 50,
),
typography: DataGridTypography(
headerTextStyle: TextStyle(fontWeight: FontWeight.bold),
cellTextStyle: TextStyle(fontSize: 14),
),
),
)
Interceptors #
// Logging interceptor
controller.addInterceptor(LoggingInterceptor<MyRow>());
// Validation interceptor
controller.addInterceptor(ValidationInterceptor<MyRow>(
validators: {
'age': (value) => value >= 0 && value <= 150,
},
));
// Custom interceptor
class MyInterceptor extends DataGridInterceptor<MyRow> {
@override
DataGridEvent? onBeforeEvent(DataGridEvent event, DataGridState<MyRow> state) {
// Modify or cancel events
return event;
}
@override
DataGridState<MyRow>? onBeforeStateUpdate(
DataGridState<MyRow> newState,
DataGridState<MyRow> oldState,
DataGridEvent? event,
) {
// Modify state before update
return newState;
}
@override
void onAfterStateUpdate(
DataGridState<MyRow> newState,
DataGridState<MyRow> oldState,
DataGridEvent? event,
) {
// React to state changes
}
}
Custom Events #
You can create custom events to extend the grid's functionality:
// 1. Define your custom event
class ExportToCSVEvent extends DataGridEvent {
final String filename;
ExportToCSVEvent({required this.filename});
@override
DataGridState<T>? apply<T extends DataGridRow>(EventContext<T> context) {
// Access current state
final rows = context.state.visibleRows;
final columns = context.state.columns;
// Perform your custom logic
_exportToCSV(rows, columns, filename);
// Return null if no state change needed
// Or return modified state if needed
return null;
}
void _exportToCSV(List rows, List columns, String filename) {
// Your export logic here
print('Exporting ${rows.length} rows to $filename');
}
}
// 2. Dispatch your custom event
controller.addEvent(ExportToCSVEvent(filename: 'data.csv'));
// 3. More complex example with state changes
class HighlightRowsAboveThresholdEvent extends DataGridEvent {
final int threshold;
HighlightRowsAboveThresholdEvent(this.threshold);
@override
DataGridState<T>? apply<T extends DataGridRow>(EventContext<T> context) {
// Find rows that meet criteria
final highlightedIds = <double>{};
for (final row in context.state.visibleRows) {
// Assuming row has a 'value' property
if ((row as dynamic).value > threshold) {
highlightedIds.add(row.id);
}
}
// Update selection to highlight these rows
return context.state.copyWith(
selection: context.state.selection.copyWith(
selectedRowIds: highlightedIds,
),
);
}
}
// Usage
controller.addEvent(HighlightRowsAboveThresholdEvent(100));
// 4. Async custom event example
class FetchAndAppendDataEvent extends DataGridEvent {
final String apiEndpoint;
FetchAndAppendDataEvent(this.apiEndpoint);
@override
bool shouldShowLoading(DataGridState state) => true;
@override
String? loadingMessage() => 'Fetching data from API...';
@override
Future<DataGridState<T>?> apply<T extends DataGridRow>(EventContext<T> context) async {
try {
// Fetch data from API
final newRows = await _fetchFromAPI(apiEndpoint);
// Append to existing data
final updatedRowsById = Map<double, T>.from(context.state.rowsById);
for (final row in newRows) {
updatedRowsById[row.id] = row as T;
}
final updatedDisplayOrder = [
...context.state.displayOrder,
...newRows.map((r) => r.id),
];
return context.state.copyWith(
rowsById: updatedRowsById,
displayOrder: updatedDisplayOrder,
isLoading: false,
);
} catch (e) {
print('Error fetching data: $e');
return context.state.copyWith(isLoading: false);
}
}
Future<List<DataGridRow>> _fetchFromAPI(String endpoint) async {
// Your API call here
await Future.delayed(Duration(seconds: 2));
return [];
}
}
// Usage
controller.addEvent(FetchAndAppendDataEvent('https://api.example.com/data'));
Delegates (Advanced) #
// Custom sort delegate
class MySortDelegate extends SortDelegate<MyRow> {
@override
Future<SortResult?> handleSort(
SortEvent event,
DataGridState<MyRow> state,
void Function(SortResult) onComplete,
) async {
// Custom sorting logic
}
}
final controller = DataGridController<MyRow>(
columns: columns,
rows: rows,
sortDelegate: MySortDelegate(),
filterDelegate: MyFilterDelegate(),
);
๐ฏ Performance Tips #
- Use isolate thresholds wisely: Default 10,000 rows works well for most cases
- Set appropriate debounce: 500ms for filters (default), 300ms for sort
- Implement valueAccessor efficiently: Avoid heavy computations
- Use pinned columns sparingly: Too many pinned columns affect performance
- Dispose controllers: Always call
controller.dispose()inState.dispose()
Web / Chrome Performance #
Debug mode on Chrome (without --release or --wasm) will have poor performance. This is expected โ Flutter's debug mode includes extra assertions, disables optimizations, and the Dart-to-JS compilation is unoptimized. The data grid automatically caps cacheExtent to 500px in debug builds to mitigate this, but scrolling will still feel sluggish compared to release builds.
For accurate performance testing on the web, always use one of:
# Release mode (recommended for testing)
flutter run -d chrome --release
# WebAssembly (best performance)
flutter run -d chrome --wasm
๐ Performance Benchmarks #
- 100,000 rows: Smooth 60fps scrolling
- Initial render: <100ms
- Sorting: Non-blocking with isolate (background thread)
- Selection: <16ms (instant feedback)
- Column resize: Instant visual feedback
๐งช Testing #
Comprehensive test suite included:
- Widget tests for all major features (140 passing tests)
- State management tests
- Selection mode tests
- Edit workflow tests
- Keyboard navigation tests
Run tests:
flutter test
๐ Event Reference #
Data Events #
LoadDataEvent- Load rowsInsertRowEvent/InsertRowsEvent- Add rowsDeleteRowEvent/DeleteRowsEvent- Remove rowsUpdateRowEvent/UpdateCellEvent- Modify data
Sort Events #
SortEvent- Apply/modify sortSortCompleteEvent- Sort finished (internal)
Filter Events #
FilterEvent- Apply filterClearFilterEvent- Remove filters
Selection Events #
SelectRowEvent- Select rowSelectRowsRangeEvent- Select rangeSelectAllRowsEvent/SelectAllVisibleEvent- Select allClearSelectionEvent- Clear selectionSetSelectionModeEvent- Change mode
Edit Events #
StartCellEditEvent- Begin editingUpdateCellEditValueEvent- Update valueCommitCellEditEvent- Save changesCancelCellEditEvent- Discard changes
Pagination Events #
EnablePaginationEvent- Enable/disable paginationSetServerSidePaginationEvent- Toggle server-side modeSetPageEvent- Go to specific pageSetPageSizeEvent- Change rows per pageNextPageEvent/PreviousPageEvent- Navigate pagesFirstPageEvent/LastPageEvent- Jump to start/endSetTotalItemsEvent- Set total count (server-side)
Keyboard Events #
NavigateUpEvent/NavigateDownEvent- Move selectionNavigateLeftEvent/NavigateRightEvent- Navigate columns
Column Events #
ColumnResizeEvent- Resize columnSetColumnWidthEvent- Set exact width
๐ง Troubleshooting #
Q: Blank cells while scrolling?
A: This is normal virtualization behavior. Cells render as they become visible.
Q: Sorting/filtering not working in tests?
A: Use sortDebounce: Duration.zero and filterDebounce: Duration.zero for tests.
Also use await tester.runAsync(() => Future.delayed(Duration(milliseconds: 50))) before assertions to allow async stream operations to complete.
Q: Selection not working?
A: Ensure SelectionMode is not set to none.
Q: Column resize not persisting?
A: Column widths are in-memory. Persist to storage if needed.
Q: Build errors after changes?
A: Run: dart run build_runner build --delete-conflicting-outputs
๐ Dependencies #
dependencies:
flutter:
sdk: flutter
rxdart: ^0.28.0
collection: ^1.18.0
freezed_annotation: ^2.4.4
dev_dependencies:
build_runner: ^2.4.12
freezed: ^2.5.7
๐ง Roadmap & Future Enhancements #
High Priority (Addressing Limitations) #
- โ Cell selection - Individual cell selection, ranges, drag-select, Ctrl+C copy
- โ Improved keyboard navigation - Reliable arrow key navigation and Tab support
- โ Accessibility improvements - Screen reader support, ARIA labels, focus management
- โ Column reordering - Drag-and-drop column reordering
- โ Stable cell editing - Fix edge cases and improve UX
Medium Priority #
- โ Context menus - Right-click menus for rows/cells/headers
- โ Copy - Ctrl+C copies focused cells as CSV
- โ Row grouping UI - Visual grouping with expand/collapse
- โ Pagination - Built-in pagination controls (added in v0.0.3)
- โ Export functionality - CSV/Excel export
- โ Column auto-sizing - Fit content or fit header
- โ Frozen rows - Pin rows to top/bottom
Low Priority #
- โ Header viewport optimization - Custom RenderObject for header/filter scrolling (no widget rebuilds)
- โ Advanced layout logic - More flexible positioning and z-ordering for overlays
- โ Touch gestures - Swipe actions, pinch-to-zoom
- โ Undo/redo - Action history
- โ Advanced filtering UI - Filter builder with AND/OR logic
- โ Conditional formatting - Rule-based cell styling
๐ฅ Contributing #
Contributions are welcome! This is an experimental project and could benefit from community input.
Areas Needing Help #
- Accessibility - Making the grid work with screen readers
- Keyboard Navigation - Improving reliability and coverage
- Accessibility - Making the grid work with screen readers
- Testing - Adding more test coverage
- Documentation - Improving examples and API docs
- Performance - Optimizing for even larger datasets
How to Contribute #
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests if applicable
- Run tests:
flutter test - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Development Setup #
# Clone the repo
git clone https://github.com/archie934/data_grid.git
cd data_grid
# Install dependencies
flutter pub get
# Run code generation (for Freezed models)
dart run build_runner build --delete-conflicting-outputs
# Run tests
flutter test
# Run the example app
cd example
flutter pub get
flutter run
๐ License #
See LICENSE file.
๐ Acknowledgments #
- Built with Flutter's CustomMultiChildLayout for efficient virtualization
- Uses RxDart for reactive state management
- Uses Freezed for immutable state models
- Inspired by data grid implementations in AG Grid, Handsontable, and Excel
Built with Flutter ๐