| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import 'dart:math' as math;
- import 'package:flutter/material.dart';
- import 'shortcut_tile.dart';
- class ShortcutSpec {
- const ShortcutSpec({
- required this.title,
- required this.subtitle,
- required this.iconAsset,
- required this.assigned,
- required this.onTap,
- this.onLongPress,
- });
- final String title;
- final String? subtitle;
- final String iconAsset;
- final bool assigned;
- final VoidCallback onTap;
- final VoidCallback? onLongPress;
- }
- /// Arranges the five home shortcuts in a centered 3+2 block on the car
- /// display, falling back to two columns on narrow windows.
- class ShortcutGrid extends StatelessWidget {
- const ShortcutGrid({
- super.key,
- required this.shortcuts,
- required this.compact,
- });
- final List<ShortcutSpec> shortcuts;
- final bool compact;
- @override
- Widget build(BuildContext context) {
- return LayoutBuilder(
- builder: (context, constraints) {
- if (!constraints.hasBoundedWidth || !constraints.hasBoundedHeight) {
- return const SizedBox.shrink();
- }
- final columns = constraints.maxWidth >= 520 ? 3 : 2;
- final horizontalGap = compact ? 18.0 : 24.0;
- final rowGap = compact ? 18.0 : 26.0;
- final labelAllowance = compact ? 45.0 : 50.0;
- final rows = (shortcuts.length / columns).ceil();
- final widthLimited =
- (constraints.maxWidth - horizontalGap * (columns - 1)) / columns;
- final heightLimited =
- (constraints.maxHeight - rowGap * (rows - 1)) / rows -
- labelAllowance;
- final size = math
- .min(math.min(widthLimited, heightLimited), compact ? 168.0 : 188.0)
- .clamp(112.0, 196.0);
- return Center(
- child: Wrap(
- alignment: WrapAlignment.center,
- crossAxisAlignment: WrapCrossAlignment.start,
- spacing: horizontalGap,
- runSpacing: rowGap,
- children: [
- for (final shortcut in shortcuts)
- ShortcutTile(
- title: shortcut.title,
- subtitle: shortcut.subtitle,
- iconAsset: shortcut.iconAsset,
- size: size,
- assigned: shortcut.assigned,
- onTap: shortcut.onTap,
- onLongPress: shortcut.onLongPress,
- ),
- ],
- ),
- );
- },
- );
- }
- }
|