import 'package:flutter/foundation.dart'; /// How tightly a design packs the interface: the spacing inside and between /// components, icon and avatar sizes, the navigation rail's width and labels /// and the floating window title bar. [comfortable] is the Default look; /// every widget that reads a density keeps its own value under it, so the /// comfortable designs draw exactly as before. /// /// A dense design sets [spacing] below 1: [space] then scales a widget's own /// spacing and snaps it to the 4px grid, so every component tightens by the /// same proportion instead of each picking its own number. @immutable class DesignDensity { const DesignDensity({ this.spacing = 1, this.iconSize, this.avatarSize = 40, this.railWidth = 96, this.railLabels = true, this.titleBarHeight = 40, }); /// The Default density. static const DesignDensity comfortable = DesignDensity(); /// The Compact density: spacing at 60% on the 4px grid, 18px icons, 28px /// avatars, an icon-only 64px rail and a 32px window title bar. static const DesignDensity compact = DesignDensity( spacing: 0.6, iconSize: 18, avatarSize: 28, railWidth: 64, railLabels: false, titleBarHeight: 32, ); /// The proportion of a widget's own spacing this density keeps. final double spacing; /// The interface's icon size; null keeps Material's 24. final double? iconSize; /// A list row's leading avatar. final double avatarSize; /// The destination rail's width on a wide window. final double railWidth; /// Whether the rail labels its destinations under their icons; without /// them the rail shows icons with tooltips. final bool railLabels; /// A floating window's title bar height, before text scaling. final double titleBarHeight; /// Whether this density is tighter than [comfortable]. bool get dense => spacing < 1; /// [comfortable] spacing under this density: unchanged at [spacing] 1, /// otherwise scaled and snapped to the 4px grid (a gap under 4px keeps a /// 2px step, so a hairline gap never collapses to nothing). double space(double comfortable) { if (spacing == 1 || comfortable <= 0) return comfortable; final scaled = comfortable * spacing; if (scaled < 4) return scaled < 3 ? 2 : 4; return (scaled / 4).round() * 4.0; } /// [comfortable] edge inset (a row's or a panel's side padding) under this /// density: halfway between [space] and the comfortable value, on the 4px /// grid, so a dense row's text still keeps clear of the pane's edge /// (16px becomes 12px at Compact). double inset(double comfortable) { if (spacing == 1 || comfortable <= 0) return comfortable; final scaled = comfortable * (1 + spacing) / 2; return scaled < 4 ? 4 : (scaled / 4).round() * 4.0; } /// The least height a tappable row keeps under this density: nothing /// beyond its content when comfortable (its own padding already gives /// room) or on a pointer device, and 40px on a touch device, so a dense /// row stays easy to hit with a finger. double get minTouchRow { if (!dense) return 0; return pointerPlatform ? 0 : 40; } /// Whether the device drives the app with a pointer (desktop, including a /// desktop browser). Reads the device's platform, not the theme's: web /// themes are pinned to Android. static bool get pointerPlatform => switch (defaultTargetPlatform) { TargetPlatform.macOS || TargetPlatform.linux || TargetPlatform.windows => true, TargetPlatform.android || TargetPlatform.iOS || TargetPlatform.fuchsia => false, }; @override bool operator ==(Object other) => other is DesignDensity && other.spacing == spacing && other.iconSize == iconSize && other.avatarSize == avatarSize && other.railWidth == railWidth && other.railLabels == railLabels && other.titleBarHeight == titleBarHeight; @override int get hashCode => Object.hash( spacing, iconSize, avatarSize, railWidth, railLabels, titleBarHeight, ); }