Roles & Permissions
Loading preview
Starting the Flutter app
A Flutter roles and permissions matrix with toggleable access per role.
import 'package:flutter/material.dart';
class RolesPermissions extends StatefulWidget {
const RolesPermissions({super.key});
@override
State<RolesPermissions> createState() => _RolesPermissionsState();
}
class _RolesPermissionsState extends State<RolesPermissions> {
static const _roles = ['Admin', 'Editor', 'Viewer'];
static const _perms = [
'View content',
'Create content',
'Edit content',
'Delete content',
'Manage users',
'Manage billing',
];
// grid[permIndex][roleIndex]
final List<List<bool>> _grid = [
[true, true, true],
[true, true, false],
[true, true, false],
[true, false, false],
[true, false, false],
[true, false, false],
];
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final scheme = theme.colorScheme;
return Scaffold(
appBar: AppBar(title: const Text('Roles & Permissions')),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
Card(
clipBehavior: Clip.antiAlias,
child: Column(
children: [
Container(
color: scheme.surfaceContainerHigh,
padding: const EdgeInsets.symmetric(vertical: 14),
child: Row(
children: [
const Expanded(
flex: 3,
child: Padding(
padding: EdgeInsets.only(left: 16),
child: Text('Permission',
style:
TextStyle(fontWeight: FontWeight.bold)),
)),
for (final r in _roles)
Expanded(
child: Center(
child: Text(r,
style: const TextStyle(
fontWeight: FontWeight.bold)),
),
),
],
),
),
for (var p = 0; p < _perms.length; p++)
Container(
decoration: BoxDecoration(
border: Border(
top: BorderSide(color: scheme.outlineVariant)),
),
child: Row(
children: [
Expanded(
flex: 3,
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16, vertical: 6),
child: Text(_perms[p]),
),
),
for (var r = 0; r < _roles.length; r++)
Expanded(
child: Center(
child: Checkbox(
value: _grid[p][r],
onChanged: r == 0
? null
: (v) => setState(
() => _grid[p][r] = v ?? false),
),
),
),
],
),
),
],
),
),
const SizedBox(height: 12),
Text('Admin permissions are locked and cannot be changed.',
style: theme.textTheme.bodySmall
?.copyWith(color: scheme.onSurfaceVariant)),
],
),
);
}
}
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 role-by-permission matrix is drawn from _roles, _perms and a nested _grid of booleans indexed as _grid[p][r]. Ticking a Checkbox mutates that list in setState and nothing is saved, so changes vanish with the screen. The Admin column is locked by passing a null onChanged when r == 0, which relies on Admin staying first in _roles.
Adapting it
- Build
_gridfrom your backend’s response. A map keyed by permission id is safer than lists aligned by position. - Send updates from the
onChangedcallback, or add a save button and batch them. - Replace the
r == 0check with a per-role locked flag if roles become user-defined.