home_screen.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:provider/provider.dart';
  4. import '../../core/app_clock.dart';
  5. import '../../core/theme/app_colors.dart';
  6. import '../../core/theme/theme_controller.dart';
  7. import '../../core/widgets/app_icon.dart';
  8. import '../../core/widgets/surface_card.dart';
  9. import '../../data/models/slot_assignment.dart';
  10. import '../../data/models/slot_type.dart';
  11. import '../apps/all_apps_screen.dart';
  12. import '../apps/app_picker_sheet.dart';
  13. import '../settings/settings_screen.dart';
  14. import 'launcher_controller.dart';
  15. import 'system_controller.dart';
  16. import 'widgets/clock_block.dart';
  17. import 'widgets/shortcut_grid.dart';
  18. import 'widgets/status_cluster.dart';
  19. import 'widgets/volume_card.dart';
  20. /// The only home page. No app bar, no dock, no pages: information, the main
  21. /// volume control, and up to 16 configurable shortcuts.
  22. class HomeScreen extends StatelessWidget {
  23. const HomeScreen({super.key});
  24. @override
  25. Widget build(BuildContext context) {
  26. final colors = context.colors;
  27. final theme = Theme.of(context);
  28. final launcher = context.watch<LauncherController>();
  29. final system = context.watch<SystemController>();
  30. final appearance = context.watch<ThemeController>();
  31. final screen = MediaQuery.sizeOf(context);
  32. final compact = screen.height < 700 || screen.width < 1100;
  33. final horizontalPadding = compact ? 30.0 : 44.0;
  34. final denseHome = launcher.homeButtonCount > 8;
  35. final leftWidth = denseHome
  36. ? (screen.width * 0.31).clamp(300.0, 400.0)
  37. : (screen.width * 0.37).clamp(300.0, 440.0);
  38. final gap = compact ? 26.0 : 40.0;
  39. final showHomeBanner = launcher.defaultHomeKnown && !launcher.isDefaultHome;
  40. ShortcutSpec slotSpec(SlotType slot) {
  41. final assignment = launcher.assignmentFor(slot);
  42. final app = assignment == null
  43. ? null
  44. : launcher.appForAssignment(assignment);
  45. final playing =
  46. slot == SlotType.music && system.isMusicPlaying && assignment != null;
  47. final nonSystemIcon = app != null && !app.isSystemApp ? app.icon : null;
  48. return ShortcutSpec(
  49. title: slot.label,
  50. subtitle: assignment?.label ?? 'Select app',
  51. iconAsset: slot.iconAsset,
  52. iconBytes: playing ? app?.icon : nonSystemIcon,
  53. showEqualizer: playing,
  54. assigned: assignment != null,
  55. onTap: () => _openSlot(context, slot),
  56. onLongPress: () => showAppPicker(context, slot),
  57. longPressHint: 'change the app',
  58. );
  59. }
  60. ShortcutSpec pinnedSpec(SlotAssignment pinned) {
  61. final app = launcher.appForAssignment(pinned);
  62. final useRealIcon = app != null && !app.isSystemApp;
  63. return ShortcutSpec(
  64. title: pinned.label.isEmpty ? pinned.packageName : pinned.label,
  65. subtitle: null,
  66. iconAsset: 'assets/icons/app-window.svg',
  67. iconBytes: useRealIcon ? app.icon : null,
  68. assigned: true,
  69. onTap: () => _openPinned(context, pinned),
  70. onLongPress: () => _removePinned(context, pinned),
  71. longPressHint: 'remove from Home',
  72. );
  73. }
  74. final shortcuts = <ShortcutSpec>[
  75. for (final slot in SlotType.values)
  76. if (launcher.isSlotEnabled(slot)) slotSpec(slot),
  77. for (final pinned in launcher.pinnedApps) pinnedSpec(pinned),
  78. ShortcutSpec(
  79. title: 'Settings',
  80. subtitle: 'Theme & PIN',
  81. iconAsset: 'assets/icons/settings.svg',
  82. assigned: true,
  83. onTap: () => _openSettings(context),
  84. ),
  85. ShortcutSpec(
  86. title: 'All apps',
  87. subtitle: 'Installed apps',
  88. iconAsset: 'assets/icons/layout-grid.svg',
  89. assigned: true,
  90. onTap: () => _openAllApps(context),
  91. ),
  92. ];
  93. return PopScope(
  94. canPop: false,
  95. child: Scaffold(
  96. backgroundColor: colors.canvas,
  97. body: Stack(
  98. children: [
  99. Positioned.fill(
  100. child: DecoratedBox(
  101. decoration: homeBackground(
  102. colors,
  103. Theme.of(context).brightness,
  104. ),
  105. ),
  106. ),
  107. Positioned(
  108. top: -190,
  109. right: -120,
  110. child: IgnorePointer(
  111. child: Container(
  112. width: 560,
  113. height: 560,
  114. decoration: BoxDecoration(
  115. shape: BoxShape.circle,
  116. gradient: RadialGradient(
  117. colors: [
  118. colors.accent.withValues(
  119. alpha: Theme.of(context).brightness == Brightness.dark
  120. ? 0.16
  121. : 0.10,
  122. ),
  123. colors.accent.withValues(alpha: 0),
  124. ],
  125. ),
  126. ),
  127. ),
  128. ),
  129. ),
  130. SafeArea(
  131. minimum: EdgeInsets.fromLTRB(
  132. horizontalPadding,
  133. compact ? 18 : 24,
  134. horizontalPadding,
  135. compact ? 14 : 18,
  136. ),
  137. child: SizedBox.expand(
  138. child: Column(
  139. crossAxisAlignment: CrossAxisAlignment.stretch,
  140. children: [
  141. Row(
  142. crossAxisAlignment: CrossAxisAlignment.start,
  143. children: [
  144. ClockBlock(compact: compact),
  145. const Spacer(),
  146. StatusCluster(
  147. status: system.status,
  148. compact: compact,
  149. showBattery: appearance.showBattery,
  150. ),
  151. ],
  152. ),
  153. SizedBox(height: compact ? 16 : 24),
  154. Expanded(
  155. child: Row(
  156. crossAxisAlignment: CrossAxisAlignment.stretch,
  157. children: [
  158. SizedBox(
  159. width: leftWidth,
  160. child: Column(
  161. crossAxisAlignment: CrossAxisAlignment.stretch,
  162. mainAxisAlignment: MainAxisAlignment.end,
  163. children: [
  164. if (showHomeBanner) ...[
  165. _HomeRoleBanner(
  166. launcher: launcher,
  167. onSetUp: () => _requestHome(context),
  168. ),
  169. const SizedBox(height: 14),
  170. ],
  171. VolumeCard(
  172. volume: system.volume,
  173. compact: compact,
  174. onSetVolume: system.setVolume,
  175. onToggleMute: system.toggleMute,
  176. ),
  177. ],
  178. ),
  179. ),
  180. SizedBox(width: gap),
  181. Expanded(
  182. child: Column(
  183. children: [
  184. Expanded(
  185. child: ShortcutGrid(
  186. shortcuts: shortcuts,
  187. compact: compact,
  188. ),
  189. ),
  190. if (!compact) ...[
  191. const SizedBox(height: 4),
  192. _HomeHint(
  193. text: launcher.pinnedApps.isEmpty
  194. ? 'Hold a shortcut to change the app'
  195. : 'Hold a slot to change · hold an added app to remove',
  196. style: theme.textTheme.bodySmall?.copyWith(
  197. color: colors.inkFaint,
  198. ),
  199. ),
  200. ],
  201. ],
  202. ),
  203. ),
  204. ],
  205. ),
  206. ),
  207. ],
  208. ),
  209. ),
  210. ),
  211. ],
  212. ),
  213. ),
  214. );
  215. }
  216. Future<void> _openPinned(
  217. BuildContext context,
  218. SlotAssignment assignment,
  219. ) async {
  220. final error = await context.read<LauncherController>().launchAssignment(
  221. assignment,
  222. );
  223. if (error != null && context.mounted) {
  224. _showMessage(context, error);
  225. }
  226. }
  227. void _removePinned(BuildContext context, SlotAssignment assignment) {
  228. unawaited(context.read<LauncherController>().removePinnedApp(assignment));
  229. _showMessage(
  230. context,
  231. '${assignment.label.isEmpty ? 'App' : assignment.label} removed from Home',
  232. );
  233. }
  234. Future<void> _openSlot(BuildContext context, SlotType slot) async {
  235. final launcher = context.read<LauncherController>();
  236. if (launcher.assignmentFor(slot) == null) {
  237. await showAppPicker(context, slot);
  238. return;
  239. }
  240. final error = await launcher.launchSlot(slot);
  241. if (error != null && context.mounted) {
  242. _showMessage(context, error);
  243. }
  244. }
  245. void _openSettings(BuildContext context) {
  246. Navigator.of(context)
  247. .push(MaterialPageRoute<void>(builder: (_) => const SettingsScreen()));
  248. }
  249. void _openAllApps(BuildContext context) {
  250. Navigator.of(context)
  251. .push(MaterialPageRoute<void>(builder: (_) => const AllAppsScreen()));
  252. }
  253. Future<void> _requestHome(BuildContext context) async {
  254. final launcher = context.read<LauncherController>();
  255. final launched = await launcher.requestDefaultHome();
  256. if (!launched && context.mounted) {
  257. _showMessage(context, 'Could not open the home app settings.');
  258. }
  259. }
  260. void _showMessage(BuildContext context, String message) {
  261. final messenger = ScaffoldMessenger.of(context);
  262. messenger.hideCurrentSnackBar();
  263. messenger.showSnackBar(SnackBar(content: Text(message)));
  264. }
  265. }
  266. class _HomeRoleBanner extends StatelessWidget {
  267. const _HomeRoleBanner({required this.launcher, required this.onSetUp});
  268. final LauncherController launcher;
  269. final VoidCallback onSetUp;
  270. @override
  271. Widget build(BuildContext context) {
  272. final colors = context.colors;
  273. final theme = Theme.of(context);
  274. return SurfaceCard(
  275. padding: const EdgeInsets.fromLTRB(18, 16, 18, 16),
  276. radius: 24,
  277. color: colors.accentSoft.withValues(alpha: 0.62),
  278. borderColor: colors.accent.withValues(alpha: 0.18),
  279. showShadow: false,
  280. child: Row(
  281. children: [
  282. Container(
  283. width: 38,
  284. height: 38,
  285. decoration: BoxDecoration(
  286. color: colors.accent.withValues(alpha: 0.14),
  287. borderRadius: BorderRadius.circular(13),
  288. ),
  289. alignment: Alignment.center,
  290. child: AppIcon(
  291. 'assets/icons/smartphone.svg',
  292. size: 18,
  293. color: colors.accent,
  294. ),
  295. ),
  296. const SizedBox(width: 13),
  297. Expanded(
  298. child: Column(
  299. crossAxisAlignment: CrossAxisAlignment.start,
  300. children: [
  301. Text(
  302. 'Make CarDash your home app',
  303. style: theme.textTheme.titleSmall,
  304. ),
  305. const SizedBox(height: 2),
  306. Text(
  307. 'Open it with the Home button.',
  308. style: theme.textTheme.bodySmall?.copyWith(
  309. color: colors.inkMuted,
  310. ),
  311. ),
  312. ],
  313. ),
  314. ),
  315. TextButton(
  316. onPressed: onSetUp,
  317. style: TextButton.styleFrom(
  318. foregroundColor: colors.accent,
  319. textStyle: theme.textTheme.labelLarge,
  320. ),
  321. child: const Text('Set up'),
  322. ),
  323. ],
  324. ),
  325. );
  326. }
  327. }
  328. class _HomeHint extends StatefulWidget {
  329. const _HomeHint({required this.text, this.style});
  330. final String text;
  331. final TextStyle? style;
  332. @override
  333. State<_HomeHint> createState() => _HomeHintState();
  334. }
  335. class _HomeHintState extends State<_HomeHint> {
  336. Timer? _timer;
  337. bool _visible = true;
  338. @override
  339. void initState() {
  340. super.initState();
  341. final elapsed = DateTime.now().difference(AppClock.startedAt);
  342. final remaining = const Duration(minutes: 2) - elapsed;
  343. if (remaining <= Duration.zero) {
  344. _visible = false;
  345. } else {
  346. _timer = Timer(remaining, () {
  347. if (mounted) setState(() => _visible = false);
  348. });
  349. }
  350. }
  351. @override
  352. void dispose() {
  353. _timer?.cancel();
  354. super.dispose();
  355. }
  356. @override
  357. Widget build(BuildContext context) {
  358. return AnimatedSwitcher(
  359. duration: const Duration(milliseconds: 350),
  360. switchInCurve: Curves.easeOut,
  361. switchOutCurve: Curves.easeIn,
  362. child: _visible
  363. ? Text(
  364. widget.text,
  365. key: const ValueKey('home-hint'),
  366. textAlign: TextAlign.center,
  367. style: widget.style,
  368. )
  369. : const SizedBox.shrink(key: ValueKey('home-hint-gone')),
  370. );
  371. }
  372. }