server_table 1.1.0
server_table: ^1.1.0 copied to clipboard
Backend-agnostic Flutter data table with server-side pagination, search, sorting, filtering, configurable response mapping, OData support, and custom REST query serializers.
import 'package:example/api/api_client.dart';
import 'package:example/customer/customer_datasource.dart';
import 'package:example/model/customer.dart';
import 'package:example/model/dummy_product.dart';
import 'package:example/model/product.dart';
import 'package:flutter/material.dart';
import 'package:server_table/server_table.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
configureAuth();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a purple toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),
home: CustomerPage(),
);
}
}
class CustomerPage extends StatelessWidget {
CustomerPage({super.key});
final columnscustomer = [
ServerColumn<Customer>(
title: 'ID',
field: 'id',
sortable: true,
width: 100,
cellBuilder: (context, customer) {
return Text(customer.id.toString());
},
),
ServerColumn<Customer>(
title: 'Name',
field: 'name',
sortable: true,
filterable: true,
width: 200,
cellBuilder: (context, customer) {
return Text(customer.name);
},
),
ServerColumn<Customer>(
title: 'Email',
field: 'emailAddress',
sortable: true,
filterable: true,
width: 250,
cellBuilder: (context, customer) {
return Text(customer.email ?? "");
},
),
];
final columns = [
ServerColumn<Product>(
title: 'ID',
field: 'id',
sortable: true,
width: 100,
cellBuilder: (context, product) {
return Text(product.id.toString());
},
),
ServerColumn<Product>(
title: 'Name',
field: 'name',
sortable: true,
filterable: true,
width: 200,
cellBuilder: (context, product) {
return Text(product.name);
},
),
ServerColumn<Product>(
title: 'Hsncode',
field: 'hscncode',
sortable: true,
filterable: true,
width: 250,
cellBuilder: (context, product) {
return Text(product.hsncode);
},
),
ServerColumn<Product>(
title: 'Price',
field: 'sellingprice',
sortable: true,
filterable: true,
width: 250,
cellBuilder: (context, product) {
return Text(product.sellingPrice.toString());
},
),
ServerColumn<Product>(
title: 'Unit',
field: 'unitId',
sortable: true,
filterable: true,
width: 250,
cellBuilder: (context, product) {
return Text(product.unit?.name ?? "");
},
),
ServerColumn<Product>(
title: 'Tax',
field: 'taxpercentage',
sortable: true,
filterable: true,
width: 250,
cellBuilder: (context, product) {
return Text(product.taxPercentage.toString());
},
),
ServerColumn<Product>(
title: 'productCode',
field: 'Productcode',
sortable: true,
filterable: true,
width: 250,
cellBuilder: (context, product) {
return Text(product.productCode.toString());
},
),
];
final dummyColumns = [
ServerColumn<DummyProduct>(
title: 'ID',
field: 'id',
width: 80,
cellBuilder: (_, p) => Text('${p.id}'),
),
ServerColumn<DummyProduct>(
title: 'Title',
field: 'title',
sortable: true,
filterable: true,
width: 250,
cellBuilder: (_, p) => Text(p.title),
),
ServerColumn<DummyProduct>(
title: 'Brand',
field: 'brand',
width: 170,
cellBuilder: (_, p) => Text(p.brand),
),
ServerColumn<DummyProduct>(
title: 'Category',
field: 'category',
filterable: true,
sortable: true,
width: 180,
cellBuilder: (_, p) => Text(p.category),
),
ServerColumn<DummyProduct>(
title: 'Price',
field: 'price',
width: 120,
filterable: true,
sortable: true,
cellBuilder: (_, p) => Text('\$${p.price}'),
),
ServerColumn<DummyProduct>(
title: 'Rating',
field: 'rating',
width: 120,
cellBuilder: (_, p) => Text('${p.rating}'),
),
ServerColumn<DummyProduct>(
title: 'Stock',
field: 'stock',
width: 120,
filterable: true,
sortable: true,
cellBuilder: (_, p) => Text('${p.stock}'),
),
];
final productController = ServerTableController<Product>(
dataSource: productSource,
);
final customerController = ServerTableController<Customer>(
dataSource: customerSource,
);
final dummyController = ServerTableController<DummyProduct>(
dataSource: dummySource,
);
final jsonSourceController = ServerTableController<DummyProduct>(
dataSource: jsonSource,
);
@override
Widget build(BuildContext context) {
return Scaffold(
body: ServerTable<Customer>(
controller: customerController,
columns: columnscustomer,
),
);
}
}