Line data Source code
1 : import 'dart:async'; 2 : import 'package:flutter/foundation.dart'; 3 : import 'package:flutter/services.dart'; 4 : 5 : /// Provides methods for controlling the device (e.g. setting the screen to full-screen). 6 : /// 7 : /// To use this class, access it via Flame.device. 8 : class Device { 9 : /// Sets the app to be full-screen (no buttons, bar or notifications on top). 10 0 : Future<void> fullScreen() { 11 : if (kIsWeb) { 12 : // TODO(erickzanardo): We probably could use dart:html and implement this for web as well 13 0 : return Future.value(); 14 : } 15 0 : return SystemChrome.setEnabledSystemUIOverlays([]); 16 : } 17 : 18 : /// Sets the preferred orientation (landscape or portrait) for the app. 19 : /// 20 : /// When it opens, it will automatically change orientation to the preferred one (if possible) depending on the physical orientation of the device. 21 0 : Future<void> setOrientation(DeviceOrientation orientation) { 22 : if (kIsWeb) { 23 0 : return Future.value(); 24 : } 25 0 : return SystemChrome.setPreferredOrientations( 26 0 : <DeviceOrientation>[orientation], 27 : ); 28 : } 29 : 30 : /// Sets the preferred orientations (landscape left, right, portrait up, or down) for the app. 31 : /// 32 : /// When it opens, it will automatically change orientation to the preferred one (if possible) depending on the physical orientation of the device. 33 0 : Future<void> setOrientations(List<DeviceOrientation> orientations) { 34 : if (kIsWeb) { 35 0 : return Future.value(); 36 : } 37 0 : return SystemChrome.setPreferredOrientations(orientations); 38 : } 39 : 40 : /// Sets the preferred orientation of the app to landscape only. 41 : /// 42 : /// When it opens, it will automatically change orientation to the preferred one (if possible). 43 0 : Future<void> setLandscape() { 44 0 : return setOrientations(<DeviceOrientation>[ 45 : DeviceOrientation.landscapeLeft, 46 : DeviceOrientation.landscapeRight, 47 : ]); 48 : } 49 : 50 : /// Sets the preferred orientation of the app to `DeviceOrientation.landscapeLeft` only. 51 : /// 52 : /// When it opens, it will automatically change orientation to the preferred one (if possible). 53 0 : Future<void> setLandscapeLeftOnly() { 54 0 : return setOrientation(DeviceOrientation.landscapeLeft); 55 : } 56 : 57 : /// Sets the preferred orientation of the app to `DeviceOrientation.landscapeRight` only. 58 : /// 59 : /// When it opens, it will automatically change orientation to the preferred one (if possible). 60 0 : Future<void> setLandscapeRightOnly() { 61 0 : return setOrientation(DeviceOrientation.landscapeRight); 62 : } 63 : 64 : /// Sets the preferred orientation of the app to portrait only. 65 : /// 66 : /// When it opens, it will automatically change orientation to the preferred one (if possible). 67 0 : Future<void> setPortrait() { 68 0 : return setOrientations(<DeviceOrientation>[ 69 : DeviceOrientation.portraitUp, 70 : DeviceOrientation.portraitDown, 71 : ]); 72 : } 73 : 74 : /// Sets the preferred orientation of the app to `DeviceOrientation.portraitUp` only. 75 : /// 76 : /// When it opens, it will automatically change orientation to the preferred one (if possible). 77 0 : Future<void> setPortraitUpOnly() { 78 0 : return setOrientation(DeviceOrientation.portraitUp); 79 : } 80 : 81 : /// Sets the preferred orientation of the app to `DeviceOrientation.portraitDown` only. 82 : /// 83 : /// When it opens, it will automatically change orientation to the preferred one (if possible). 84 0 : Future<void> setPortraitDownOnly() { 85 0 : return setOrientation(DeviceOrientation.portraitDown); 86 : } 87 : }