data tables
data tables for flutter.
- full screen pagnitated data tables for tablets/desktops
- mobile listview with action buttons for sorting and selecting all
- supports dark mode
getting started with data tables
- you can optionally build the listview for mobile with a builder, by default it creates a expansiontile with the remaining columns as children
- the tablet breakpoint can also be set.
bool showmobilelistview;
– when set to false it will always show a data tableint sortcolumnindex;
– current sorted columnbool sortascending;
– sort ordervaluechanged<bool> onselectall;
– called for selecting and deselecting allvaluechanged<int> onrowsperpagechanged;
– called when rows change on data table or last row reached on mobile.int rowsperpage;
– default rows per pagewidget header;
– widget header for desktop and tablet data tablelist<datacolumn> columns;
– list of columns (must match length of datacells in datasource)indexedwidgetbuilder mobileitembuilder;
– optional item builder for the list view for mobilesize tabletbreakpoint;
– tablet breakpoint for the screen width and heightlist<widget> actions, selectedactions;
– actions that show when items are selected or notrefreshcallback onrefresh;
– if not null the list view will be wrapped in a refreshindicator
screenshots
example for data tables
import 'package:data_tables/data_tables.dart';
import 'package:flutter/material.dart';
import 'data/dessert.dart';
void main() => runapp(myapp());
class myapp extends statefulwidget {
@override
_myappstate createstate() => _myappstate();
}
class _myappstate extends state<myapp> {
int _rowsperpage = paginateddatatable.defaultrowsperpage;
int _sortcolumnindex;
bool _sortascending = true;
@override
void initstate() {
_items = _desserts;
super.initstate();
}
void _sort<t>(
comparable<t> getfield(dessert d), int columnindex, bool ascending) {
_items.sort((dessert a, dessert b) {
if (!ascending) {
final dessert c = a;
a = b;
b = c;
}
final comparable<t> avalue = getfield(a);
final comparable<t> bvalue = getfield(b);
return comparable.compare(avalue, bvalue);
});
setstate(() {
_sortcolumnindex = columnindex;
_sortascending = ascending;
});
}
list<dessert> _items = [];
int _rowsoffset = 0;
@override
widget build(buildcontext context) {
return materialapp(
// theme: themedata.dark(),
home: scaffold(
appbar: appbar(
title: const text('native data table example'),
),
body: nativedatatable.builder(
rowsperpage: _rowsperpage,
itemcount: _items?.length ?? 0,
firstrowindex: _rowsoffset,
handlenext: () async {
setstate(() {
_rowsoffset += _rowsperpage;
});
await new future.delayed(new duration(seconds: 3));
setstate(() {
_items += [
dessert('new item 4', 159, 6.0, 24, 4.0, 87, 14, 1),
dessert('new item 5', 159, 6.0, 24, 4.0, 87, 14, 1),
dessert('new item 6', 159, 6.0, 24, 4.0, 87, 14, 1),
];
});
},
handleprevious: () {
setstate(() {
_rowsoffset -= _rowsperpage;
});
},
itembuilder: (int index) {
final dessert dessert = _items[index];
return datarow.byindex(
index: index,
selected: dessert.selected,
onselectchanged: (bool value) {
if (dessert.selected != value) {
setstate(() {
dessert.selected = value;
});
}
},
cells: <datacell>[
datacell(text('${dessert.name}')),
datacell(text('${dessert.calories}')),
datacell(text('${dessert.fat.tostringasfixed(1)}')),
datacell(text('${dessert.carbs}')),
datacell(text('${dessert.protein.tostringasfixed(1)}')),
datacell(text('${dessert.sodium}')),
datacell(text('${dessert.calcium}%')),
datacell(text('${dessert.iron}%')),
]);
},
header: const text('data management'),
sortcolumnindex: _sortcolumnindex,
sortascending: _sortascending,
onrefresh: () async {
await new future.delayed(new duration(seconds: 3));
setstate(() {
_items = _desserts;
});
return null;
},
onrowsperpagechanged: (int value) {
setstate(() {
_rowsperpage = value;
});
print("new rows: $value");
},
// mobileitembuilder: (buildcontext context, int index) {
// final i = _desserts[index];
// return listtile(
// title: text(i?.name),
// );
// },
onselectall: (bool value) {
for (var row in _items) {
setstate(() {
row.selected = value;
});
}
},
rowcountapproximate: true,
actions: <widget>[
iconbutton(
icon: icon(icons.info_outline),
onpressed: () {},
),
],
selectedactions: <widget>[
iconbutton(
icon: icon(icons.delete),
onpressed: () {
setstate(() {
for (var item in _items
?.where((d) => d?.selected ?? false)
?.toset()
?.tolist()) {
_items.remove(item);
}
});
},
),
],
columns: <datacolumn>[
datacolumn(
label: const text('dessert (100g serving)'),
onsort: (int columnindex, bool ascending) => _sort<string>(
(dessert d) => d.name, columnindex, ascending)),
datacolumn(
label: const text('calories'),
tooltip:
'the total amount of food energy in the given serving size.',
numeric: true,
onsort: (int columnindex, bool ascending) => _sort<num>(
(dessert d) => d.calories, columnindex, ascending)),
datacolumn(
label: const text('fat (g)'),
numeric: true,
onsort: (int columnindex, bool ascending) =>
_sort<num>((dessert d) => d.fat, columnindex, ascending)),
datacolumn(
label: const text('carbs (g)'),
numeric: true,
onsort: (int columnindex, bool ascending) =>
_sort<num>((dessert d) => d.carbs, columnindex, ascending)),
datacolumn(
label: const text('protein (g)'),
numeric: true,
onsort: (int columnindex, bool ascending) => _sort<num>(
(dessert d) => d.protein, columnindex, ascending)),
datacolumn(
label: const text('sodium (mg)'),
numeric: true,
onsort: (int columnindex, bool ascending) => _sort<num>(
(dessert d) => d.sodium, columnindex, ascending)),
datacolumn(
label: const text('calcium (%)'),
tooltip:
'the amount of calcium as a percentage of the recommended daily amount.',
numeric: true,
onsort: (int columnindex, bool ascending) => _sort<num>(
(dessert d) => d.calcium, columnindex, ascending)),
datacolumn(
label: const text('iron (%)'),
numeric: true,
onsort: (int columnindex, bool ascending) =>
_sort<num>((dessert d) => d.iron, columnindex, ascending)),
],
),
),
);
}
final list<dessert> _desserts = <dessert>[
dessert('frozen yogurt', 159, 6.0, 24, 4.0, 87, 14, 1),
dessert('ice cream sandwich', 237, 9.0, 37, 4.3, 129, 8, 1),
dessert('eclair', 262, 16.0, 24, 6.0, 337, 6, 7),
dessert('cupcake', 305, 3.7, 67, 4.3, 413, 3, 8),
dessert('gingerbread', 356, 16.0, 49, 3.9, 327, 7, 16),
dessert('jelly bean', 375, 0.0, 94, 0.0, 50, 0, 0),
dessert('lollipop', 392, 0.2, 98, 0.0, 38, 0, 2),
dessert('honeycomb', 408, 3.2, 87, 6.5, 562, 0, 45),
dessert('donut', 452, 25.0, 51, 4.9, 326, 2, 22),
dessert('kitkat', 518, 26.0, 65, 7.0, 54, 12, 6),
dessert('frozen yogurt with sugar', 168, 6.0, 26, 4.0, 87, 14, 1),
dessert('ice cream sandwich with sugar', 246, 9.0, 39, 4.3, 129, 8, 1),
dessert('eclair with sugar', 271, 16.0, 26, 6.0, 337, 6, 7),
dessert('cupcake with sugar', 314, 3.7, 69, 4.3, 413, 3, 8),
dessert('gingerbread with sugar', 345, 16.0, 51, 3.9, 327, 7, 16),
dessert('jelly bean with sugar', 364, 0.0, 96, 0.0, 50, 0, 0),
dessert('lollipop with sugar', 401, 0.2, 100, 0.0, 38, 0, 2),
dessert('honeycomb with sugar', 417, 3.2, 89, 6.5, 562, 0, 45),
dessert('donut with sugar', 461, 25.0, 53, 4.9, 326, 2, 22),
dessert('kitkat with sugar', 527, 26.0, 67, 7.0, 54, 12, 6),
dessert('frozen yogurt with honey', 223, 6.0, 36, 4.0, 87, 14, 1),
dessert('ice cream sandwich with honey', 301, 9.0, 49, 4.3, 129, 8, 1),
dessert('eclair with honey', 326, 16.0, 36, 6.0, 337, 6, 7),
dessert('cupcake with honey', 369, 3.7, 79, 4.3, 413, 3, 8),
dessert('gingerbread with honey', 420, 16.0, 61, 3.9, 327, 7, 16),
dessert('jelly bean with honey', 439, 0.0, 106, 0.0, 50, 0, 0),
dessert('lollipop with honey', 456, 0.2, 110, 0.0, 38, 0, 2),
dessert('honeycomb with honey', 472, 3.2, 99, 6.5, 562, 0, 45),
dessert('donut with honey', 516, 25.0, 63, 4.9, 326, 2, 22),
dessert('kitkat with honey', 582, 26.0, 77, 7.0, 54, 12, 6),
dessert('frozen yogurt with milk', 262, 8.4, 36, 12.0, 194, 44, 1),
dessert('ice cream sandwich with milk', 339, 11.4, 49, 12.3, 236, 38, 1),
dessert('eclair with milk', 365, 18.4, 36, 14.0, 444, 36, 7),
dessert('cupcake with milk', 408, 6.1, 79, 12.3, 520, 33, 8),
dessert('gingerbread with milk', 459, 18.4, 61, 11.9, 434, 37, 16),
dessert('jelly bean with milk', 478, 2.4, 106, 8.0, 157, 30, 0),
dessert('lollipop with milk', 495, 2.6, 110, 8.0, 145, 30, 2),
dessert('honeycomb with milk', 511, 5.6, 99, 14.5, 669, 30, 45),
dessert('donut with milk', 555, 27.4, 63, 12.9, 433, 32, 22),
dessert('kitkat with milk', 621, 28.4, 77, 15.0, 161, 42, 6),
dessert('coconut slice and frozen yogurt', 318, 21.0, 31, 5.5, 96, 14, 7),
dessert(
'coconut slice and ice cream sandwich', 396, 24.0, 44, 5.8, 138, 8, 7),
dessert('coconut slice and eclair', 421, 31.0, 31, 7.5, 346, 6, 13),
dessert('coconut slice and cupcake', 464, 18.7, 74, 5.8, 422, 3, 14),
dessert('coconut slice and gingerbread', 515, 31.0, 56, 5.4, 316, 7, 22),
dessert('coconut slice and jelly bean', 534, 15.0, 101, 1.5, 59, 0, 6),
dessert('coconut slice and lollipop', 551, 15.2, 105, 1.5, 47, 0, 8),
dessert('coconut slice and honeycomb', 567, 18.2, 94, 8.0, 571, 0, 51),
dessert('coconut slice and donut', 611, 40.0, 58, 6.4, 335, 2, 28),
dessert('coconut slice and kitkat', 677, 41.0, 72, 8.5, 63, 12, 12),
];
}
Comments are closed.