import "package:flutter/widgets.dart"; import "package:garage_ui/garage_ui.dart"; import "package:garage_updates/garage_updates.dart"; import "package:go_router/go_router.dart"; // The whole integration is the one call at the top of main. Everything under // it is just an app with a "restart to update" button, which is the only // other thing an app has to do for itself. // // Run it with `flutter run` and the updater prints that this copy isnt an // install and stays out of the way - it only ever touches a release build // that the installer (or a previous update) put in place. Future main(List args) async { // an app with helpers of its own (an agent, a tray) stops them here GarageUpdater.instance.onBeforeRestart = () async { print("about to restart into the new build"); }; await GarageUpdater.launch( args: args, project: "proj_example", build: 12, // the number after the + in pubspec's version label: "v2610-1", branding: const UpdaterBranding( name: "Example App", logo: Icon(LucideIcons.package), ), mode: UpdateMode.prompt, ); runApp(const ExampleApp()); } final _theme = ThemeData( colorScheme: GarageSchemes.carbon, density: const Density.product(), ); final _router = GoRouter( routes: [ GoRoute(path: "/", builder: (context, state) => const HomePage()), ], ); class ExampleApp extends StatelessWidget { const ExampleApp({super.key}); @override Widget build(BuildContext context) { return GarageApp.router(routerConfig: _router, theme: _theme, title: "Example App"); } } class HomePage extends StatefulWidget { const HomePage({super.key}); @override State createState() => _HomePageState(); } class _HomePageState extends State { String? _error; Future _restart() async { try { await GarageUpdater.instance.restartToUpdate(); } on UpdateError catch (e) { // a UAC prompt turned down, say. the update is still waiting print("couldnt restart into the update: $e"); if (mounted) setState(() => _error = e.message); } } @override Widget build(BuildContext context) { final updater = GarageUpdater.instance; return Scaffold( child: Center( child: ValueListenableBuilder( valueListenable: updater.ready, builder: (context, waiting, _) { final latest = waiting?.latest; return Column( mainAxisSize: MainAxisSize.min, children: [ Text("Example App ${updater.label}").large().semiBold(), const Gap.sm(), if (latest == null) const Text("Up to date").muted() else ...[ Text("${latest.label} is ready to install").muted(), const Gap.sm(), // the notes are the apps to show, the splash never does if (latest.notes.isNotEmpty) Text(latest.notes).muted(), const Gap.lg(), Button( style: const ButtonStyle.primary(), onPressed: _restart, child: const Text("Restart to update"), ), ], if (_error != null) ...[ const Gap.md(), Text(_error!).muted(), ], ], ); }, ), ), ); } }