91 lines
2.4 KiB
Dart
91 lines
2.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import 'app_card.dart';
|
|
|
|
class AppDataColumn<T> {
|
|
const AppDataColumn({
|
|
required this.label,
|
|
required this.cellBuilder,
|
|
this.sortKey,
|
|
this.flex = 1,
|
|
this.alignment = Alignment.centerLeft,
|
|
});
|
|
|
|
final String label;
|
|
final Widget Function(BuildContext context, T row) cellBuilder;
|
|
final String? sortKey;
|
|
final int flex;
|
|
final Alignment alignment;
|
|
}
|
|
|
|
class AppDataTable<T> extends StatelessWidget {
|
|
const AppDataTable({
|
|
super.key,
|
|
required this.columns,
|
|
required this.rows,
|
|
this.sortColumn,
|
|
this.sortAscending = true,
|
|
this.onSort,
|
|
this.emptyMessage = 'No records found',
|
|
});
|
|
|
|
final List<AppDataColumn<T>> columns;
|
|
final List<T> rows;
|
|
final String? sortColumn;
|
|
final bool sortAscending;
|
|
final void Function(String column, bool ascending)? onSort;
|
|
final String emptyMessage;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (rows.isEmpty) {
|
|
return Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(32),
|
|
child: Text(
|
|
emptyMessage,
|
|
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
return AppCard(
|
|
clipBehavior: Clip.antiAlias,
|
|
child: SingleChildScrollView(
|
|
scrollDirection: Axis.horizontal,
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(minWidth: 720),
|
|
child: DataTable(
|
|
sortColumnIndex: sortColumn == null
|
|
? null
|
|
: columns.indexWhere((c) => c.sortKey == sortColumn),
|
|
sortAscending: sortAscending,
|
|
columns: columns
|
|
.map(
|
|
(col) => DataColumn(
|
|
label: Text(col.label),
|
|
onSort: col.sortKey == null
|
|
? null
|
|
: (_, ascending) => onSort?.call(col.sortKey!, ascending),
|
|
),
|
|
)
|
|
.toList(),
|
|
rows: rows
|
|
.map(
|
|
(row) => DataRow(
|
|
cells: columns
|
|
.map((col) => DataCell(col.cellBuilder(context, row)))
|
|
.toList(),
|
|
),
|
|
)
|
|
.toList(),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|