Skip to content

Commit

Permalink
Tap button to take screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
calcitem committed Apr 27, 2024
1 parent 598ff6e commit 393cbfe
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
76 changes: 71 additions & 5 deletions src/ui/flutter_app/lib/game_page/widgets/game_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import 'dart:async';
import 'dart:io' show Platform;
import 'dart:io';
import 'dart:ui';

import 'package:catcher/catcher.dart';
Expand All @@ -24,6 +24,10 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hive/hive.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:screenshot/screenshot.dart';

import '../../appearance_settings/models/display_settings.dart';
import '../../custom_drawer/custom_drawer.dart';
Expand Down Expand Up @@ -160,6 +164,62 @@ class _Game extends StatefulWidget {
}

class _GameState extends State<_Game> {
final ScreenshotController screenshotController = ScreenshotController();

Future<void> triggerScreenshot() async {
await _takeScreenshot();
}

Future<void> _takeScreenshot() async {
logger.i("Attempting to capture screenshot...");
final Uint8List? image = await screenshotController.capture(
delay: const Duration(milliseconds: 100));
if (image == null) {
logger.e("Failed to capture screenshot: Image is null.");
return;
}
logger.i("Screenshot captured, proceeding to save...");
await saveImage(image, 'screenshot.png');
}

Future<void> saveImage(Uint8List image, String filename) async {
try {
final result = await ImageGallerySaver.saveImage(image, name: filename);

Check warning on line 187 in src/ui/flutter_app/lib/game_page/widgets/game_page.dart

View workflow job for this annotation

GitHub Actions / Lint flutter code

Missing type annotation.. See https://dart.dev/tools/diagnostic-messages#always_specify_types

if (result is Map) {
final Map<String, dynamic> resultMap =
Map<String, dynamic>.from(result);

if (resultMap['isSuccess'] == true) {
logger.i("Image saved to Gallery with path ${resultMap['filePath']}");
} else {
logger.e("Failed to save image to Gallery");
}
} else {
logger.e("Unexpected result type");
}
} catch (e) {
logger.e("Failed to save image: $e");
}
}

Future<String?> getFilePath(String filename) async {
Directory? directory;
// TODO: Change to correct path
if (Platform.isAndroid) {
directory = await getExternalStorageDirectory();
} else {
directory = await getApplicationDocumentsDirectory();
}

// Ensure directory exists
if (directory != null) {
return path.join(directory.path, filename);
} else {
return null;
}
}

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -317,11 +377,11 @@ class _GameState extends State<_Game> {
List<Widget> analysisToolbarItems(BuildContext context) {
final ToolbarItem captureBoardImageButton = ToolbarItem(
child: Icon(
FluentIcons.arrow_previous_24_regular,
FluentIcons.camera_24_regular,
// TODO
semanticLabel: S.of(context).takeBackAll,
semanticLabel: S.of(context).welcome,
),
onPressed: () => HistoryNavigator.takeBackAll(context, pop: false),
onPressed: () => triggerScreenshot(),
);

return <Widget>[
Expand Down Expand Up @@ -432,7 +492,13 @@ class _GameState extends State<_Game> {
])
else
const SizedBox(height: AppTheme.boardMargin),
const GameBoard(),
Screenshot(
controller: screenshotController,
child: Container(
alignment: Alignment.center,
child: const GameBoard(),
),
),
if ((DB().displaySettings.isUnplacedAndRemovedPiecesShown ||
GameController().gameInstance.gameMode ==
GameMode.setupPosition) &&
Expand Down
6 changes: 6 additions & 0 deletions src/ui/flutter_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@ dependencies:
hive: 2.2.3
hive_flutter: 1.1.0
image: 4.1.3
image_gallery_saver:
git:
url: https://github.com/knottx/image_gallery_saver.git
ref: knottx
intl: 0.18.1
json_annotation: 4.8.1
kplayer: 0.4.2
logger: 1.4.0
marquee: 2.2.3
package_info_plus: 4.1.0
path: 1.9.0
path_provider: 2.1.1
share_plus: 7.1.0
sliver_tools: 0.2.12
Expand All @@ -52,6 +57,7 @@ dependencies:
url: https://gitlab.com/calcitem/soundpool.git
ref: windows_linux_support
path: soundpool_windux
screenshot: 2.3.0
url_launcher: 6.1.14
uuid: 3.0.7

Expand Down

0 comments on commit 393cbfe

Please sign in to comment.