Skip to content

Instantly share code, notes, and snippets.

@radzish
Created June 11, 2022 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radzish/2efe24d5917840f7bca5b4b72ac921d5 to your computer and use it in GitHub Desktop.
Save radzish/2efe24d5917840f7bca5b4b72ac921d5 to your computer and use it in GitHub Desktop.
void main() {
final grid = Grid<Employee>(
columns: [
Column<Employee, String>(
valueResolver: (employee) => employee.name,
valueFormatter: (value) => value.toUpperCase(),
),
Column<Employee, double>(
valueResolver: (employee) => employee.salary,
valueFormatter: (value) => value.toStringAsFixed(2),
),
],
items: [
Employee('John', 100.24),
Employee('Mary', 130.15),
],
);
grid.render();
}
typedef CellValueFormatter<VALUE> = String Function(VALUE);
typedef CellValueResolver<ITEM, VALUE> = VALUE Function(ITEM);
class Employee {
Employee(this.name, this.salary);
final String name;
final double salary;
}
class Grid<ITEM> {
final List<Column<ITEM, Object>> columns;
final List<ITEM> items;
Grid({
required this.columns,
required this.items,
});
void render() {
for (final item in items) {
final result = StringBuffer();
for (final column in columns) {
final value = column.valueResolver(item);
final formattedValue = column.valueFormatter(value);
result.write(formattedValue);
result.write(';');
}
print(result.toString());
}
}
}
class Column<ITEM, VALUE> {
Column({
required this.valueResolver,
required this.valueFormatter,
});
final CellValueResolver<ITEM, VALUE> valueResolver;
final CellValueFormatter<VALUE> valueFormatter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment