home_screen.dart 11 KB

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