mirror of
https://github.com/foss42/apidash.git
synced 2025-05-21 00:09:55 +08:00
feat: Save on close
This commit is contained in:
72
lib/app.dart
72
lib/app.dart
@ -1,3 +1,5 @@
|
||||
// ignore_for_file: use_build_context_synchronously
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_manager/window_manager.dart' hide WindowCaption;
|
||||
@ -18,6 +20,19 @@ class _AppState extends ConsumerState<App> with WindowListener {
|
||||
void initState() {
|
||||
super.initState();
|
||||
windowManager.addListener(this);
|
||||
_init();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
windowManager.removeListener(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _init() async {
|
||||
// Add this line to override the default close handler
|
||||
await windowManager.setPreventClose(true);
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
@override
|
||||
@ -37,39 +52,67 @@ class _AppState extends ConsumerState<App> with WindowListener {
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void onWindowClose() async {
|
||||
bool isPreventClose = await windowManager.isPreventClose();
|
||||
if (isPreventClose) {
|
||||
if (ref.watch(
|
||||
settingsProvider.select((value) => value.promptBeforeClosing))) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (_) => AlertDialog(
|
||||
title: const Text('Save Changes'),
|
||||
content:
|
||||
const Text('Want to save changes before you close API Dash?'),
|
||||
actions: [
|
||||
OutlinedButton(
|
||||
child: const Text('No'),
|
||||
onPressed: () async {
|
||||
Navigator.of(context).pop();
|
||||
await windowManager.destroy();
|
||||
},
|
||||
),
|
||||
FilledButton(
|
||||
child: const Text('Save'),
|
||||
onPressed: () async {
|
||||
await ref
|
||||
.read(collectionStateNotifierProvider.notifier)
|
||||
.saveData();
|
||||
Navigator.of(context).pop();
|
||||
await windowManager.destroy();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
} else {
|
||||
await windowManager.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const Dashboard();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
windowManager.removeListener(this);
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
class DashApp extends ConsumerStatefulWidget {
|
||||
class DashApp extends ConsumerWidget {
|
||||
const DashApp({super.key});
|
||||
|
||||
@override
|
||||
ConsumerState<DashApp> createState() => _DashAppState();
|
||||
}
|
||||
|
||||
class _DashAppState extends ConsumerState<DashApp> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final isDarkMode =
|
||||
ref.watch(settingsProvider.select((value) => value.isDark));
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
theme: ThemeData(
|
||||
visualDensity: VisualDensity.adaptivePlatformDensity,
|
||||
fontFamily: kFontFamily,
|
||||
fontFamilyFallback: kFontFamilyFallback,
|
||||
colorSchemeSeed: kColorSchemeSeed,
|
||||
useMaterial3: true,
|
||||
brightness: Brightness.light,
|
||||
visualDensity: VisualDensity.adaptivePlatformDensity,
|
||||
),
|
||||
darkTheme: ThemeData(
|
||||
fontFamily: kFontFamily,
|
||||
@ -77,6 +120,7 @@ class _DashAppState extends ConsumerState<DashApp> {
|
||||
colorSchemeSeed: kColorSchemeSeed,
|
||||
useMaterial3: true,
|
||||
brightness: Brightness.dark,
|
||||
visualDensity: VisualDensity.adaptivePlatformDensity,
|
||||
),
|
||||
themeMode: isDarkMode ? ThemeMode.dark : ThemeMode.light,
|
||||
home: kIsMobile
|
||||
|
Reference in New Issue
Block a user