mirror of
https://github.com/foss42/apidash.git
synced 2025-05-21 08:16:29 +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/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:window_manager/window_manager.dart' hide WindowCaption;
|
import 'package:window_manager/window_manager.dart' hide WindowCaption;
|
||||||
@ -18,6 +20,19 @@ class _AppState extends ConsumerState<App> with WindowListener {
|
|||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
windowManager.addListener(this);
|
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
|
@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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return const Dashboard();
|
return const Dashboard();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
void dispose() {
|
|
||||||
windowManager.removeListener(this);
|
|
||||||
super.dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class DashApp extends ConsumerStatefulWidget {
|
class DashApp extends ConsumerWidget {
|
||||||
const DashApp({super.key});
|
const DashApp({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
ConsumerState<DashApp> createState() => _DashAppState();
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
}
|
|
||||||
|
|
||||||
class _DashAppState extends ConsumerState<DashApp> {
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
final isDarkMode =
|
final isDarkMode =
|
||||||
ref.watch(settingsProvider.select((value) => value.isDark));
|
ref.watch(settingsProvider.select((value) => value.isDark));
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
debugShowCheckedModeBanner: false,
|
debugShowCheckedModeBanner: false,
|
||||||
theme: ThemeData(
|
theme: ThemeData(
|
||||||
visualDensity: VisualDensity.adaptivePlatformDensity,
|
|
||||||
fontFamily: kFontFamily,
|
fontFamily: kFontFamily,
|
||||||
fontFamilyFallback: kFontFamilyFallback,
|
fontFamilyFallback: kFontFamilyFallback,
|
||||||
colorSchemeSeed: kColorSchemeSeed,
|
colorSchemeSeed: kColorSchemeSeed,
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
brightness: Brightness.light,
|
brightness: Brightness.light,
|
||||||
|
visualDensity: VisualDensity.adaptivePlatformDensity,
|
||||||
),
|
),
|
||||||
darkTheme: ThemeData(
|
darkTheme: ThemeData(
|
||||||
fontFamily: kFontFamily,
|
fontFamily: kFontFamily,
|
||||||
@ -77,6 +120,7 @@ class _DashAppState extends ConsumerState<DashApp> {
|
|||||||
colorSchemeSeed: kColorSchemeSeed,
|
colorSchemeSeed: kColorSchemeSeed,
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
brightness: Brightness.dark,
|
brightness: Brightness.dark,
|
||||||
|
visualDensity: VisualDensity.adaptivePlatformDensity,
|
||||||
),
|
),
|
||||||
themeMode: isDarkMode ? ThemeMode.dark : ThemeMode.light,
|
themeMode: isDarkMode ? ThemeMode.dark : ThemeMode.light,
|
||||||
home: kIsMobile
|
home: kIsMobile
|
||||||
|
Reference in New Issue
Block a user