apptomate_custom_data_table 0.0.1
apptomate_custom_data_table: ^0.0.1 copied to clipboard
An enhanced DataTable widget with customizable styling and layout options for Flutter applications.
example/lib/main.dart
import 'package:apptomate_custom_data_table/apptomate_custom_data_table.dart';
import 'package:flutter/material.dart';
void main() {
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(
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _CustomDataTableExampleState();
}
class _CustomDataTableExampleState extends State<MyHomePage> {
bool _sortAscending = true;
List<Map<String, dynamic>> _data = [
{'name': 'John Doe', 'age': 30, 'email': 'john@example.com'},
{'name': 'Jane Smith', 'age': 28, 'email': 'jane@example.com'},
{'name': 'Alice Johnson', 'age': 25, 'email': 'alice@example.com'},
];
void _sortData(int columnIndex, bool ascending) {
setState(() {
if (columnIndex == 0) {
_data.sort((a, b) => ascending
? a['name'].compareTo(b['name'])
: b['name'].compareTo(a['name']));
} else if (columnIndex == 1) {
_data.sort((a, b) => ascending
? a['age'].compareTo(b['age'])
: b['age'].compareTo(a['age']));
}
_sortAscending = ascending;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Custom Data Table')),
body: Padding(
padding: const EdgeInsets.all(12),
child: CustomDataTable(
headingRowColor: Colors.blueAccent,
columnSpacing: 16,
dataRowHeight: 56,
showCheckboxColumn: false,
columns: [
DataColumn(
label: const Text(
'Name',
style: TextStyle(fontWeight: FontWeight.bold),
),
onSort: (columnIndex, ascending) {
_sortData(columnIndex, ascending);
},
),
DataColumn(
label: const Text(
'Age',
style: TextStyle(fontWeight: FontWeight.bold),
),
onSort: (columnIndex, ascending) {
_sortData(columnIndex, ascending);
},
),
const DataColumn(
label: Text(
'Email',
style: TextStyle(fontWeight: FontWeight.bold),
),
),
const DataColumn(
label: Text(
'Actions',
style: TextStyle(fontWeight: FontWeight.bold),
),
),
],
rows: _data
.map(
(data) => DataRow(
cells: [
DataCell(Text(data['name'])),
DataCell(Text(data['age'].toString())),
DataCell(Text(data['email'])),
DataCell(
IconButton(
icon: const Icon(Icons.delete, color: Colors.red),
onPressed: () {
setState(() {
_data.remove(data);
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('${data['name']} deleted'),
),
);
},
),
),
],
),
)
.toList(),
),
),
);
}
}