Skip to content

Commit

Permalink
feat: Warning and docs about fullscreen methods outside the mobile pl…
Browse files Browse the repository at this point in the history
…atforms (#3419)

Adds a warning when calling `fullScreen` and the new added method
`restoreFullscreen` when used in a desktop platform.

Also adds a brief warning to the docs about some methods in the device
class not working outside mobile

---------

Co-authored-by: Lukas Klingsbo <[email protected]>
  • Loading branch information
erickzanardo and spydon authored Dec 19, 2024
1 parent de8e3bc commit 994e098
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/flame/other/other.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

```{toctree}
:hidden:
Debugging <debug.md>
Utils <util.md>
Widgets <widgets.md>
Expand Down
7 changes: 7 additions & 0 deletions doc/flame/other/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ On this page you can find documentation for some utility classes and methods.

## Device Class

```{warning}
Many methods in this class only work on mobile platforms (Android and iOS).
Using these methods on other platforms will not have any effect and you will
get a warning printed on your console when running in debug mode.
```

This class can be accessed from `Flame.device` and it has some methods that can be used to control
the state of the device, for instance you can change the screen orientation and set whether the
application should be fullscreen or not.
Expand Down
23 changes: 23 additions & 0 deletions packages/flame/lib/src/device.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

/// Provides methods for controlling the device (e.g. setting the screen to
/// full-screen).
///
/// To use this class, access it via Flame.device.
class Device {
void _warnIfDesktop(String source) {
assert(() {
if (!kIsWeb &&
(Platform.isMacOS || Platform.isWindows || Platform.isLinux)) {
// ignore: avoid_print
print(
'Warning: $source is not supported on desktop platforms. '
'It will be a no-op.',
);
}
return true;
}());
}

/// Sets the app to be full-screen (no buttons, bar or notifications on top).
Future<void> fullScreen() {
_warnIfDesktop('fullScreen');
return SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
}

/// Restore the UI mode to the default ([SystemUiMode.edgeToEdge).
Future<void> restoreFullscreen() {
_warnIfDesktop('restoreFullscreen');
return SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
}

/// Sets the preferred orientation (landscape or portrait) for the app.
///
/// When it opens, it will automatically change orientation to the preferred
Expand Down

0 comments on commit 994e098

Please sign in to comment.