From 74931bb25c30e972e379502c99fcd612bcfc093e Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Wed, 1 Mar 2023 22:13:50 +0600 Subject: [PATCH] feat: launch at startup toggle and material 3 --- lib/components/ui/top_bar.dart | 10 +++++----- lib/hooks/use_force_updater.dart | 19 +++++++++++++++++++ lib/main.dart | 3 ++- lib/pages/settings/settings.dart | 25 +++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 lib/hooks/use_force_updater.dart diff --git a/lib/components/ui/top_bar.dart b/lib/components/ui/top_bar.dart index 877a5f3..1fcaf60 100644 --- a/lib/components/ui/top_bar.dart +++ b/lib/components/ui/top_bar.dart @@ -15,9 +15,9 @@ class TopBar extends StatelessWidget with PreferredSizeWidget { height: 20, width: double.infinity, color: Colors.transparent, - child: ElevatedButtonTheme( - data: ElevatedButtonThemeData( - style: ElevatedButton.styleFrom( + child: FilledButtonTheme( + data: FilledButtonThemeData( + style: FilledButton.styleFrom( shape: const CircleBorder(), minimumSize: const Size(10, 10), ), @@ -40,7 +40,7 @@ class TopBar extends StatelessWidget with PreferredSizeWidget { start: 0, child: Padding( padding: const EdgeInsets.only(top: 5.0), - child: ElevatedButton( + child: FilledButton( onPressed: () => Navigator.of(context).pop(), child: const Icon(Icons.arrow_back, size: 14), ), @@ -51,7 +51,7 @@ class TopBar extends StatelessWidget with PreferredSizeWidget { end: 0, child: Padding( padding: const EdgeInsets.only(top: 5.0), - child: ElevatedButton( + child: FilledButton( onPressed: () => CloseWindowAction().invoke(const CloseWindowIntent()), child: const Icon(Icons.close, size: 14), diff --git a/lib/hooks/use_force_updater.dart b/lib/hooks/use_force_updater.dart new file mode 100644 index 0000000..d47d85a --- /dev/null +++ b/lib/hooks/use_force_updater.dart @@ -0,0 +1,19 @@ +import 'package:flutter/scheduler.dart'; +import 'package:flutter_hooks/flutter_hooks.dart'; + +void Function([dynamic]) useForceUpdater() { + final state = useState(false); + final isMounted = useIsMounted(); + return ([_]) async { + if (!isMounted()) return; + + // if there's a current frame, + if (SchedulerBinding.instance.schedulerPhase != SchedulerPhase.idle) { + // wait for the end of that frame. + await SchedulerBinding.instance.endOfFrame; + if (!isMounted()) return; + } + + state.value = !state.value; + }; +} diff --git a/lib/main.dart b/lib/main.dart index c09d673..435cf9f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -160,8 +160,9 @@ class _FlemoziState extends State with WidgetsBindingObserver { child: MaterialApp( debugShowCheckedModeBanner: false, themeMode: ThemeMode.dark, - theme: ThemeData.light(), + theme: ThemeData.light(useMaterial3: true), darkTheme: ThemeData( + useMaterial3: true, brightness: Brightness.dark, colorSchemeSeed: SystemTheme.accentColor.accent, splashFactory: NoSplash.splashFactory, diff --git a/lib/pages/settings/settings.dart b/lib/pages/settings/settings.dart index 397348c..8c8681a 100644 --- a/lib/pages/settings/settings.dart +++ b/lib/pages/settings/settings.dart @@ -1,6 +1,9 @@ import 'package:flemozi/components/ui/top_bar.dart'; +import 'package:flemozi/hooks/use_force_updater.dart'; import 'package:flemozi/pages/settings/about.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_hooks/flutter_hooks.dart'; +import 'package:launch_at_startup/launch_at_startup.dart'; class Settings extends StatelessWidget { const Settings({super.key}); @@ -20,6 +23,28 @@ class Settings extends StatelessWidget { child: ListView( padding: const EdgeInsets.all(20), children: [ + HookBuilder( + builder: (context) { + final update = useForceUpdater(); + final snapshot = useFuture(launchAtStartup.isEnabled()); + return SwitchListTile( + secondary: const Icon(Icons.power_settings_new), + title: const Text('Launch at startup'), + value: snapshot.data ?? false, + onChanged: !snapshot.hasData + ? null + : (value) async { + if (value) { + await launchAtStartup.enable(); + } else { + await launchAtStartup.disable(); + } + update(); + }, + ); + }, + ), + const SizedBox(height: 5), ListTile( title: const Text('About'), leading: const Icon(Icons.info),