diff --git a/lib/ui/pages/workspace_page/components/top_bar/top_app_bar.dart b/lib/ui/pages/workspace_page/components/top_bar/top_app_bar.dart index 4b48b950..3249e47a 100644 --- a/lib/ui/pages/workspace_page/components/top_bar/top_app_bar.dart +++ b/lib/ui/pages/workspace_page/components/top_bar/top_app_bar.dart @@ -3,14 +3,18 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:paintroid/core/commands/command_manager/command_manager.dart'; import 'package:paintroid/core/commands/command_manager/command_manager_provider.dart'; import 'package:paintroid/core/enums/tool_types.dart'; +import 'package:paintroid/core/providers/object/io_handler.dart'; import 'package:paintroid/core/providers/state/app_bar_provider.dart'; import 'package:paintroid/core/providers/state/canvas_state_provider.dart'; import 'package:paintroid/core/providers/state/paint_provider.dart'; import 'package:paintroid/core/providers/state/toolbox_state_provider.dart'; +import 'package:paintroid/core/providers/state/workspace_state_notifier.dart'; import 'package:paintroid/core/tools/line_tool/line_tool.dart'; import 'package:paintroid/core/tools/tool.dart'; +import 'package:paintroid/core/utils/widget_identifier.dart'; import 'package:paintroid/ui/pages/workspace_page/components/top_bar/overflow_menu.dart'; import 'package:paintroid/ui/shared/action_button.dart'; +import 'package:paintroid/ui/shared/dialogs/discard_changes_dialog.dart'; import 'package:paintroid/ui/utils/top_bar_action_data.dart'; class TopAppBar extends ConsumerWidget implements PreferredSizeWidget { @@ -21,6 +25,31 @@ class TopAppBar extends ConsumerWidget implements PreferredSizeWidget { @override Size get preferredSize => const Size.fromHeight(kToolbarHeight); + Future _onBack(BuildContext context, WidgetRef ref) async { + final isFullscreen = ref.watch( + workspaceStateProvider.select((state) => state.isFullscreen), + ); + final ioHandler = ref.watch(IOHandler.provider); + final workspaceStateNotifier = ref.watch(workspaceStateProvider.notifier); + + if (isFullscreen) { + workspaceStateNotifier.toggleFullscreen(false); + return null; + } + if (!workspaceStateNotifier.hasSavedLastWork) { + final shouldDiscard = await showDiscardChangesDialog(context); + if (shouldDiscard != null && !shouldDiscard && context.mounted) { + bool savedImage = await ioHandler.saveImage(context); + if (!savedImage) { + return null; + } + } + } + if (!context.mounted) return null; + Navigator.pop(context); + return null; + } + void Function()? _onUndo( Tool currentTool, CommandManager commandManager, @@ -100,6 +129,13 @@ class TopAppBar extends ConsumerWidget implements PreferredSizeWidget { ref.watch(appBarProvider); return AppBar( + leading: IconButton( + key: const ValueKey(WidgetIdentifier.backButton), + icon: const Icon(Icons.arrow_back), + onPressed: () async { + await _onBack(context, ref); + }, + ), title: Text(title), centerTitle: false, actions: [ diff --git a/lib/ui/pages/workspace_page/workspace_page.dart b/lib/ui/pages/workspace_page/workspace_page.dart index 6d31be18..04ed8cec 100644 --- a/lib/ui/pages/workspace_page/workspace_page.dart +++ b/lib/ui/pages/workspace_page/workspace_page.dart @@ -1,17 +1,13 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; - import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:toast/toast.dart'; - -import 'package:paintroid/core/providers/object/io_handler.dart'; import 'package:paintroid/core/providers/state/workspace_state_notifier.dart'; import 'package:paintroid/ui/pages/workspace_page/components/bottom_bar/bottom_nav_bar.dart'; import 'package:paintroid/ui/pages/workspace_page/components/bottom_bar/tool_options/tool_options.dart'; import 'package:paintroid/ui/pages/workspace_page/components/drawing_surface/drawing_canvas.dart'; import 'package:paintroid/ui/pages/workspace_page/components/drawing_surface/exit_fullscreen_button.dart'; import 'package:paintroid/ui/pages/workspace_page/components/top_bar/top_app_bar.dart'; -import 'package:paintroid/ui/shared/dialogs/discard_changes_dialog.dart'; +import 'package:toast/toast.dart'; class WorkspacePage extends ConsumerStatefulWidget { const WorkspacePage({super.key}); @@ -39,30 +35,7 @@ class _WorkspaceScreenState extends ConsumerState { workspaceStateProvider.select((state) => state.isFullscreen), (_, isFullscreen) => _toggleStatusBar(isFullscreen), ); - final ioHandler = ref.watch(IOHandler.provider); - final workspaceStateNotifier = ref.watch(workspaceStateProvider.notifier); return PopScope( - canPop: false, - onPopInvoked: (didPop) async { - if (didPop) { - return; - } - if (isFullscreen) { - workspaceStateNotifier.toggleFullscreen(false); - return; - } - if (!workspaceStateNotifier.hasSavedLastWork) { - final shouldDiscard = await showDiscardChangesDialog(context); - if (shouldDiscard != null && !shouldDiscard && context.mounted) { - bool savedImage = await ioHandler.saveImage(context); - if (!savedImage) { - return; - } - } - } - if (!context.mounted) return; - Navigator.pop(context); - }, child: Scaffold( appBar: isFullscreen ? null : const TopAppBar(title: 'Pocket Paint'), backgroundColor: Colors.grey.shade400,