Users Table
Loading preview
Starting the Flutter app
A Flutter admin users table with search, status badges, row actions, and pagination.
import 'package:flutter/material.dart';
class _User {
final String name;
final String email;
final String role;
final String status;
const _User(this.name, this.email, this.role, this.status);
}
class UsersTable extends StatefulWidget {
const UsersTable({super.key});
@override
State<UsersTable> createState() => _UsersTableState();
}
class _UsersTableState extends State<UsersTable> {
static const _users = [
_User('Alex Kim', 'alex@acme.io', 'Admin', 'Active'),
_User('Bianca Rossi', 'bianca@acme.io', 'Editor', 'Active'),
_User('Chen Wei', 'chen@acme.io', 'Viewer', 'Invited'),
_User('Dana Ford', 'dana@acme.io', 'Editor', 'Suspended'),
_User('Elias Bauer', 'elias@acme.io', 'Admin', 'Active'),
_User('Farah Nasser', 'farah@acme.io', 'Viewer', 'Active'),
];
String _query = '';
List<_User> get _visibleUsers {
final query = _query.trim().toLowerCase();
if (query.isEmpty) return _users;
return _users
.where((u) => [u.name, u.email, u.role, u.status]
.any((field) => field.toLowerCase().contains(query)))
.toList();
}
Color _statusColor(BuildContext context, String s) {
switch (s) {
case 'Active':
return Colors.green;
case 'Invited':
return Colors.orange;
default:
return Theme.of(context).colorScheme.error;
}
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final scheme = theme.colorScheme;
final visibleUsers = _visibleUsers;
return Scaffold(
appBar: AppBar(
title: const Text('Users'),
actions: [
Padding(
padding: const EdgeInsets.all(8),
child: FilledButton.icon(
onPressed: () => _showDemo(context, 'Add user (demo)'),
icon: const Icon(Icons.add),
label: const Text('Add user')),
),
],
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(16),
child: SizedBox(
width: 320,
child: TextField(
onChanged: (value) => setState(() => _query = value),
decoration: InputDecoration(
hintText: 'Search users',
prefixIcon: const Icon(Icons.search),
isDense: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12)),
),
),
),
),
Expanded(
child: SingleChildScrollView(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: DataTable(
columns: const [
DataColumn(label: Text('Name')),
DataColumn(label: Text('Email')),
DataColumn(label: Text('Role')),
DataColumn(label: Text('Status')),
DataColumn(label: Text('')),
],
rows: [
for (final u in visibleUsers)
DataRow(cells: [
DataCell(Row(
children: [
CircleAvatar(
radius: 14,
child: Text(u.name[0],
style: const TextStyle(fontSize: 12))),
const SizedBox(width: 10),
Text(u.name),
],
)),
DataCell(Text(u.email)),
DataCell(Text(u.role)),
DataCell(Container(
padding: const EdgeInsets.symmetric(
horizontal: 10, vertical: 4),
decoration: BoxDecoration(
color: _statusColor(context, u.status)
.withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(20),
),
child: Text(u.status,
style: TextStyle(
color: _statusColor(context, u.status),
fontSize: 12,
fontWeight: FontWeight.w600)),
)),
DataCell(PopupMenuButton<String>(
itemBuilder: (context) => const [
PopupMenuItem(value: 'edit', child: Text('Edit')),
PopupMenuItem(
value: 'remove', child: Text('Remove')),
],
)),
]),
],
),
),
),
),
Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
Expanded(
child: Text(
'Showing ${visibleUsers.length} of ${_users.length} users',
overflow: TextOverflow.ellipsis,
style: TextStyle(color: scheme.onSurfaceVariant)),
),
// Every row fits on one page, so paging stays disabled.
const IconButton(
tooltip: 'Previous page',
onPressed: null, icon: Icon(Icons.chevron_left)),
const Text('1'),
const IconButton(
tooltip: 'Next page',
onPressed: null, icon: Icon(Icons.chevron_right)),
],
),
),
],
),
);
}
}
void _showDemo(BuildContext context, String message) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(SnackBar(content: Text(message)));
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
A DataTable inside nested vertical and horizontal SingleChildScrollViews renders _visibleUsers, the const _users list filtered by _query across name, email, role and status. The footer count comes from those lists. _statusColor maps status text to a pill colour, and anything other than Active or Invited gets the error colour. The PopupMenuButton has no onSelected, and the paging chevrons are disabled because every row fits.
Adapting it
- Fetch
_usersfrom your API with page and query parameters.PaginatedDataTablecan take over paging for large sets. - Debounce the search
TextField’sonChangedif it refetches. - Handle
editandremovein a newonSelectedon thePopupMenuButton.