import 'package:flutter/material.dart'; import '../theme/app_design_tokens.dart'; /// One button of an [EmptyState]: a label, an optional icon and its action. typedef EmptyStateAction = ({ String label, IconData? icon, VoidCallback onPressed, }); /// Builds an [EmptyStateAction]; a named constructor reads better at call /// sites than a record literal. EmptyStateAction emptyStateAction( String label, VoidCallback onPressed, { IconData? icon, }) => (label: label, icon: icon, onPressed: onPressed); /// What a list shows when it has nothing yet: never an error, always a next /// step. One look on every screen: a large icon on a tonal disc, a short /// title, one muted line of explanation and up to two actions (the primary /// one filled). [child] adds content under the message, such as a command to /// copy. The whole state scrolls in a short pane instead of overflowing. /// /// Shapes follow the active design: the disc takes the design's corner /// radius (square under Terminal, round under Classic and Paper). class EmptyState extends StatelessWidget { const EmptyState({ super.key, required this.icon, required this.title, this.message, this.action, this.secondaryAction, this.child, this.dense = false, }); final IconData icon; final String title; final String? message; final EmptyStateAction? action; final EmptyStateAction? secondaryAction; final Widget? child; /// A smaller icon and tighter spacing, for narrow list panes. final bool dense; @override Widget build(BuildContext context) { final theme = Theme.of(context); final scheme = theme.colorScheme; final tokens = AppDesignTokens.of(context); final disc = dense ? 56.0 : 72.0; final primary = action; final secondary = secondaryAction; return Center( child: SingleChildScrollView( padding: const EdgeInsets.all(24), child: ConstrainedBox( constraints: const BoxConstraints(maxWidth: 420), child: Semantics( container: true, child: Column( mainAxisSize: MainAxisSize.min, children: [ DecoratedBox( decoration: BoxDecoration( color: scheme.secondaryContainer, borderRadius: tokens.corners( BorderRadius.circular(disc / 2), ), ), child: SizedBox.square( dimension: disc, child: Icon( icon, size: disc / 2, color: scheme.onSecondaryContainer, ), ), ), SizedBox(height: dense ? 12 : 16), Text( title, textAlign: TextAlign.center, style: theme.textTheme.titleMedium, ), if (message case final message?) ...[ const SizedBox(height: 6), Text( message, textAlign: TextAlign.center, style: theme.textTheme.bodyMedium?.copyWith( color: scheme.onSurfaceVariant, ), ), ], if (child case final child?) ...[ const SizedBox(height: 16), child, ], if (primary != null || secondary != null) ...[ SizedBox(height: dense ? 16 : 20), Wrap( alignment: WrapAlignment.center, spacing: 8, runSpacing: 8, children: [ if (primary != null) primary.icon == null ? FilledButton( onPressed: primary.onPressed, child: Text(primary.label), ) : FilledButton.icon( onPressed: primary.onPressed, icon: Icon(primary.icon), label: Text(primary.label), ), if (secondary != null) secondary.icon == null ? TextButton( onPressed: secondary.onPressed, child: Text(secondary.label), ) : TextButton.icon( onPressed: secondary.onPressed, icon: Icon(secondary.icon), label: Text(secondary.label), ), ], ), ], ], ), ), ), ), ); } }