Data table
Use a data table when readers need to work with the records they are comparing. Sorting a numeric column should order its values by size.
Sort the stock list
#Sort by Units to put the smallest quantity first, then reverse the order. Select a row or move to the next page to inspect the remaining record.
AppliancePagination uses one-based page numbers. The example connects it to the table controller’s zero-based page index.
components/data-table/appliance/sort_stock.dart
import 'package:flutter/widgets.dart';
import 'package:appliance_ui/appliance_ui.dart';
class SortStock extends StatefulWidget {
const SortStock({super.key});
@override
State<SortStock> createState() => _SortStockState();
}
class _SortStockState extends State<SortStock> {
final stock = DesignTableController<(String, int)>(
rows: const [('Ink', 12), ('Paper', 4), ('Pens', 8)],
rowKey: (item) => item.$1,
pageSize: 2,
columns: [
DesignTableColumn(id: 'item', label: 'Item', sortKey: (item) => item.$1),
DesignTableColumn(
id: 'units',
label: 'Units',
numeric: true,
sortKey: (item) => item.$2,
),
],
);
@override
void dispose() {
stock.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 620),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ApplianceDataTable<(String, int)>(
controller: stock,
cellText: (item, column) =>
column.id == 'item' ? item.$1 : '${item.$2}',
),
const SizedBox(height: 12),
ListenableBuilder(
listenable: stock,
builder: (context, _) => AppliancePagination(
page: stock.page + 1,
pageCount: stock.pageCount,
onChanged: (page) => stock.goToPage(page - 1),
),
),
],
),
),
);
}
Parameters
ApplianceDataTable API ↗controller- Holds the rows, columns, sorting, selection and current page.
cellText- A cell's words.
selectable- Whether rows are selected (a box at each row's start).
onActivate- Opens a row (Enter, or a press when it does not select).
rowLabel- A row's name for its box, for a screen reader.
style- Its style, over ApplianceThemeData.tableStyle.
Shared behaviour
- Keyboard and focus. Tab through the sortable headers and use Enter or Space. Try the row and paging controls in the order the look supplies.
- Screen readers. Read the header’s sort state, then follow the first item and its quantity. Compare the announced row state before and after selection.
- Direction and type. RTL changes where each column starts. Try sorting at phone width and 200% to check how headers and values fit.